Loop INSERT

Mmh no.

Do it all in one insert query. You have it almost perfect in your second codeblock, just need a very slight variable change…
Notice that the keys of the $_POST[‘qnt’] array are already your product ID’s. You dont need to send them seperately/insert them seperately.

(Hint: [FPHP]foreach[/FPHP] syntax might help you understand)

I’ve tried every iteration I can think of, swapped the prod_id with qnt, I’ve tried adding multiple foreach loops. Worse is that even when I have one working, either the prod_id or the qnt, it enters double into the table.

I cant get it to work.


$query_Recordset3 = "INSERT INTO temporder ( cust, qnt, prod_id) VALUES ";
foreach($_POST['qnt'] AS [COLOR="#FF0000"]$id[/COLOR] => $qnt) {
 //Sanitize both $id and $qty here     
      $query_Recordset3 .= "('".$cust."','".$qnt."','".[COLOR="#FF0000"]$prod_id[/COLOR]."'),"; 
}

$query_Recordset3 = substr($query_Recordset3,0,-1); // get rid of trailing comma, since we cant use implode  
$Recordset3 = mysql_query($query_Recordset3, $szabo) or die(mysql_error());  

sings one of these things just doesnt belong here, one of these things just isnt the same…

Thanks…i used to like that song.

I get to sing that one around here quite a bit :wink:

Hope that solved your problem.

No, sadly it has not. I took your specific hint and removed the $id. the foreach loop still works, but does not enter the prod_id. I have tried numerous variations based on your hint, but none have worked. Still getting the double entry into the DB, several of the edits got me 18 inserts on one submit (3 qnt inputs, I’m assuming 3x3x2, but I don’t understand how). One variation got me prod_id to insert 0,1,2…when there are no 0,1,2. Other variations have jumbled the qnt with the prod_id…

print_r $_POST[‘qnt’];

will output the contents of the qnt array. Please paste that here so i can see what’s coming through, so I can give you a specific answer.

I input 1,2,3 into the form, result was

Array ( [0] => 1 [1] => 2 [2] => 3 )

I also print’d the prod_id

Array ( [0] => Array )


$prod_id=array("$_POST[prod_id]");
print_r ($prod_id);
///////////////////////////////////From Forum////////////////////////////////////////////

//Assumes $cust predefined (Session Value?)
$query_Recordset3 = "INSERT INTO temporder ( qnt, prod_id) VALUES ";
foreach($_POST['qnt'] AS   $value ) {

 //Sanitize both $id and $qty here
      $query_Recordset3 .= "('".$qnt."','".$prod_id."'),";
}

$query_Recordset3 = substr($query_Recordset3,0,-1); // get rid of trailing comma, since we cant use implode
$Recordset3 = mysql_query($query_Recordset3, $szabo) or die(mysql_error());


there shouldnt have been anything in a ‘prod_id’ array.

You’ve either not filled in the form fields as i showed you, or the values you’re retrieving for those field-keys are 0,1 and 2.

Oops, made a mistake on the prod_id print_r, results are 3,4,14 were posted.

Array ( [0] => Array ( [3] => 3 [4] => 4 [14] => 14 ) )

So you made your select box look like i showed you in Post #19? (Hint: In post #20, you did it wrong.)

qnt array, entered 1,2,3

Array ( [0] => 1 [1] => 2 [2] => 3 )

prod_id, should be 3,4,14

Array ( [0] => Array ( [3] => 3 [4] => 4 [14] => 14 ) )

Form


<tr><td><[B]select name ="qnt[<?php echo $row_Recordset22['qnt'] ?>]" id="qnt" >
<td>[B]<input type="text" id="prod_id" name="prod_id[<?php echo $row_Recordset22['prod_id'] ?>]" [/B]size="4"  readonly="readonly" value = "<?php echo $row_Recordset22['prod_id'] ?>"></td>

I got rid of the cust to simplify.


$query_Recordset3 = "INSERT INTO temporder ( qnt, prod_id) VALUES ";
foreach($_POST['qnt'] AS   $qnt ) {

 //Sanitize both $id and $qty here     
      $query_Recordset3 .= "('".$qnt."','".$prod_id."'),"; 
}

$query_Recordset3 = substr($query_Recordset3,0,-1); // get rid of trailing comma, since we cant use implode  
$Recordset3 = mysql_query($query_Recordset3, $szabo) or die(mysql_error()); 


Results 1,2,3 qnt, // o,o,o for prod_id, but inserts 6 rows into DB 1,2,3 1,2,3

As I said… go back and look at post #19.

I understand the difference, and I had it as you suggested.


<select name ="qnt[<?php echo $row_Recordset22['[COLOR="#FF0000"]prod_id[/COLOR]'] ?>]" id="qnt" >

However the result was the prod_id was entered into the qnt col.

I then changed the prod_id to qnt in the form input name=“qnt”, and the qnt was correct.

I then keyed in the prod_id on the form to create the array.

I can get either to work, not both at the same time.

Am I missing something?

Again, I appreciate your patience.

The difference is between the KEY, and the VALUE.
As i said before… reading the FOREACH syntax would be helpful. As that seems to have either passed over your head or you didnt bother to read it, i’ll spell it out for you.

foreach($_POST[‘qnt’] AS $prod_id => $qnt)

For each entry in the array $_POST[‘qnt’], take the Key and stick it into $prod_id, and take the Value and stick it into $qnt, then run the content of the loop.

<select name =“qnt[<?php echo $row_Recordset22[‘prod_id’] ?>]” id=“qnt” >

Which generates the HTML (using prod_id 14 as example):
<select name =“qnt[14]” id=“qnt” >

This means when you get to your form handler, there will be a Key of 14, with Value <whatever they chose in the select box>. These will be in $prod_id and $qnt respectively. Use these to fill in your query.

Sorry for the delay in response, unfortunately I was not getting it, I assumed when it did not work the first time you had typo’d prod_id for qnt. Anyway, thank you for your patience and help, it is now working.

Gary

How do I add additional values to the loop? In addition to the qnt and prod_id, I have ptype, model, model_num, msrp. I have tried to put them into an array for the value but that does not work.

Thanks again

Gary