SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Insert Multiple Records to One Table Error

    How can I insert multiple rows of records to go to just the one table in a database? I understand I need to use 'foreach', so I created the below, but get the following error: Notice: Undefined index: office_date, office_ho in C:\htdocs\wvgymnastics\admin\info\office_info_insert.php on line 38

    Warning: Invalid argument supplied for foreach() in C:\htdocs\wvgymnastics\admin\info\office_info_insert.php on line 38

    Code PHP:
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      foreach($_POST['office_date, office_ho'] as $s){
      $insertSQL = sprintf("INSERT INTO office (office_date, office_ho) VALUES (%s, %s)",
                           GetSQLValueString($_POST['office_date'], "text"),
                           GetSQLValueString($_POST['office_ho'], "text"));
     
      mysql_select_db($database_wvgsadmin, $wvgsadmin);
      $Result1 = mysql_query($insertSQL, $wvgsadmin) or die(mysql_error());
     
      $insertGoTo = "office_info_list.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
    }

    Code HTML4Strict:
    <form method="post" name="form1" action="<?php echo $editFormAction; ?>">
    <table align="center" class="center" id="striped">
    <tr valign="baseline">
    <td nowrap align="right">Date:</td>
    <td><input type="text" name="office_date" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Hours:</td>
    <td><input type="text" name="office_ho" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Date:</td>
    <td><input type="text" name="office_date" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Hours:</td>
    <td><input type="text" name="office_ho" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Date:</td>
    <td><input type="text" name="office_date" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Hours:</td>
    <td><input type="text" name="office_ho" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Date:</td>
    <td><input type="text" name="office_date" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Hours:</td>
    <td><input type="text" name="office_ho" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Date:</td>
    <td><input type="text" name="office_date" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Hours:</td>
    <td><input type="text" name="office_ho" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">&nbsp;</td>
    <td><input type="submit" value="Insert record"></td>
    </tr>
    </table>
    <input type="hidden" name="MM_insert" value="form1">
    </form>

    Thank you!

  2. #2
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    $_POST['office_date, office_ho']... your index is invalid..
    can be either office_date or office_ho but not in the same time

  3. #3
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    foreach($_POST['office_date, office_ho'] as $s) is the problem. Your $_POST array doesn't have an index named office_date,office_ho. If you are looking to make an array using office_date and office_ho you can just do foreach(array($_POST['office_date'], $_POST['office_ho']) as $s)
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  4. #4
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Okay. What do you suggest?

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    simply do:

    foreach($_POST['office_date'] as $s){

    unless you have office_date's blank, but office_ho's with values ...

  6. #6
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toad78 View Post
    Okay. What do you suggest?
    We help you with syntax.. you go with the logic part

  7. #7
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'll need both office_date and office_ho.

  8. #8
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach(array($_POST['office_date'], $_POST['office_ho']) as $s
    >.>
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  9. #9
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I finally got it panned out, with your help!

    I love this place!
    toad78

  10. #10
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    i don't do php so i'm not sure i understood your script correctly, but it appears that you are generating a series of INSERT statements like this --

    INSERT INTO office (office_date, office_ho) VALUES ('2009-01-25', 123);
    INSERT INTO office (office_date, office_ho) VALUES ('2009-01-26', 456);
    INSERT INTO office (office_date, office_ho) VALUES ('2009-01-27', 937);

    it is substantially more efficient if you generate just one single INSERT statement like this --

    INSERT INTO office (office_date, office_ho) VALUES
    ('2009-01-25', 123)
    , ('2009-01-26', 456)
    , ('2009-01-27', 937);

    you just need to tweak your loop a little (although i cannot tell you exactly how)

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •