简体   繁体   中英

Updating product quantity (php and Mysql)

I am trying to update the product quantity in the basket.php page. The quantity would also be posted on MySQL database under coloumn in the table. 表格中的coloumn 下发布在MySQL数据库中。 But unfortunately it doesn't work as i had hoped.

  1. Updating quantity only works for one item in the basket.
  2. After entering the quantity in the box say "3" and clicking the update_qty button, the value in the box is changed back to one. but the price is correctly updated.
  3. Adding multiple items and updating their quantity will result in a wrong total price and the quantity of the last item is saved in the qty column for all items in the basket table.

f_id type int(10) primary key//food id

ip_add type  varchar(225)

qty type int(10)//quantity in basket

1   food_id type    int(100) AUTO_INCREMENT primary key

 2  food_cat type   int(100)            

3   food_type type  int(100)            

4   food_title type varchar(255)    

 5  food_price type float(23,2) 

6   food_desc type  text    

7   food_image type text    

8   food_keywords type  text

   session_start();
    include ("functions/functions.php");

    <form action = " " method="post" enctype="multipart/form-data">
    <table>
    <tr>
    <th>Remove</th>
    <th>Dish(s)</th>
    <th>Quantity</th>
    <th>Price</th>
    </tr>

    <?php 

    $total = 0;

   global $con;

   $ip = getIp();

   $sel_price = "select * from basket where ip_add='$ip'";

   $run_price = mysqli_query($con, $sel_price);

  while ($p_price = mysqli_fetch_array($run_price)) {

  $foo_id = $p_price['f_id'];

  $foo_price = "select * from food where food_id ='$foo_id'";

  $run_foo_price = mysqli_query($con, $foo_price);

  while ($ff_price = mysqli_fetch_array($run_foo_price)) {

    $food_price = array ($ff_price['food_price']);

    $food_title = $ff_price['food_title'];

    $food_image = $ff_price ['food_image'];

    $single_price = $ff_price['food_price'];

    $values = array_sum($food_price);

    $total += $values;
    ?>
    <?php 
  //updates quantity of items in basket
  if (isset($_POST['update_qty'])){

  $qty = $_POST['qty'];
  $update_qty = "update basket set qty='$qty'";
  $run_qty = mysqli_query($con, $update_qty);


  $total = $values*$qty;

  }
    ?>
<input type="number"  name="qty" value="<?php   echo $_SESSION['qty']; ?>" >


 <?php } } ?>


<b>Sub Total:</b>

 <?php echo "£". $total;?>

    <div id="up">
    <input type="submit" name="update_basket" value="Remove">
    </div>
    <div id="up">
    <input type="submit" name="update_qty" value="Update Quantity">
    </div>
     <div id="con">
     <input type="submit" name="continue" value="Continue Shopping">
    </div>
    <div id="chck">
     <a href="checkout.php"><button  type="hidden">Checkout</button></a>

    </div>

      //Creates the basket
     function basket(){

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

      global $con;

    $ip = getIp();

    $foo_id = $_GET['add_basket'];

   $check_foo = "select * from basket where ip_add= '$ip' AND f_id =  
   '$foo_id'";

   $run_check = mysqli_query($con, $check_foo);

  if(mysqli_num_rows($run_check)>0){

   echo " ";
  }

 else {

 $insert_foo = "insert into basket (f_id,ip_add) values ('$foo_id', '$ip')";
 $run_foo = mysqli_query($con, $insert_foo);

  echo "<script>window.open('order.php','_self')</script>";
   }
 }

 }

   //Get total items in Basket
   function total_items(){

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

      global $con;

      $ip = getIp();

      $get_items = "select * from basket where ip_add='$ip'";

      $run_items = mysqli_query($con, $get_items);

      $count_items = mysqli_num_rows($run_items);

      }

      else {

        global $con;

        $ip = getIp();

    $get_items = "select * from basket where ip_add='$ip'";

    $run_items = mysqli_query($con, $get_items);

    $count_items = mysqli_num_rows($run_items);

     }

    echo $count_items;
     }

  //get the total price of items in basket
 function total_price(){

  $total = 0;

   global $con;

  $ip = getIp();

  $sel_price = "select * from basket where ip_add='$ip'";

  $run_price = mysqli_query($con, $sel_price);

    while ($p_price = mysqli_fetch_array($run_price)) {

    $foo_id = $p_price['f_id'];

    $foo_price = "select * from food where food_id ='$foo_id'";

    $run_foo_price = mysqli_query($con, $foo_price);

    while ($ff_price = mysqli_fetch_array($run_foo_price)) {

    $food_price = array ($ff_price['food_price']);

    $values = array_sum($food_price);

    $total += $values;

    }
   }

 echo "£" . $total;
    }

How can i change my code to solve these problems.

thanks.

@connor991, Your code is very clumsy. You need to rewrite your code. I'll suggest two ways.

But first of all you need to have a form where all items are displayed and the user selects an item to purchase. Then you redirect them to a place where you give more information and then they choose quality.

Then, first method: When displaying all items in the cart, you display them in separate form tags. This way, you have a textbox with current qty where the user can update if they wish to. You also provide a submit button to submit the form for update. This way, you can fetch individual food_id s and qty s and update accordingly. I also realized that since one user ip can purchase more than one item, updating according to just ip_add will update all products under his/her ip_add . Therefore, you need to do this:

// Assuming that you have ip and food_id values already
UPDATE basket SET qty=$qty WHERE ip_add=$ip AND food_id=$food_id

Second option is to display all items in a table without providing textbook to update qty right away. You display an update link against each cart item so that when they click on it, you pass the food_id and ip_id of that particular cart item to a new page where you fetch the data from database and now display them plus the qty but with the qty in a textbook . When the user clicks on the update button on this page, you retrieve the values from this particular page and perform the update.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM