php - 更新产品数量(php和Mysql)

标签 php html mysql session phpmyadmin

我正在尝试更新basket.php页面中的产品数量。该数量还将发布在 MySQL 数据库的 basket 表中的 qty 列下。但不幸的是它并没有像我希望的那样工作。

问题:

  1. 更新数量仅适用于购物篮中的一件商品。
  2. 在框中输入数量“3”并单击 update_qty 按钮后,框中的值将更改回 1。但价格已正确更新。
  3. 添加多个商品并更新其数量将导致总价错误,并且最后一个商品的数量将保存在购物篮表中所有商品的数量列中。

数据库结构:

篮子 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

这是我的代码:

篮子.php

   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>

functions.php

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

我如何更改代码来解决这些问题。

谢谢。

最佳答案

@connor991,你的代码非常笨拙。你需要重写你的代码。我会建议两种方法。

但首先您需要有一个表单,其中显示所有商品并让用户选择要购买的商品。然后您将他们重定向到您提供更多信息的地方,然后他们选择质量。

然后,第一种方法:显示购物车中的所有商品时,将它们显示在单独的 form 标记中。这样,您就有了一个包含当前数量文本框,用户可以根据需要进行更新。您还提供了一个submit 按钮来提交表单以进行更新。这样,您就可以获取单独的 food_idqty 并进行相应更新。 我还意识到,由于一个用户ip可以购买多件商品,因此仅根据ip_add更新将更新他/她的ip_add下的所有产品。因此,您需要这样做:

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

第二个选项是显示表格中的所有项目,而不提供textbook来立即更新qty。您针对每个购物车项目显示一个 update 链接,以便当他们单击该链接时,您可以将该特定购物车项目的 food_id 和 ip_id 传递到新页面,您可以在该页面中从数据库获取数据并立即显示它们加上qty,但使用教科书中的qty。当用户单击此页面上的更新按钮时,您将从该特定页面检索值并执行更新。

希望这有帮助。

关于php - 更新产品数量(php和Mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530367/

相关文章:

html - 如何在将鼠标悬停在元素上时阻止其他表格元素向右移动

javascript - 需要每行总值并且应显示在行末尾

MySQL 返回一个空结果集(即零行),其中期望多于零行

mysql - 如何确定地球表面的哪些图像与任意给定图像重叠?

php - 在不同表中具有相同名称的三个列中查找相同的值

php - 供应商/autoload.php : failed to open stream

php - 将 html 网站转换为简单的 PHP 模板网站

php - 在 CodeIgniter 中连接两个或多个表

javascript - Bootstrap 选项卡中的 CSS 阴影和圆 Angular

jquery - 显示后选项的高度搞砸了