Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. <?php
  2. include_once('config.php');
  3.  
  4. if (isset($_REQUEST['id']))
  5. {
  6. if (ctype_digit($_REQUEST['id']) &&
  7. !isset($_REQUEST['qty']))
  8. {
  9. $productID = $_REQUEST['id'];
  10.  
  11. addToCart($con, $productID, 1);
  12. }
  13. else if (ctype_digit($_REQUEST['id']) &&
  14. isset($_REQUEST['qty']))
  15. {
  16. $productID = $_REQUEST['id'];
  17. $quantity = $_REQUEST['qty'];
  18. addToCart($con, $productID, $quantity);
  19. }
  20. else
  21. {
  22. header('location: products.php');
  23. }
  24. }
  25. else
  26. {
  27. header('location: products.php');
  28. }
  29.  
  30. function getPrice($con, $pid)
  31. {
  32. $sql_price = "SELECT price FROM products
  33. WHERE productID=$pid";
  34. $result_price = $con->query($sql_price)
  35. or die(mysqli_error($con));
  36. return mysqli_fetch_object($result_price)->price;
  37. }
  38.  
  39. function isExisting($con, $pid)
  40. {
  41. $sql_existing = "SELECT productID FROM orders_details
  42. WHERE orderNo=0 AND userID=1 AND productID=$pid";
  43. $result_existing = $con->query($sql_existing)
  44. or die(mysqli_error($con));
  45. return mysqli_num_rows($result_existing) > 0 ? true : false;
  46. }
  47.  
  48. function addToCart($con, $pid, $qty)
  49. {
  50. $price = getPrice($con, $pid);
  51. $amount = $price * $qty;
  52.  
  53. $sql_cart = isExisting($con, $pid) ?
  54. "UPDATE orders_details SET quantity = quantity + $qty,
  55. amount = amount + $amount
  56. WHERE orderNo=0 AND userID=1 AND productID=$pid" :
  57. "INSERT INTO orders_details VALUES ('', 0,
  58. 1, $pid, $price, $qty, $amount)";
  59. $result_cart = $con->query($sql_cart)
  60. or die(mysqli_error($con));
  61. }
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement