mohammed mohammed - 1 year ago 65
PHP Question

Number 0 not work with if statement inside while loop php

i want check a quantities from all in table Pharmacy

if the quantity == 0 make update and change "Out Stock"

else the quantity will be change to "In Stock"

when get number 0 if statement not work.

this my php code:

$select="SELECT * FROM drugs WHERE quantity";
$resultt = mysqli_query($connect, $select);


while($row = $resultt->fetch_array(MYSQLI_ASSOC)){

if($Quantity == "0"){
$update="UPDATE drugs SET stock='Out Stock' WHERE id='$id'";
$result = mysqli_query($connect, $update);

$update="UPDATE drugs SET stock='In Stock' WHERE id='$id'";
$result = mysqli_query($connect, $update);





Answer Source

Don't bother.

You're setting one value based on another in the same row. There's no need to do that. The table design is just duplicating the existing data into two representations.

If the end goal is just to output stuff to users, do that in the view.

if ($row['quantity'] < 1) {
  echo "out of stock!";

If you really want to update the database, do it in two queries instead of 2 per individual row:

UPDATE drugs SET stock = 'Out of Stock' WHERE quantity = 0;
UPDATE drugs SET stock = 'In Stock' WHERE quantity > 0;

Assuming of course the stock field is CHAR or VARCHAR type.

But as I said, it's a bad design. You already have the information about whether something is in stock contained within the value of the quantity field, so use that where you need it instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download