我試圖確保數據庫中的每一列都會從HTML中拉出來,但只有前兩列會拉出來。最後三列不起作用。兩個是文本值,最後一列是小數。有人能告訴我我錯過了什麼嗎?謝謝!PHP爲foreach()循環提供的參數無效
$colmName = $_POST["colName"];
$colmValue = $_POST["colValue"];
$count = 0;
if ($colmName = 'productID')
{
$theQuery = "SELECT * FROM products
WHERE productID = $colmValue";
$rSet = $db->query ($theQuery);
}
elseif ($colmName = 'categoryID')
{
$theQuery = "SELECT * FROM products
WHERE categoryID = $colmValue";
$rSet = $db->query($theQuery);
}
elseif ($colmName = 'productCode')
{
$theQuery = "SELECT * FROM products
WHERE (productCode = '$colmValue')";
$rSet= $db->query($theQuery);
}
elseif ($colName = 'productName')
{
$theQuery = "SELECT * FROM products
WHERE (productName = '$colmValue')";
$rSet = $db->query ($theQuery);
}
elseif ($colName = 'listPrice')
{
$theQuer = "SELECT * FROM products
WHERE listPrice = $colmValue";
$rSet = $db->query ($theQuery);
}
else
{
echo ('Enter a valid column name from the products table and an existing value. Either productID, categoryID, productCode, productName, or listPrice.');
include (index.html);
exit();
}//end if
foreach($rSet AS $products)
{
$list .= $count.' '.$products['productID']
.' '.$products['categoryID']
.' '.$products['productCode']
.' '.$products['productName']
.' '.$products['listPrice']
.'<br>';
$count++;
}//foreach
echo ("<p>The data for $colmName with a value of $colmValue is listed below:<p>");
// echo ($list);//just to check if it works before putting it in a table
// echo ('<p>');//for spacing
?>
<DOCTYPE! html>
<html>
<head>
<title>Product Results</title>
</head>
<body>
<section>
<table border="1">
<tr><th>productID</th><th>categoryID</th><th>productCode</th><th>productName</th><th>listPrice</th></tr>
<tr><td><?php echo $products['productID'];?></td><td><?php echo $products['categoryID'];?></td><td><?php echo $products['productCode'];$
</table>
</section>
</body>
</html>
您運行的是什麼版本的PHP? 'foreach'只能用於> = 5.4版本的查詢結果。 http://www.php.net/manual/en/class.mysqli-result.php – Anthony
是的我現在正在使用最新的版本(用於學習目的)。謝謝大家的建議。其實問題是「=」與「==」。顯然PHP並不認爲它有條件,即使認爲它是一個「如果」的條件開始。所以我不得不將它改爲==,然後才能工作。 – user2852792