如何从<select>获取值并放入sql查询?

时间:2016-01-09 21:03:36

标签: php html mysql

我在获取select的值并将其查询到另一个数据时遇到问题。当我运行这个时,我收到了这个错误:   警告:第49行的C:\ wamp \ www \ pizza.php中为foreach()提供的参数无效 pizza.php  &lt; ul class =“products”&gt;     &LT; PHP     包括'core / admin / admin_db.php';     $ sql =“SELECT DISTINCT product_id,product_name,product_description,product_image FROM products WHERE pro_cat_id IN('105','106')GROUP BY product_name”;     echo“&lt; div class ='scroll2'&gt;”;     foreach($ conn-&gt; query($ sql)as $ row){         $ product_name = $ row ['product_name'];         echo“&lt; li&gt;”;         echo“&lt; form class ='form-item'&gt;”;         echo“&lt; div class ='grow2 pic'&gt;&lt; img src ='”。$ row ['product_image']。“'&gt;&lt; / div&gt;”;         echo“&lt; h5&gt;”。$ product_name。“&lt; / h5&gt;”;         echo“&lt; h6&gt;&lt; i&gt;”。$ row ['product_description']。“&lt; / i&gt;&lt; / h6&gt;&lt; / br&gt;”;         ?&GT;         &lt; select name =“product_size”id =“mySelect”onchange =“myFunction()”&gt;                &LT;选项&GT;                   选择大小                &LT; /选项&GT;                &LT; PHP                $ sql1 =“SELECT pro_size.pro_size_desc AS size,product_price,products.pro_size_id AS pro_size FROM products INNER JOIN pro_size ON products.pro_size_id = pro_size.pro_size_id WHERE product_name ='$ product_name'”;         foreach($ conn-&gt; query($ sql1)as $ row1){             echo“&lt; option value ='”。$ row1 ['pro_size']。“'&gt;”。$ row1 ['size']。“--- P”。$ row1 ['product_price']。“。00&lt。 ; /选项&gt;“中;         }                ?&GT;              &LT; /选择&GT;                 &LT; PHP             $ sample =“&lt; p id ='demo'&gt;&lt; / p&gt;”;             echo $ sample; ?&GT;     &LT;脚本&GT;     function myFunction(){         var x = document.getElementById(“mySelect”)。value;         document.getElementById(“demo”)。innerHTML =“你选择了:”+ x;     }     &LT; /脚本&GT;         &LT; PHP         $ sql6 =“SELECT product_id from products WHERE product_name ='$ product_name'AND pro_size_id ='$ sample'”;         foreach($ conn-&gt; query($ sql6)as $ row6){             echo“”。$ row6 ['product_id']。“”;         echo“&lt; input name ='product_id'type ='hidden'value ='{$ row6 ['product_id']}'&gt;”;         }         echo“&lt; button type ='submit'&gt;添加到购物车&lt; / button&gt;”;         echo“&lt; / form&gt;”;     echo“&lt; / li&gt;”;     ?&GT;     &LT; / UL&GT;

1 个答案:

答案 0 :(得分:0)

你应该阅读mysqli部分的php手册。

您尝试迭代一个对象。 $ CON组&GT;查询( “查询”); - 如果mysqli_result对象成功则返回。 (见这里:http://php.net/manual/de/class.mysqli-result.php

foreach ($conn->query($sql1) as $row1) {...}

正确的是:

$result = $conn->query("query");
if ($result) {
    while($row = $result->fetch_assoc()) {
        echo $row["product_id"];
    }
}