我有一个包含 3 种类型字段的表单:
- (2) 提交给“tblCocktail”的文本字段
- (4) 从“tblIngredient”中选择填充值的字段并提交到“tblRecipe”
- (4) 选择提交给“tblRecipe”的带有预设选项的字段
表单代码(每个“选择红色”和“数量”下拉菜单有 4 个):
<form method="POST" action="addcocktail.php" >
Cocktail Name: <input type="text" name="cocktailname" />
How To: <input type="text" name="howto" />
<br>
<select id="selectingred1" name="selectingred1">
<?php
$sql = "SELECT ingredientID, name FROM tblIngredient ".
"ORDER BY name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "<option value=\"".$row['ingredientID']."\">".$row['name']."</option>\n ";
}
?>
</select>
<select id="quantity1" name="quantity1">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<br>
<input type="submit" value="add" />
</form>
addcocktail.php:
<?php include("databasecon.php"); ?>
<?php
mysql_select_db("mwheywood", $con);
//insert cocktail details
$sql="INSERT INTO tblCocktail (name, howto)
VALUES
('$_POST[cocktailname]','$_POST[howto]')";
$sql2="INSERT INTO tblRecipe (ingredientID, quantity)
VALUES
('$_POST[selectingred1]','$_POST[quantity1]'),
('$_POST[selectingred2]','$_POST[quantity2]'),
('$_POST[selectingred3]','$_POST[quantity3]'),
('$_POST[selectingred4]','$_POST[quantity4]')";
if (!mysql_query($sql,$con))
{
die('Error: you fail at life' . mysql_error());
}
echo "cocktail added";
if (!mysql_query($sql2,$con))
{
die('Error: you fail at life' . mysql_error());
}
echo "ingredients added";
mysql_close($con);
?>
目前这只是将“selectingred4”和“quantity4”值添加到“tblRecipe”中。它忽略了文本框的两个插入,以及前 3 个选择框条目。
我的另一个问题也是我从表单中的 php 中获取了“ingredientID”和“名称”,但是当我提交表单时,它也没有将“ingredientID”提交到“tblRecipe”中。
- 任何帮助将不胜感激 -Matt