简体   繁体   English

在IF语句中更新数据库

[英]Updating database in an IF Statement

I am trying to update my database inside an IF statement but it doesn't seem to be working. 我正在尝试在IF语句中更新数据库,但似乎无法正常工作。 email_sent doesn't change to 1. Is my statement correct? email_sent不会更改为1。我的陈述正确吗?

$result2 = mysql_query ("SELECT * FROM stock_control WHERE id = '$id' ");

while ($row1 = mysql_fetch_array($result2))
{
$item=$row1['item'];
$location=$row1['location'];
$quantity=$row1['quantity'];
$threshold=$row1['threshold'];
$emailSent=$row1['email_sent'];
}


if ($quantity <= $threshold && $emailSent == 0) {
mysql_query("UPDATE stock_control SET quantity=quantity - '$n_quantity', 
email_sent = '1' WHERE id = '$id' ");
} else {
mysql_query("UPDATE stock_control SET quantity=quantity - '$n_quantity' WHERE 
id = '$id' ");
}

You are closing your loop too fast for the while. 您暂时关闭循环的速度过快。 You are just getting the last value of the loop: 您只是获得循环的最后一个值:

$result2 = mysql_query ("SELECT * FROM stock_control WHERE id = '$id' ");

while ($row1 = mysql_fetch_array($result2))
{
   $item=$row1['item'];
   $location=$row1['location'];
   $quantity=$row1['quantity'];
   $threshold=$row1['threshold'];
   $emailSent=$row1['email_sent'];

   if ($quantity <= $threshold && $emailSent == 0) {
      mysql_query("UPDATE stock_control SET quantity=quantity - '$n_quantity', 
      email_sent = '1' WHERE id = '$id' ");
   } else {
      mysql_query("UPDATE stock_control SET quantity=quantity - '$n_quantity' WHERE 
      id = '$id' ");
   }
}

Check on the $n_quantity variable, it does not seem to be properly defined. 检查$n_quantity变量,它似乎没有正确定义。 Did you mean $quantity instead? 您是说$quantity吗?

You may not need the while loop, since you are dealing with only one row of the table, the row with the given id. 您可能不需要while循环,因为您只处理表的一行,即具有给定id的行。 Otherwise, if you had more rows, the while loop as given would have been prematurely closed. 否则,如果您有更多行,则给定的while循环将过早关闭。

SELECT * could select too many columns, which might be disadvantageous. SELECT *可能选择太多列,这可能是不利的。 Also, the use of msyql_query is deprecated, you will need to use mysqli_query or PDO. 另外,不建议使用msyql_query,则需要使用mysqli_query或PDO。 So the following might be helpful, assuming that $con is your database connection: 因此,假设$con是您的数据库连接,以下内容可能会有所帮助:

$result2 = mysqli_query($con,"SELECT item, location, quantity, threshold, email_sent from stock_control where id = '$id'");

list($item,$location,$quantity,$threshold,$emailSent) = mysqli_fetch_array($result2);

if ($quantity <= $threshold && $emailSent == 0) {
      mysqli_query($con,"UPDATE stock_control SET quantity=quantity - '$quantity', 
      email_sent = '1' WHERE id = '$id' ");
   } else {
      mysqli_query($con,"UPDATE stock_control SET quantity=quantity - '$quantity' WHERE id = '$id' ");
   }

The resolution was time related to the query and therefore I needed to close the connection and open a new connection after my HTML to query the DB. 解决方案的时间与查询有关,因此我需要在HTML之后关闭连接并打开新连接以查询数据库。

Many thanks to all for your comments as they were all very useful and will help me in the future. 非常感谢您的意见,因为它们非常有用,将来会对我有帮助。

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM