【发布时间】:2020-03-07 09:59:58
【问题描述】:
<?php
require 'database.php';
$msg= "Quntity is less than 10";
$numResults = mysqli_query($conn, "select COUNT(*) as `count` from item where item_quantity <10");
$row = mysqli_fetch_array($numResults);
$count = $row['count'];
if ($count>0) {
$query = mysqli_query($conn, "select item_name from item where item_quantity <10");
while ($notify = mysqli_fetch_assoc($query)) {
$td2 = $notify['item_name'];
$query = mysqli_query($conn, "insert into notification (notification_name ,msg , status , notification_date)
VALUES('$td2','$msg','unread' , current_time )");
}
}
?>
这里的问题是每次刷新页面时执行的代码,插入循环只插入一行而不是所有行。
【问题讨论】:
-
在你的循环中你重新使用
$query变量,它也是SELECT。您的代码可以对使用准备好的语句进行一些更改,删除不必要的第一个SELECT并将INSERT转换为INSERT...SELECT...(对不起,我刚刚离开,但这条评论可能会有所帮助)。