数据ID未显示为什么?

时间:2011-09-11 16:38:18

标签: php mysql database arrays

这是我的代码为什么价格数据显示而ID不是?请帮忙

<?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);
?>

3 个答案:

答案 0 :(得分:1)

不要忘记大括号,否则只会执行一行代码。

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
}

不要忘记escape user input,否则您会受到SQL injectionXSS攻击。

$myValue = mysql_real_escape_string($_REQUEST['dValue']);

答案 1 :(得分:1)

你没有任何大括号描述你的while循环,所以它只为每一行运行$price = $row['PRICE'];行。尝试:

while ($row = mysql_fetch_assoc($result))
{
        $price = $row['PRICE'];
        $id = $row['ID'];
        echo "PHP $price";
        echo "$id";
}

答案 2 :(得分:0)

为什么你没有使用花括号进行while循环

while ($row = mysql_fetch_assoc($result))

它只会执行1行而不是第二行