I have the following code:
for ($i=1; $i<=10; $i++)
{
echo " <input type=text name=name=amount size=5 /> “;
echo " <select name=measurement>”;
$measurement = mysql_query(“SELECT * FROM fit_ingredient_measurements”);
while($meas = mysql_fetch_array($measurement))
{
echo “<option value=” . $meas[‘id_measurement_type’] . "> " . $meas[‘measurement_type’] . “</option>”;
}
echo "</select> ";
echo " <input type=text name=name=ingredient /> “;
echo " <select name=grocerycat>”;
$grocery_cat = mysql_query(“SELECT * FROM fit_grocery_cat”);
while($cat = mysql_fetch_array($grocery_cat))
{
echo “<option value=” . $cat[‘id_grocery_cat’] . "> " . $cat[‘grocery_cat_name’] . “</option>”;
}
echo “</select><br><br>”;
}
I’m needing to send the results from this 4 fields to 2 separate tables and link them by the same ID.
Here is how my tables are setup:
Table 1:
id_ingredient
ingredient_name
id_grocery_cat
Table 2:
id_integredient
measurement_amount
measurement_type
Any ideas?
I’m trying to use the following code to submit to database but it is not submitting to database at all.
$ingredient = !empty($_POST[‘ingredient’]) ? $_POST[‘ingredient’] : array();
$measurement = !empty($_POST[‘measurement’]) ? $_POST[‘measurement’] : array();
$grocerycat = !empty($_POST[‘grocerycat’]) ? $_POST[‘grocerycat’] : array();
$amount = !empty($_POST[‘amount’]) ? $_POST[‘amount’] : array();
foreach($ingredient as $ing) {
mysql_query("INSERT INTO ingredients (ingredient_name, id_grocery_cat, measurement_amount, measurement_type) VALUES ('$ing', '$grocerycat[$i]', '$amount[$i]', '$measurement[$i]')");
mysql_query("INSERT INTO recipe_ingredients (id_recipe, id_ingredient) VALUES ('$just_added_id', '$ing')");
}
I think
name=name=
in a couple places is messing up your form.
an I think you’re missing
$just_added_id = mysql_insert_id();
or something similar in there.
I have fixed those issues but it still is not submitting. It’s not outputting any errors either.
What I’d do is make sure you’re getting values. You could
var_dump($_POST);
to see everything there, or just
var_dump($ingredient);
before the loop. Sounds like it’s empty or it isn’t getting to that code at all.
I did a var dump and it is displaying all of the data so it doesn’t appear to be coming in empty. Weird :\
Are you connecting to the database before that piece of code? Might want to make sure error reporting is on when developing. Put this at the top of your code.
ini_set( 'display_errors', 1 );
error_reporting( E_ALL | E_STRICT );
If the errors don’t give you a clue, I’d try a db select statement somewhere just to see if it’s really “seeing” the database.
It’s not reporting any errors. Other select statements in the page are working. Very odd.
Your first INSERT needs to be two separate queries if your tables are as you stated. (sorry, I saw 2 tables and 2 inserts and I didn’t look close enough).
You have “INSERT INTO ingredients” but then have the fields of another table in there too.
var_dump( mysql_error() );
after that statement should show you.
I see now what you’re trying to do. I think the main problem is that’s not really the best way to go. You really have three tables, right? recipe_ingredients should have the ingredient_id and a measurement_id (and the measurement table shouldn’t have ingredient_id). That way you’d have and ingredient, a measurement, and a recipe that knows which of those to use.