How to Create a Shopping Cart using Session in PHP

javascript php mysql

243 просмотра

1 ответ

I'm having trouble creating a shopping cart. How do I make the output so that when I click the add cart button on my products table, it will appear on the cart?

//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 } ?>

Item Details Panel

    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>

Cart

    <!-- 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)

Автор: Odie Источник Размещён: 08.11.2019 11:05

Ответы (1)


0 плюса

None of your input element have name attributes so (I think) when you submit the form, the URL query parameters (if the form has GET method) or the request body parameters (POST method) won't be available and you can't "catch" the act of someone submitting the order. If you add them and handle the form submission (say by ifing on a isset($_POST['order']) you can INSERT the order into the DB and render the cart from the DB table. Also look into http://php.net/manual/en/mysqli.prepare.php to avoid SQL injection.

Автор: Tomáš Hübelbauer Размещён: 20.08.2016 09:27
Вопросы из категории :
32x32