查询使用mysqli在php中无法正常工作

时间:2017-08-31 05:44:46

标签: php mysqli

 **DELETE.php**

<?php
include("db.php");
$id = $_GET['id'];
$check=mysqli_query($conn,"SELECT * FROM details WHERE id='$id'");
while($row = mysqli_fetch_array($check))
{
   $sub=$row['sub'];
   $quan=$row['quan'];
}
$check1=mysqli_query($conn,"SELECT * FROM instock WHERE s_brand=$sub");
while($row1 = mysqli_fetch_array($check1))
{
    $qty1=$row1['qty'];
}
$check2=mysqli_query($conn,"SELECT * FROM outstock WHERE s_brand=$sub");
while($row2 = mysqli_fetch_array($check2))
{
    $qty2=$row2['qty'];
}
$test2=mysqli_query($conn,"UPDATE instock SET qty =$qty1+$quan WHERE 
s_brand='$sub'");
$test3=mysqli_query($conn,"UPDATE  outstock SET qty =$quan-$qty2 WHERE 
s_brand='$sub'");
$result = mysqli_query($conn, "DELETE FROM details WHERE id=$id");
header("Location:add_sale.php");
?>

这是我的delete.php文件  删除查询工作正常。但是两个更新查询工作错误。它只是从$ quan设置了数量。       我需要马上       提前谢谢。

2 个答案:

答案 0 :(得分:2)

在你的第二个和第三个SELECT语句中,我认为你可能需要在$ sub附近添加单引号,所以它们将是:

$check1=mysqli_query($conn,"SELECT * FROM instock WHERE s_brand='$sub'");

$check2=mysqli_query($conn,"SELECT * FROM outstock WHERE s_brand='$sub'");

答案 1 :(得分:0)

通过加入查询,您可以在一个查询中完成所有更新,而无需任何SELECT

$update_stmt = mysqli_prepare($conn, "
    UPDATE details AS d
    JOIN outstock AS o ON o.brand = d.sub
    JOIN instock AS i ON i.brand = d.sub
    SET o.qty = o.qty - d.quan,
        i.qty = i.qty + d.quan
    WHERE d.id = ?") or die(mysqli_error($conn));
mysqli_stmt_bind_param($update_stmt, "i", $id);
mysqli_stmt_execute($update_stmt);
$delete_stmt = mysqli_prepare($conn, "DELETE FROM details WHERE id = ?");
mysqli_stmt_bind_param($delete_stmt, "i", $id);
mysqli_stmt_execute($delete_stmt);