0

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

2 回答 2

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++;
}
于 2013-10-06T23:42:12.870 回答
0

代替

foreach($rSet AS $products)

while ($products = mysql_fetch_assoc($rSet))

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

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

while ($products = $rSet->fetch(PDO::FETCH_ASSOC))
于 2013-10-06T23:43:54.050 回答