Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在IF语句中更新数据库_Php_Mysql_Sql_If Statement_Sql Update - Fatal编程技术网

Php 在IF语句中更新数据库

Php 在IF语句中更新数据库,php,mysql,sql,if-statement,sql-update,Php,Mysql,Sql,If Statement,Sql Update,我试图在IF语句中更新我的数据库,但它似乎不起作用。发送的电子邮件\u未更改为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']; $

我试图在IF语句中更新我的数据库,但它似乎不起作用。发送的电子邮件\u未更改为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' ");
}
$result2=mysql_查询(“从库存_控件中选择*,其中id='$id');
而($row1=mysql\u fetch\u数组($result2))
{
$item=$row1['item'];
$location=$row1['location'];
$quantity=$row1['quantity'];
$threshold=$row1['threshold'];
$emailSent=$row1['email_sent'];
}

如果($quantity您在一段时间内关闭循环的速度过快。您刚刚获得循环的最后一个值:

$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' ");
   }
}
$result2=mysql_查询(“从库存_控件中选择*,其中id='$id');
而($row1=mysql\u fetch\u数组($result2))
{
$item=$row1['item'];
$location=$row1['location'];
$quantity=$row1['quantity'];
$threshold=$row1['threshold'];
$emailSent=$row1['email_sent'];

如果($quantity检查
$n\u quantity
变量,它似乎没有正确定义。您是指
$quantity

您可能不需要while循环,因为您只处理表中的一行,即具有给定id的行。否则,如果您有更多行,则给定的while循环将提前关闭

SELECT*
可能会选择太多的列,这可能会对您不利。此外,不推荐使用msyql_查询,您需要使用mysqli_查询或PDO。因此,假设
$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' ");
   }
$result2=mysqli\u查询($con,“选择商品、位置、数量、阈值、从库存控制发送的电子邮件,其中id='$id');
列表($item、$location、$quantity、$threshold、$emailSent)=mysqli_fetch_数组($result2);

如果($quantity解析是与查询时间相关的,因此我需要关闭连接并在HTML之后打开一个新连接来查询数据库

非常感谢大家的评论,因为它们都非常有用,将来会对我有所帮助


:)

电子邮件发送是整数还是字符串?因为
'1'
是字符串。你应该只使用
1
@JuanCarlosOropeza这不是问题,MySQL在这种情况下会自动转换类型,你可能需要显示整个脚本以便更好地理解。例如,$n\u数量变量来自哪里?因此它会更新dat如果有意义的话,我让它准备并发送电子邮件,然后发送代码IF($emailSent==1){$message=introde(“\r\n“,$message”);mysql_query(“更新库存控制集email_sent=2,其中id='$id');}不,对不起,我解释错了,不是时间问题。你必须在while循环中更新数据库。while..{…}。我只是移动了while的结束以包含循环。