gpt4 book ai didi

PHP 为 foreach() 循环提供的参数无效

转载 作者:行者123 更新时间:2023-11-30 01:14:11 25 4
gpt4 key购买 nike

我试图确保数据库中的每一列都会从 HTML 中提取,但只有前两列会提取。最后三列不起作用。两列是文本值,最后一列是小数。有人可以告诉我我缺少什么吗?谢谢你!

$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>

最佳答案

只要$db->query只是一个常规的mysql_query()函数,就不能使用foreach。您应该使用:

while ($products = mysql_fetch_assoc($rSet)) {
$list .= $count.' '.$products['productID']
.' '.$products['categoryID']
.' '.$products['productCode']
.' '.$products['productName']
.' '.$products['listPrice']
.'<br>';
$count++;
}

关于PHP 为 foreach() 循环提供的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19215222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com