What’s wrong with my code? I can’t even place an order or save it on database. I’m using ajax/jquery, it reaches the success but the problem is, it doesn’t save in database.
PS: I also included place_order(); under script type.
Button:
<button id="place_order" class="btn btn-black">PLACE ORDER</button>
Jquery:
function place_order() {
$("#place_order").click(function(e){
var action = "Place Order";
$.ajax({
type: 'POST',
url: '../pages/class.php',
data: {action: action},
success:function(data) {
//location.href="../pages/index.php";
}
})
});
}
Class.php
if($_POST['action'] == "Place Order") {
if(isset($_SESSION['item_cart'])) {
foreach($_SESSION['item_cart'] as $id=>$val) {
$user_id = $_SESSION['id']; //id of users
$product_stocks = $val['product_stocks']; //product stocks
$product_id = $val['product_id']; //id of product/item
$product_name = $val['product_name']; //name of product
$product_quantity = $val['product_qty']; //quantity of product
$product_price = $val['product_price']; //price of product
$product_size = $val['product_size']; //size of product
//Total Price
$total = $product_quantity * $product_price;
//Check if the stocks is less than quantity
if ($product_stocks < $product_quantity) {
echo "Insufficient Stock";
} else {
//Insert it on database
$insert_query = "INSERT INTO tbltransactions(product_name, product_price, product_qty, total_price, product_id, account_id) VALUES('$product_name', $product_price, $product_quantity, $total, $product_id, $user_id)";
$query = mysqli_query($db_conn, $insert_query);
//If the query is success, update the stocks in database
if($query) {
$update_query = "UPDATE tblproduct_extension SET product_stocks = $product_stocks - $product_quantity WHERE product_id = '$product_id' AND product_size='$product_size'";
$query = mysqli_query($db_conn, $update_query);
//unset the SESSION cart
unset($_SESSION['item_cart']);
}
}
}
}
}