Php 提交表单后,获取另一个表中条目的ID
我当前的表单将数据提交到两个不同的表中,我希望一个表中的自动递增值也存储在第二个表中Php 提交表单后,获取另一个表中条目的ID,php,mysql,forms,Php,Mysql,Forms,我当前的表单将数据提交到两个不同的表中,我希望一个表中的自动递增值也存储在第二个表中 <form method="POST" action="addcocktail.php" > Cocktail Name: <input type="text" name="cocktailname" /> How To: <input type="text" name="howto" />
<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>
鸡尾酒名称:
如何:
1.
2.
3.
4.
addcockbox.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);
?>
执行insert查询后,如果函数成功,您可以获得insert id。hmm我不太确定它如何知道在insert语句之后为每个选择框条目插入Cocktaild?仔细想想,您会发现。你有所有的功能,只剩下把它们放在一起了。