<form method="post" action="insert.php" >
<p>Name:<input type="text" name="food_name" id="food_name"/>
<p>Price:<input type="text" name="food_price" id="food_price"/>
<?php
$query = "SELECT * FROM category";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());
//$options = "";
$dropdown = "<select name='cat_name'>";
while($row = mysql_fetch_assoc($result))
{
$dropdown .= "\r\n<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
<input type="submit" value="Insert Price" />
<a href="show.php">Cancel
</form>
//这是从下拉菜单和文本框中获取数据的表单
X=======================X
if(isset($_POST['cat_name'])!= '')
{
$cat_id=$_POST['cat_name'];
$food_name=$_POST['food_name'];
$foodPrice=$_POST['food_price'];
$query="INSERT INTO food (food_name,food_price) values( $food_name,$foodPrice) where
cat_id= $cat_id";
if(mysql_query($query))
{
echo "new item added";
}
else
{
die(mysql_error());}
}
?>
这是错误“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'where cat_id= 4' 附近使用正确的语法”
它应该通过选择类别来插入食物名称和价格