Reputation: 35
<?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 )");
}
}
?>
The problem here is the code execute every time when I refresh the page and insert loop only inserts one row not all row.
Upvotes: 1
Views: 56
Reputation: 37497
There's no need for first fetching the data from the database just to write it back again. You can just issue an INSERT ... SELECT ...
here.
INSERT INTO notification
(notification_name,
msg,
status,
notification_date)
SELECT item_name,
'Quntity is less than 10',
'unread',
current_time
FROM item
WHERE item_quantity < 10;
To always have accurate figures you can create a view, that computes the messages if necessary.
CREATE VIEW notification
AS
SELECT item_name,
'Quntity is less than 10',
'unread',
current_time
FROM item
WHERE item_quantity < 10;
Upvotes: 1