We help IT Professionals succeed at work.

PHP Multiple Record Insert

MariusGM
MariusGM asked
on
444 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
Comment
Watch Question

Aleksandar BradarićSoftware Developer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Aleksandar BradarićSoftware Developer
CERTIFIED EXPERT

Commented:
> need for rows

need four rows
CERTIFIED EXPERT

Commented:
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);
}

Author

Commented:
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
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
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++;
      }
?>
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.