Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Batch add records to database

    I would like to create an option on my admin page to add batch records to Mysql database.

    I guess I need a textarea, a field limiter and a line break

    After the form has been submitted, the script should parse the data into arrays and add each array value to the database, most probably with a loop.

    Can someone help me here? Example:

    books

    title | year | author

    And what if I want to upload and insert CSV format (without phpmyadmin)?

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Not sure exactly what you mean, but here's my best guess:

    You want to have a textarea where you can insert data for multiple database rows.

    You have a database with the fields title, year, and author.

    In your textarea, enter the data like this:

    title-year-author;title2-year2-author3;title3-year3-author3; and so on

    then use this code:
    PHP Code:
    $row explode(";",$_POST['textarea']);
    foreach(
    $row)
       {
       
    $field explode("-",$row);
       
    $query "INSERT INTO `tablename` (`title`,`year`,`author`) VALUES '{$field[0]}','{$field[1]}',{$field[2]}'";
      
    //Run query
       

    HTH,
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    It's almost like it. Except each record row will be seperated with a line break and the seperator should be something unique like :: or | etc.

    An example of a textarea input:

    HONEYMOON|2006|James Patterson
    GONE|2005|Lisa Gardner
    S IS FOR SILENCE|Sue Grafton

    So maybe your code can be modified like this:

    PHP Code:
    $row explode("\n",$_POST['textarea']);
    foreach(
    $row)
       {
       
    $field explode("|",$row);
       
    $query "INSERT INTO `tablename` (`title`,`year`,`author`) VALUES '{$field[0]}','{$field[1]}',{$field[2]}'";
      
    //Run query
       


    And I am curious about a CSV import.

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Yeah that should work... I haven't tested it or anything, but it looks good enough to me...

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    MySQL allows inserting more than one row in one query (newer MySQL varsions that is).
    Code:
    INSERT INTO table ('column1', 'column 2', ...) VALUES
    (1, 2, ...),
    (3, 4, ...),
    ...
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

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