I want to create stock management system in my website using php. It sounds like 'balancing' stock in database with amount of items that will purchased. If the item's amount is more than the item's stock, the website should displaying an alert. So, the amount of item cannot be more than the item's stock in database. Here's my code :
$stock = array();
foreach ($_SESSION['cart'] as $k => $v) {
$sql = mysql_query("SELECT book_stock FROM book WHERE book_title='$k'");
while ($row = mysql_fetch_assoc($sql)) {
$stock[] = $row['book_stock'];
}
if ($stock < $v) {
echo "Stock less than the amount";
}
else {
echo "Stock sufficient";
}
}
The $k
variable store the item's name in the session cart. And the $v
variable store the item's amount in the transaction.
The item's amount will be increased if the visitor click 'Increase' button.
But if i increase the item's amount, the if
and else
logic like "isn't working". No matter how much the item's amount is added, the if
and else
logic keep print "Stock Sufficient".
Can anyone help me to solve this? Thanks in advance :)
Edit : The variable in original code is in indonesian. So when i'm typing this post, i'm editing the variables too here. So if the variables is different, i'm just forget to rename it. So i edit the script again :)