php - 更新一行而不影响其他行

标签 php mysql

here's the output

因此,当我单击(+)按钮和(-)按钮时,其他行都会受到影响,正如您在“生成”列的图片中看到的那样,我想要的是,只有选定的行才会受到我单击的影响(+) 按钮和 (-) 按钮。

所以这是代码:

foreach( $result as $row ) {

$data_id = $row['product_id'];
$data_product_name = $row['product_name'];
$data_price = $row['price'];
$data_total_price = $row['total'];
$data_quantity = $row['quantity'];
$data_expiration_date = $row['expiration_date'];

 echo <<<EOD
   <div class="table_row">
     <div class="table_column 1">
       Select <input type="checkbox" name="checkbox_select[]" value=$data_id />
     </div>  
       <div class="table_column 2">$data_id</div>
       <div class="table_column 3">$data_product_name</div>
       <div class="table_column 4">P $data_price</div>
       <div class="table_column 5">P $data_total_price</div>
       <div class="table_column 6">$data_quantity
           <button name="button_add_quantity" class="buttons_add_subtract">+</button>
           <button name="button_subtract_quantity" class="buttons_add_subtract">-</button>
       </div>
     <div class="table_column 7">
         Expiration Date: $data_expiration_date<br>
         Time Left:
     </div>
     </div>
EOD;


if ($_SERVER['REQUEST_METHOD'] === 'POST') {

 if (isset($_POST['button_add_quantity'])) {

   $data_quantity += 1;
   $statement_update = $pdo_connection->prepare("UPDATE finished_goods SET quantity=?
   WHERE product_id IN ('$data_id')");
   $statement_update->execute([$data_quantity]);
   header('refresh:0');

   } elseif (isset($_POST['button_subtract_quantity'])) {

   $data_quantity -= 1;
   $statement_update = $pdo_connection->prepare("UPDATE finished_goods SET quantity=?
   WHERE product_id IN ('$data_id')");
   $statement_update->execute([$data_quantity]);
   header('refresh:0');

     }

    }
   }

任何帮助都可以:)

最佳答案

我建议您使用 ajax 来实现此目的,并确保您的按钮动态填充产品的 ID。

     <button name="button_add_quantity" class="buttons_add_subtract" id="add_$data_id">+</button>
   <button name="button_subtract_quantity" class="buttons_add_subtract" id="minus_$data_id">-</button>

两个按钮现在都将有一个唯一的 ID,前缀为减号,或者添加产品 ID 作为后缀。

然后使用ajax处理按钮点击。

script type="text/javascript">
  $('document').ready(function(){
    //minus button

    $('[id^="minus_"]').on('click',function(event){
        event.preventDefault();
        var index = $(this).attr('id').split("_")[1]; //get the id of the click product

        $.ajax({
          type :"POST",
          data : {id:index,action:"minus"},
          url  : "somefile.php",
          dataType : "json",
          encode : true,
          success : function(data){
            //after ajax success do something
            if(data == "ok"){
              //updated success
              location.reload();

            }else{

              alert(data); //alert the error
            }
          }
        });
  });

    //add button
    $('[id^="add_"]').on('click',function(event){
        event.preventDefault();
        var index = $(this).attr('id').split("_")[1];

        $.ajax({
          type :"POST",
          data : {id:index,action:"add"},
          url  : "somefile.php",
          dataType : "json",
          encode : true,
          success : function(data){
            //after ajax success do something
            if(data == "ok"){
              //updated success
              location.reload();

            }else{

              alert(data); //alert the error
            }
          }
        });
  });

  });
</script>

然后

somefile.php

<?php
    include 'YOUR CONNECTION FILE';


    function minus($pdo_connection){

        $id = isset($_POST['id']) ? $_POST['id']:null;
            //update make sure does not go to negative quantity
        $statement_update = $pdo_connection->prepare("UPDATE finished_goods SET quantity = quantity - 1 WHERE product_id = ? AND quantity > 0 ")->execute([$id]);

        if($statement_update){

            echo json_encode("ok");//success
        }else{

            echo json_encode("error updating"); //error investigate the error
        }
    }

    function add($pdo_connection){

        $id = isset($_POST['id']) ? $_POST['id']:null;

        $statement_update = $pdo_connection->prepare("UPDATE finished_goods SET quantity = quantity + 1 WHERE product_id = ? ")->execute([$id]);

        if($statement_update){

            echo json_encode("ok");
        }else{

            echo json_encode("error updating"); //error investigate the error
        }
    }

    if($_SERVER['REQUEST_METHOD'] === "POST"){

        $request_action = isset($_POST['action']) ? $_POST['action']:null;

        switch ($request_action) {
            case 'minus':
                minus($pdo_connection); // call the minus function
                break;

            case 'add':
                add($pdo_connection);
                break;
        }
    }

关于php - 更新一行而不影响其他行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49153259/

相关文章:

c# - UDP广播从PHP发送并在C#中接收

php - 如何配置和传输 MySQL(使用登录凭据)从 XAMPP 本地网络服务器到基于 Hostgator/Linux 的托管服务器?

python - 在多个 session 中更新/选择 mysql 表

MySQL查询性能?

sql - MYSQL 附加日期时如何按日期 desc 排序?

mysql - 仅从另一个表中获取 1 个条目

mysql - 随机时间跨度后,AWS RDS MySQL性能下降

php - JSON Android PHP 如何纠正错误或如何显示数据

PHP XPath 问题

javascript - 如何在php页面中多次使用同一个onclick函数?