如何使用PHP中的Session创建购物车

时间:2016-08-20 08:46:54

标签: javascript php mysql

我在创建购物车时遇到了麻烦。如何进行输出,以便当我单击产品表上的“添加购物车”按钮时,它将显示在购物车上?

//Product List
<?php 

    $query = "SELECT * FROM products";
    $exec = mysqli_query($connection, $query);

    while ($row = mysqli_fetch_array($exec)) {
      $product_id = $row['product_id'];
      $product_name = $row['product_name'];
      $product_quantity = $row['quantity'];
      $product_price = $row['sell_price'];


   ?>
      <tr>
          <td class="text-center"><?php echo $product_id; ?></td>
          <td><?php echo $product_name; ?></td>
          <td><?php echo $product_price; ?></td>
          <td><?php echo $product_quantity; ?></td>
          <td class="text-center">
            <div class="btn-group">
              <a href="add_sales.php?action=add&product_id=<?php echo $product_id; ?>"  class="btn btn-xs btn-info" data-toggle="tooltip" title="Select">
                <span class="fa fa-shopping-cart"></span>
              </a>
            </div>
          </td>
      </tr>
      <?php } ?>

项目详细信息面板

    if (isset($_GET['product_id'])) {

      $prod_id = $_GET['product_id'];

      $selectProd = "SELECT * FROM products WHERE product_id = $prod_id";
      $execProd = mysqli_query($connection, $selectProd);

      while ($row = mysqli_fetch_array($execProd)) {

        $prod_name = $row['product_name'];
        $prod_price = $row['sell_price'];
        $quantity = $row['quantity'];
  ?>


    <div class="panel-body"> 
      <div class="col-md-2">
       <div class="form-group">
        <label for="">Item ID</label>
          <input type="text" class="form-control" value="<?php echo $prod_id; ?>" readonly>
          </div>
      </div>

      <div class="col-md-7">
       <div class="form-group">
        <label for="">Item Name</label>
          <input type="text" class="form-control" value="<?php echo $prod_name ?>" readonly>
          </div>
      </div>

      <div class="col-md-4">
        <div class="form-group">
          <label for="">Unit Price</label>
            <input type="text" class="form-control" value="<?php echo $prod_price; ?>" readonly id="unitPrice">
        </div>
      </div>

      <div class="col-md-5">
        <div class="form-group">
          <label for="">Available Qty</label>
            <input type="text" class="form-control" value="<?php echo $quantity ?>" readonly>
        </div>
      </div>

      <div class="col-md-4">
        <div class="form-group">
          <label for="">Sale Qty</label>
            <input type="number" class="form-control" id="saleQty" name="saleQty">
        </div>
      </div>

      <div class="col-md-5">
        <div class="form-group">
          <label for="">Total Amount</label>
            <input type="text" class="form-control" id="totalSale" name="saleTotal" readonly>
        </div>
      </div>

      <div class="col-md-4">
        <div class="form-group">
            <input type="submit" value="Add to Cart" class="btn btn-info form-control" formnovalidate>
        </div>
      </div>

    <!-- Start of Customer's Cart -->
<div class="col-md-12"> 
  <div class="panel panel-default">
    <div class="panel-heading">
      <strong>
        <span class="fa fa-shopping-cart"></span>
        <span>Customer's Cart</span>
      </strong>
    </div>
  <div class="panel-body">
    <table class="table table-hover">
      <thead>
        <tr>
          <th>Product ID</th>
          <th>Product Name</th>
          <th>Product Quantity</th>
          <th>Product Amount</th>
          <th>Total Amount</th>
          <th>Remove</th>
        </tr>
      </thead>
      <tbody>

        <tr>
          <td></td>
        </tr>


      </tbody>
    </table>
   </div>
  </div>
</div> 
<!-- End of Customer Cart -->

Ordering Page Flow (GIF)

1 个答案:

答案 0 :(得分:0)

当您提交表单,URL查询参数(如果表单具有GET方法)或请求正文参数(POST方法)时,您的input元素都没有name属性(我认为) )无法获得,你无法抓住&#34;提交订单的人的行为。如果您添加它们并处理表单提交(例如if isset($_POST['order']),您可以INSERT将订单导入数据库并从数据库表中呈现购物车。同时查看{ {3}}以避免SQL注入。