http://ipset.netfilter.org/iptables.man.html
因此,当我点击(+)按钮和( - )按钮时,其他行会受到影响,正如您在“生产”图片中看到的那样。列,我想要的是,只有一个选定的行会受到影响,我点击(+)按钮和( - )按钮。
所以这里是代码:
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');
}
}
}
任何帮助都可以:)
答案 0 :(得分: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;
}
}