Save data on button click jquery

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 id="place_order" class="btn btn-black">PLACE ORDER</button>


function place_order() {
		var action = "Place Order";
			type: 'POST',
			url: '../pages/class.php',
			data: {action: action},
			success:function(data) {


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

Is the session set? item_cart If the session is not set this will not work. Also having error messages for when something is not as is should be will help a ton when it comes to debugging

You have a line


but it runs inside the if() that checks whether the first transaction query has executed. As I read it, that means your code will write the first transaction for the first line in the cart, then it’ll update the quantity for the product on that line, then it will unset the entire shopping cart.

As @jgetner asked, have you enabled the session in your PHP code but just not posted it? And the same question for your database connection code?

This line also concerns me, but for different reasons:

$product_stocks = $val['product_stocks']; //product stocks

You seem to be storing the stock holding of a particular item in the shopping cart. While it might be useful to store that, what about if or when it changes between the time the user starts shopping, and checks out? Maybe some more stock arrived and got booked in, maybe some other users bought the same product while the first user was thinking about it. Surely the available-stock check needs to be based on the actual current stock, not the stock as it was when the line was added to the cart?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.