0

I'm using a jQuery plugin to dynamically add input text fields. The name of the input elements becomes name="cl[]", name="ingredient[]".

Now I need to submit these fields to a database somehow...
If there was only one text field that should be added, I guess I could just do a simple foreach-loop like this:

foreach($ingredient as $val){
    //  do an ordinary PDO sql insert statement on each of them
}

But I need is to submit two text fields, and the last inserted ID from a previous query, on each of them. If I added two fields, I would have three in total to be submitted; Like this:

<input type="text" name="oz[]" id="cl_1" placeholder="cl" class="cl" >
<input type="text" name="ingredient[]" id="ingredient_1" placeholder="ingredient name" class="ingredient" />

<input type="text" name="oz[]" id="cl_2" placeholder="cl" class="cl" >
<input type="text" name="ingredient[]" id="ingredient_2" placeholder="ingredient name" class="ingredient" />

<input type="text" name="oz[]" id="cl_3" placeholder="cl" class="cl" >
<input type="text" name="ingredient[]" id="ingredient_3" placeholder="ingredient name" class="ingredient" />

These fields are ingredients for drinks that is inserted into a sepparate table, and the last id is the relations key.

Any suggestion on how I can accomplish this?

UPDATE: I just tried to add a hidden textfield with the name raw_materials[] and do a ẁhile`-loop on that one.
Then do a sql insert statement inside this while loop. Maybe I'm on to something, but this didn't work:

while($raw_materials){
    $ins_ingredients = $con->prepare(
        'INSERT INTO recipes_ingredients (
            recipe_id, raw_material_id, amount
        ) VALUES (
            :last_id, :material, :amount
        )'
    );
    $ins_ingredients->bindValues(':last_id',$last_id);
    $ins_ingredients->bindValues(':material',$ingredient);
    $ins_ingredients->bindValues(':amount',$oz);
    $ins_ingredients->execute();
}

2 Answers 2

2

Check out this:

$values = array_map(null, $_POST['oz'], $_POST['ingredient']);  // You can add other arrays after this
foreach ($values as $value)
{
    list($oz, $ingredient) = $value;
    // Insert data in DB
}
3
  • That should do the trick. I'm not able to test it out right now. But I'll test it when I get home...
    – ThomasK
    Aug 24, 2012 at 8:39
  • I have two tables. One for recipes with some how-to descriptions. And another table with all the raw materials needed by that recipe. I insert one record into the recipe table, which has a auto-increment, which I use to create the relation for each raw material in the other one...
    – ThomasK
    Aug 24, 2012 at 19:13
  • Ok then everything is fine. :)
    – Savageman
    Aug 26, 2012 at 17:12
1

The cl and ingredient field in one group have the same number, so instead of a foreach loop you should use a for loop:

for($x=0, $x<count($_POST['cl']); $x++) {
    // now you can use $_POST['cl'][$x] and $_POST['ingredients'][$x]
}
1
  • I'm kind of stuck here. Do I first need to do an insert with the $last_id, then update it in order to add the cl (oz) and ingredient?
    – ThomasK
    Aug 23, 2012 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.