SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: Loop INSERT

  1. #26
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.....

  2. #27
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #28
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 )
    PHP Code:
    $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()); 

  4. #29
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #30
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ) )

  6. #31
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    So you made your select box look like i showed you in Post #19? (Hint: In post #20, you did it wrong.)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #32
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    <tr><td><[B]select name ="qnt[<?php echo $row_Recordset22['qnt'] ?>]" id="qnt" >
    <td><input type="text" id="prod_id" name="prod_id[<?php echo $row_Recordset22['prod_id'] ?>]" size="4"  readonly="readonly" value = "<?php echo $row_Recordset22['prod_id'] ?>"></td>
    I got rid of the cust to simplify.

    PHP Code:
    $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

  8. #33
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    As I said... go back and look at post #19.
    Quote Originally Posted by gwpaul View Post
    Form
    Code:
    <tr><td><[B]select name ="qnt[<?php echo $row_Recordset22['qnt'] ?>]" id="qnt" >
    <td><input type="text" id="prod_id" name="prod_id[<?php echo $row_Recordset22['prod_id'] ?>]" size="4"  readonly="readonly" value = "<?php echo $row_Recordset22['prod_id'] ?>"></td>
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #34
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand the difference, and I had it as you suggested.

    Code:
    <select name ="qnt[<?php echo $row_Recordset22['prod_id'] ?>]" 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.

  10. #35
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  11. #36
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  12. #37
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •