单击下订单后,将数据保存在数据库中

时间:2017-11-25 08:40:27

标签: javascript php jquery ajax mysqli

我的代码出了什么问题?我甚至无法下订单或将其保存在数据库中。我正在使用ajax / jquery,它达到了成功但问题是,它不会保存在数据库中。

PS:我还包括了place_order();在脚本类型下。

按钮:

<button id="place_order" class="btn btn-black">PLACE ORDER</button>

Jquery:

//Placing Order / Complete the Transaction
function place_order() {
    $('#place_order').click(function(e){
        var place_order = $('#place_order').val();
        $.ajax({
            type: 'POST',
            url: '../pages/class.php',
            data: {place_order:place_order},
            success:function(data) {
                location.href="../pages/index.php";
            }
        });
    });
}

Class.php

if(isset($_POST['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
                      unset($_SESSION['item_cart']);
                  }
              }   
          }   
      }   
  }   

transac

1 个答案:

答案 0 :(得分:0)

没有为变量place_order

分配值
     data: {place_order:place_order},

使用

为变量赋值
    var place_order = $("#place_order").val(); //change field name