Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Multiple Record Insert

Posted on 2007-10-03
7
Medium Priority
?
431 Views
Last Modified: 2013-12-13
Hi All,

I need a way of adding multiple records to a DB table, as specified by the user. For example a user needs 15 records added, he can simply enters '15' in a text box, submit and 15 records are inserted into the DB.

Is this possible? Any solutions or recommendations would be greatly appreciated.

Marius
0
Comment
Question by:MariusGM
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 17

Assisted Solution

by:Aleksandar Bradarić
Aleksandar Bradarić earned 800 total points
ID: 20006112
Not sure of the details, but you can always construct the appropriate SQL using PHP. If you need for rows added, use:
---
INSERT INTO yourTable(field1, field2) VALUES ('value11', 'value12'), ('value21', 'value22'), ('value31', 'value32'), ('value41', 'value42')
---

Same for as many rows as you need.
0
 
LVL 17

Expert Comment

by:Aleksandar Bradarić
ID: 20006116
> need for rows

need four rows
0
 
LVL 13

Expert Comment

by:MasonWolf
ID: 20006136
PHP does not support running multiple queries with a single call to mysql_query, so the best option left to you is to iterate through a basic "for" loop.

for($i = 0; $i < $_POST['num_rows']; $i++)
{
     mysql_query($query);
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MariusGM
ID: 20006275
Hi MasonWolf. A for loop iteration sounds like the ticket.

Not sure how to implement the code example you gave though. Still learning PHP. Could you possibly give me an example with a form and required PHP logic.

Such as:

<form id="add_rows" name="add_rows" method="post" action="">
  <input name="row_total" type="text" id="row_total" />
  <input type="submit" name="Submit" value="Submit" />
</form>

Thanks in advance - M
0
 
LVL 13

Accepted Solution

by:
MasonWolf earned 1200 total points
ID: 20007058
Actually, I was writing my comment when leannom was posting his. I knew it was impossible to do multiple queries with a mysql_query call, but I didn't realize you could just use one query for multiple inserts like that. I like that method better, since string functions are relatively fast and mysql queries are comparatively slow. I'm assuming it works, but having not even known about it until earlier this morning, I've never tested it.

Here's my suggestion based on that, and if it works then split the points between us both please.

<?php
   if(isset($_POST['row_total']) && ereg('^[0-9]+$', $_POST['row_total']) && $_POST['row_total'] > 0)
   {
       $query = "INSERT INTO table ('column1', 'column2') VALUES ('value1', 'value2')";
       for($i = 1; $i < $_POST['row_total']; $i++)
       {
            $query .= ", ('value1', 'value2')";
       }
       mysql_query($query);
   }
?>
<form method="post">
  <input name="row_total" type="text" id="row_total" onKeyUp="this.value=this.value.replace(/\D+/,'');" />
  <input type="submit" name="Submit" value="Submit" />
</form>
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 20009053
not sure you want this?

you need to create this table

CREATE TABLE `three` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(11) NOT NULL,
  `age` varchar(11) NOT NULL,
  PRIMARY KEY  (`id`)
)


<form action="" method="post">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>#</td>
    <td>name</td>
    <td>age</td>
  </tr>
  <tr>
    <td>1</td>
    <td><input type="text" name="name[]"></td>
    <td><input type="text" name="age[]"></td>
  </tr>
  <tr>
    <td>2</td>
    <td><input type="text" name="name[]"></td>
    <td><input type="text" name="age[]"></td>
  </tr>
  <tr>
    <td>3</td>
    <td><input type="text" name="name[]"></td>
    <td><input type="text" name="age[]"></td>
  </tr>
  <tr>
    <td>4</td>
    <td><input type="text" name="name[]"></td>
    <td><input type="text" name="age[]"></td>
  </tr>
  <tr>
    <td>5</td>
    <td><input type="text" name="name[]"></td>
    <td><input type="text" name="age[]"></td>
  </tr>
  <tr>
    <td colspan="3"><input type="submit" name="Submit" value="Submit"></td>
  </tr>
</table>
</form>
<?php
      $link = mysql_connect("localhost", "root", "");
      mysql_select_db("cars", $link);
      
      $i = 0;
      while($_POST['name'][$i]) {
            $name = $_POST['name'][$i];
            $age  = $_POST['age'][$i];
            mysql_query("INSERT INTO three (name, age) VALUES ('$name', '$age')");
            $i++;
      }
?>
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 20009071
this does not matter how many records you need to enter. you just need to do

1; create fields array and keep in form
2; submit form and get posted array
3; loop through array and insert into DB
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question