I have a form that has 3 types of field:
- (2) text fields that submit to “tblCocktail”
- (4) select fields that are populated with values from “tblIngredient”
and submit to “tblRecipe” - (4) select fields with pre-set options that submit to “tblRecipe”
Form Code (There are 4x of each “selectingred” & “quantity” drop-downs):
<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);
?>
This is currently only adding the “selectingred4” and “quantity4” values into “tblRecipe”. It’s ignoring the two inserts for the text boxes, and the first 3 select box entries.
My other problem was also that Im grabbing the “ingredientID” and “name” from the php in my form, but when I submit the form it’s not submitting “ingredientID” into “tblRecipe” either.
-any help would be appreciated -Matt
You keep overwriting your SQL query and only the last one actually exists to be executed. Here’s a fixed version of your code:
Furthermore, you can combine the last four queries into one query: