添加新库存时,我正在尝试更新库存数量。我使用数组是因为我想在页面中添加多个产品。但是数据不会插入数据库,也不会更新库存数量。我会在此代码上错过什么?
<?php
include 'db_connection.php';
if(isset($_POST["submit4"])){
foreach($_POST['fk_product'] as $index => $searchid){
$sql="INSERT INTO supplier (supplier_name, date_in, fk_product, quantity_in) VALUES ('".$_POST['supplier_name']."','".$_POST['date_in']."','".$_POST['fk_product'][$index]."', '".$_POST["quantity_in"][$index]."')";
$quantity_bal=$_POST['quantity_bal'];
if($quantity_bal==0){
$sql="UPDATE product set quantity_bal= quantity_ori + '".$_POST["quantity_in"][$index]."' WHERE id_produk = '".$_GET['id']."'";
}else if($quantity_bal!=0){
$sql="UPDATE product set quantity_bal= quantity_bal + '".$_POST["quantity_in"][$index]."' WHERE id_produk = '".$_GET['id']."'";
}
$query = mysqli_multi_query($conn,$sql);
if($query){
header("location:product.php");
}
}
}
?>
答案 0 :(得分:-2)
您所缺少的是,您需要使用=
赋值运算符来连接查询,您刚刚覆盖了前一个字符串,所以应该像这样
foreach($_POST['fk_product'] as $index => $searchid){
$sql = "INSERT INTO supplier (supplier_name, date_in, fk_product, quantity_in) VALUES ('".$_POST['supplier_name']."','".$_POST['date_in']."','".$_POST['fk_product'][$index]."', '".$_POST["quantity_in"][$index]."')";
$quantity_bal = $_POST['quantity_bal'];
if($quantity_bal == 0){
//I'm presuming that the quantity_ori is a variable here 'coz you're trying to sum up with the original to the latest one
$sql .= "UPDATE product set quantity_bal= $quantity_ori + '".$_POST["quantity_in"][$index]."' WHERE id_produk = '".$_GET['id']."'";
}else if($quantity_bal != 0){
$sql .= "UPDATE product set quantity_bal= $quantity_bal + '".$_POST["quantity_in"][$index]."' WHERE id_produk = '".$_GET['id']."'";
}
}
我们将更新查询中的=
符号更改为.=
,然后您先前的quantity_ori
没有$
符号,因为我们认为它是一个变量,请不要忘记$