simultaneous inserting of row in a databse

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • backups2007
    New Member
    • Jul 2007
    • 92

    simultaneous inserting of row in a databse

    is it possible to insert, say for example, 5 rows of records in a table?

    is this possible?
    [code=php]
    $query1 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");

    $query2 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");

    $query3 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");

    $query4 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");

    $query5 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");
    [/code]
    Last edited by Atli; Oct 4 '07, 01:58 AM. Reason: Added [code] tags.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I think I understand your problem.
    Well to start, this is the SQL for multiple record INSERT
    Code:
    INSERT INTO tbl_items (item_code, description) VALUES
    ('$item_code1', '$description1'),
    ('$item_code2', '$description2'),
    ('$item_code3', '$description3'),
    ('$item_code4', '$description4')
    But I understand that $item_code', '$description' variables are changing within your code, but you can approach this in a variety ways.
    Build up and create the query within a loop by concatenating strings and variables.
    Make $item_code and $description arrays then either extract and concatenate as above or
    explode the arrays into the VALUES part of the query.
    The best method will depend on the structure of your code so far, but I hope this gives you something to think about,

    Comment

    • backups2007
      New Member
      • Jul 2007
      • 92

      #3
      ok. thanks for the tip.

      could I also use this code?
      [code=php]
      $query1 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");

      if(query1)
      {
      $query2 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");
      if(query2)
      {
      $query3 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");
      if(query3)
      {
      $query4 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");
      if(query4)
      {
      $query5 = mysql_query("IN SERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description') ");
      if(query5)
      {
      echo "insert success";
      }
      }
      }
      }
      }
      [/code]
      Or will this code even work?
      Last edited by Atli; Oct 4 '07, 01:58 AM. Reason: Added [code] tags.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        Please enclose your posted code in [code] tags (See How to Ask a Question).

        This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

        Please use [code] tags in future.

        Moderator

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Both of the code examples you have posted will work. But they are a little to complicated for what you are trying to do.

          As code green said, you can insert multiple rows in a single INSERT statement.

          This would do exactly the same thing both your previous code examples do, this would probably be a little faster though:
          [code=php]
          $query = "
          INSERT INTO tbl_items (item_code, description) VALUES
          ('$item_code', '$description') ,
          ('$item_code', '$description') ,
          ('$item_code', '$description') ,
          ('$item_code', '$description') ,
          ('$item_code', '$description') ";

          $result = mysql_query($qu ery);
          [/code]

          Comment

          • backups2007
            New Member
            • Jul 2007
            • 92

            #6
            what if right after inserting the data, I want to get the item code and pass it to another table. say for example,

            [CODE=php]$query = "
            INSERT INTO tbl_items (item_code, description) VALUES
            ('$item_code', '$description') ,
            ('$item_code', '$description') ,
            ('$item_code', '$description') ,
            ('$item_code', '$description') ,
            ('$item_code', '$description') ";

            $result = mysql_query($qu ery);

            if($result){
            mysql_query(INS ERT INTO inventory (item_code) VALUES ("$item_code")) ";
            }
            [/CODE]
            is this possible?
            Last edited by Atli; Oct 4 '07, 04:58 AM. Reason: Changed [code] tags to [code=php] tags.

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              [PHP]$query = "
              INSERT INTO tbl_items (item_code, description) VALUES
              ('$item_code', '$description') ,
              ('$item_code', '$description') ,
              ('$item_code', '$description') ,
              ('$item_code', '$description') ,
              ('$item_code', '$description') ";
              $result = mysql_query($qu ery);
              if($result){
              mysql_query("IN SERT INTO inventory (item_code) VALUES ("$item_code")) ";
              }
              }[/PHP]
              is this possible?
              Not sure what you are doing here.
              $item_code and $description have only one value throughout.
              You are inserting the same values 5 times.
              And then inserting the same value of $item_code in to inventory

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Originally posted by backups2007
                is this possible?
                Of course this is possible. Everything is possible! (within reason)

                Try setting up a test server (if you haven't already) and just try it out. It will be a lot quicker than asking us. It's a much better way to learn.

                Comment

                • backups2007
                  New Member
                  • Jul 2007
                  • 92

                  #9
                  Originally posted by Atli
                  Of course this is possible. Everything is possible! (within reason)

                  Try setting up a test server (if you haven't already) and just try it out. It will be a lot quicker than asking us. It's a much better way to learn.
                  I've already done that. It did worked. Thanks. Xiexie. Arigato. Salamat.

                  Comment

                  Working...