0

here's my code why is that the data for price is showing while the ID is not? Help please

<?php
include"Connection.php";
$dTime = time();
$myValue = $_REQUEST['dValue'];
echo "The time is: {$dTime}<br/>
The choice is {$myValue} ";

$sql = "Select * from product where NAME = '{$myValue}'";
 $result = mysql_query($sql);
 while ($row = mysql_fetch_assoc($result))

        $price = $row['PRICE'];
        $id = $row['ID'];
        echo "PHP $price";
        echo "$id";

$sql2 ="INSERT INTO `starbucks`.`order_details` (`ID`, `ORDER_ID`, `PRODUCT_ID`, `QTY`) VALUES ('2', '1', '$id', '1');";

$result2 = mysql_query($sql2);
?>
  • Try a `var_dump` on $row – Ed Heal Sep 11 '11 at 16:41
  • 1
    We can't answer this without seeing the structure of your 'product' table. Can you run `SHOW COLUMNS FROM PRODUCT` on your database? Edit: It's probably the curly brackets though rather than the data structure. – Gus Sep 11 '11 at 16:44
  • Hi there [Bobby Tables](http://xkcd.com/327/). Why on earth would you do `echo "$id";` and not `echo $id;`. Go learn some PHP basics, please. – Shef Sep 11 '11 at 16:46

3 Answers3

1

Do NOT forget the braces, otherwise only one line of code will execute.

while ($row = mysql_fetch_assoc($result)){
    $price = $row['PRICE'];
    $id = $row['ID'];
    echo 'PHP ', $price; // do not use variable concatenation or double quotes if you can
    echo $id; // no need to wrap a variable with quotes for printing it
}

Do NOT forget to escape user input, otherwise you will be prone to SQL injection and XSS attacks.

$myValue = mysql_real_escape_string($_REQUEST['dValue']);
Community
  • 1
  • 1
Shef
  • 44,808
  • 15
  • 79
  • 90
1

You don't have any curly brackets delineating your while loop, so it is only running the line $price = $row['PRICE']; for each row. Try:

while ($row = mysql_fetch_assoc($result))
{
        $price = $row['PRICE'];
        $id = $row['ID'];
        echo "PHP $price";
        echo "$id";
}
Gus
  • 7,289
  • 2
  • 26
  • 23
0

Why you are not using curly braces for while loop

while ($row = mysql_fetch_assoc($result))

it will only execute 1 line after this not the second one

Astha
  • 1,728
  • 5
  • 17
  • 36