【问题标题】:Query Not working correctly in php using mysqli使用 mysqli 在 php 中查询无法正常工作
【发布时间】:2017-08-31 05:44:46
【问题描述】:
 **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 设置 qty。 我需要立即 提前致谢。

【问题讨论】:

  • 为什么总是while语句?
  • 从其他表中检索数据
  • 我已经试过了@bharatparmar
  • 使用 1 次查询并参数化您的查询。
  • 尝试对变量执行算术运算,然后将其直接分配给更新中的数量

标签: php mysqli


【解决方案1】:

在您的第二个和第三个 SELECT 语句中,我认为您可能需要在 $sub 周围添加单引号,因此它们将是:

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

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

【讨论】:

    【解决方案2】:

    您可以通过加入查询在单个查询中完成所有更新,无需任何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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      相关资源
      最近更新 更多