Guest User

Untitled

a guest
Dec 28th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. <?php
  2. session_start();
  3. $user="root";
  4. $pass="";
  5. $host="localhost";
  6. $dbnamne="neardeal";
  7.  
  8. $name = $_REQUEST['name'];
  9. $noHp = $_REQUEST['no_hp'];
  10. $address = $_REQUEST['address'];
  11. $productStr = $_REQUEST['productIds']; //productIds string separated by ,
  12. $pricesStr = $_REQUEST['prices']; //productIds string separated by ,
  13.  
  14. //create connection
  15. $conn = new mysqli($host, $user, $pass, $dbnamne);
  16.  
  17. //check connection
  18. if($conn->connect_error){
  19. die("connection failed" .$com->connect_error);
  20. }
  21.  
  22. if(!isset($_SESSION['user_id'])) {
  23. die("You're not allowed to access this api");
  24. }
  25.  
  26. $user_id = $_SESSION['user_id'];
  27. $sql = "INSERT INTO order VALUES(NULL, {$user_id}, '{$name}', '{$noHp}', '{$address}')";
  28. $conn->query($sql);
  29.  
  30. $order_id = $conn->insert_id;
  31. $productIds = explode(",", $productStr);
  32. $prices = explode(",", $pricesStr);
  33.  
  34. foreach($productIds as $k=>$productId) {
  35. $sql = "INSERT INTO item_order VALUES(NULL, {$order_id}, {$productId}, {$prices[$k]})";
  36. $conn->query($sql);
  37. }
  38.  
  39. echo json_encode(array(
  40. 'success' => true,
  41. 'order_id' => $order_id
  42. ));
Add Comment
Please, Sign In to add comment