0

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 :)

Dhimas Yoga
  • 87
  • 10
  • 4
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Dec 20 '16 at 09:48
  • 1
    `$stok` you made an array. So what is `if ($stok < $v) {` supposed to be doing – RiggsFolly Dec 20 '16 at 09:51
  • I assume there is only one book with a specific title, so why the loop to read the result set – RiggsFolly Dec 20 '16 at 09:54
  • Variables in my original code is in indonesian. So when i'm posting it, i edit the variable here. So the original of the 'stock' is 'stok'. I'm just forgot to edit it when i post this question – Dhimas Yoga Dec 20 '16 at 09:58
  • As RiggsFolly mentioned, in the `if ($stok < $v)` area, you're telling PHP: `if (Array() < $v)`. As they mentioned, assuming you're having unique book titles, it doesn't need to be an array, but as-is, you could try `if ($stok[0] < $v)` just to test it, quick. Also, just to make sure, I'm assuming all of this code is being reprocessed as someone clicks to increment/decrement their quantity, elsewhere in your code, correct? – clicheName Dec 20 '16 at 09:59
  • and i'm using mysql_ because my php version is 5.*.*. mysql_ extension works well in my code – Dhimas Yoga Dec 20 '16 at 10:01
  • @clicheName so if i write it like this : $stok[$k], does it works well like $stok[0] do? – Dhimas Yoga Dec 20 '16 at 10:08
  • From the looks of it, no. Your stock() array is indexed, not associative. `$stock[] = $row['book_stock'];` basically means "Add to the stock() array, giving a value of whatever this row's 'book_stock' value is". How you have your code set up right now, it's assuming that no two books have the exact same titles (if you do have more than one book with the same title, this will cause conflicts, as your code is, right now). – clicheName Dec 20 '16 at 10:26
  • But if you want to leave it this way, make `$stock = array();` into `$stock = 0;`, then instead of `$stock[] = $row['book_stock'];`, use `$stock = $row['book_stock'];` (Just remember to rename it back to 'stok' if you're using that spelling). – clicheName Dec 20 '16 at 10:28
  • @clicheName Thanks for your answer, i'll try your code after i get home. Thanks for your help :D – Dhimas Yoga Dec 20 '16 at 10:36
  • Yup, no problem. And actually, disregard that last comment I made. Instead, if you want to keep your array, we can just make it an associative array, like you asked. Change `$stock[] = $row['book_stock'];` to `$stock[$k] = $row['book_stock']` and `if ($stock < $v) {` to `if ($stock[$k] < $v) {` – clicheName Dec 20 '16 at 10:38
  • (Forgot to say, if you change the array into an int, you'll have to move $stock = 0 into the loop instead of outside of it). If you decide to do it as an array, keep in mind that using the titles as the 'key' in the arrays isn't really a good idea, since punctuation will mess it up (using an ID instead of a book title would work, though). Sorry for the convoluted replies. – clicheName Dec 20 '16 at 10:45
  • @clicheName Hey dude, your suggestion (that told me to change the $stok = array(); to an integer using $stok = 0) is work for me. Thanks for your help so far. Thank you so much :D – Dhimas Yoga Dec 20 '16 at 12:54
  • @clicheName and as an additional, i declare `$stock = 0` outside the `foreach` loop. And i use `if($stock >= $v)` instead of `if($stock > 0)` becasuse it's fine as long as the item's amount is **not more than** the stock in the database. Once again, thanks for your help in advance so far. I really appreciate it :D – Dhimas Yoga Dec 20 '16 at 13:01

1 Answers1

0
$sql = mysql_query("SELECT book_stock FROM ooku WHERE book_title='$k'");

I am assuming that your query will return unique book (single book) stock detail, you have to use $stok instead of $stok[] Array

while ($row = mysql_fetch_assoc($sql)) {
        $stok = $row['book_stock'];
    }
Amit Kumar Sahu
  • 495
  • 6
  • 15
  • I've renamed the 'ooku' to 'book'. I'm just forget to rename the old variable name (from the original code, which the variable name is in indonesian) to new variable name. So could you kindly please check my updated question above? Thanks in advance :) – Dhimas Yoga Dec 20 '16 at 10:13