0

So here is my problem. I have a 2 tables. One for the Product details and One for the Product quantity.

In the product details there is a field Status it should be available or Not Available. Then in the Product quantity table there is a qtyleft meaning the quantity available for the product. Now the problem is i put 0 on quantity on 4 products meaning no quantity left and it should echo "Item is out of stock". It works but on 1 item only. I can't figure this out because I analyze everything and i think there is no problem. Is this a bug or what?

Here is my code:

<?php
if (isset($_GET['id']))
    {
        include('config.php');

        $id=$_GET['id'];
        $result = mysql_query("SELECT * FROM athan_products WHERE product_id = $id");
        while($row3 = mysql_fetch_array($result))
            {
                $resultq = mysql_query("SELECT * FROM inventory WHERE product_id LIKE '%".$id."%'");
                //$resultq = mysql_query("SELECT * FROM inventory WHERE product_id LIKE =$id");
                while($rows = mysql_fetch_array($resultq))
                    { 
                        $qwerty=$rows['qtyleft'];
                    }       
                if ($qwerty !=0){           
                    echo '<tr>';
                    //echo '<td>'.$row3['product_size_name'].'</td>';
                    echo '<td>'.$row3['price'].'</td>';
                    echo '<td>'.$row3['description'].'</td>';
                    echo '<td>'.'<input name="but" type="image" value="'.$row3['id'].'" src="images/button.png"  onclick="return myFunction()" />'.'</td>';
                    echo '</tr>';
                }
                else
                    {
                        echo '<tr>';
                        //echo '<td>'.$row3['product_size_name'].'</td>';
                        echo '<td align="center">'.'<h2>'.'Item is out of stock!'.'</td>';
                        echo '</tr>';
                        //echo '<td>'.'<h1>'.'"not available"'.'</h1>'.'</td>';
                    }
            }
    }
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
user3295525
  • 37
  • 1
  • 8
  • 4
    Too much of code.Can you just post the code relevant to your question ? – Rikesh Feb 11 '14 at 07:17
  • 4
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Feb 11 '14 at 07:18
  • Chances are it's a logic error but the relevant PHP is obscured in a lot of irrelevant markup. I did notice you're using mysql_* functions and are putting user input directly into queries though. The former is bad because mysql_* is deprecated, and the latter is bad because it opens you up to SQL injection attacks. – GordonM Feb 11 '14 at 07:19
  • Why are you using `LIKE` in the second query instead of `=`. – Barmar Feb 11 '14 at 07:21
  • i think you should be trying by changing name of input tag to array like this ` – krishna Feb 11 '14 at 07:24
  • Why do you have a `while` loop for the results of the first query? Can there be more than one product with the same ID? – Barmar Feb 11 '14 at 07:25
  • oh i see. i change the second query and it echo what i want but it puts error in line 12 16. thank you for telling me sir @Barmar – user3295525 Feb 11 '14 at 07:25
  • One product cannot have the same id sir – user3295525 Feb 11 '14 at 07:26
  • So why do you need a `while` loop? Also, can there be multiple `inventory` records with the same `product_id`? – Barmar Feb 11 '14 at 07:27

1 Answers1

0

I review your code, I think there is not any such error but you need to change you query.

$resultq = mysql_query("SELECT * FROM inventory WHERE product_id LIKE '%".$id."%'");

Into this which is given below.

$resultq = mysql_query("SELECT * FROM inventory WHERE product_id='".$id."'");

Hope you will solve your issue.

Thanks.