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

时间:2013-10-06 23:38:12

标签: php html mysql

我正在尝试确保数据库中的每一列都将从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>

2 个答案:

答案 0 :(得分:0)

只要$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++;
}

答案 1 :(得分:0)

替换

foreach($rSet AS $products)

使用

while ($products = mysql_fetch_assoc($rSet))

不完全确定您正在使用的db-library。它看起来不像ext / mysql。

如果您使用的是PDO,则该行应为

while ($products = $rSet->fetch(PDO::FETCH_ASSOC))