2011-09-11 75 views
0

這是我的代碼爲什麼價格數據顯示,而ID不是?請幫助數據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); 
?> 
+0

嘗試在$行 –

+1

一個'var_dump'沒有看到「產品」表的結構,我們無法回答。你可以在你的數據庫上運行SHOW COLUMNS FROM PRODUCT嗎?編輯:它可能是大括號,而不是數據結構。 – Gus

+0

你好[Bobby Tables](http://xkcd.com/327/)。爲什麼你會'echo'$ id「;'而不是'echo $ id;'。請學習一些PHP基礎知識。 – Shef

回答

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

您沒有任何大括號劃定while循環,所以它只能運行每一行的行$price = $row['PRICE'];。嘗試:

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

爲什麼不使用大括號的while循環

while ($row = mysql_fetch_assoc($result)) 

它只會在此之後執行1線沒有第二個