Jump to content

INSERTING MANY ROWS AT THE SAME TIME


coder500

Recommended Posts

I want to insert multiple records to the database at the same time.

First I need to enter the number of records to be inserted.

Depending on the number of records to be inserted

<input type="text">

should come

After entering all records , they need to be inserted in the database.

I am looking for an option that can be done using single PHP page. Is it possible?

Any help !

Link to comment
https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/
Share on other sites

Sure it's possible.

 

You can do this by looping through all the records to insert one query at a time. Here's a generic example:

 

<?php
foreach ($_POST['checkbox'] AS $value) {
$sql = "INSERT INTO tbl_name (my_column) VALUES ('$value')";
// ... mysql functions ... 
}
?>

 

Or you can do it all in one query by linking values:

 

<?php
$sql = "INSERT INTO tbl_name (my_column) VALUES ";
foreach ($_POST['checkbox'] AS $value) {
$sql .= "('$value'), ";
}
// ... mysql functions ... 
?>

 

In the later example, I'll leave the problem with pulling off the last comma to you (if this example actually pertains to your needs at all.)

Hi bretticus ,

Thanks for the hint. But I could not solve it .

I am posting my code here. Can you make a quick look and correct it please...

 

<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >

  <label>Number of rows to be inserted

  <input type="text" name="num_rec" />

  </label>

  <label>

  <input type="submit" name="display" value="Display" />

  </label>

  <p> </p>

</form>

<form name="form2" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

 

  <?php

    $num_rec = $_POST['num_rec'];

 

  for($x=0;$x<$num_rec;$x++){

  ?>

  <input type="text" name="check"  />

 

   <?

  // end of for loop

  }

  ?>

  <p>

    <input type="submit" name="Insert" value="Insert">

  </p>

</form>

 

<?php

$db  =  mysql_connect ( "" , "" , "" )or die('Cannot connect to the database because: ' . mysql_error());   

mysql_select_db ("");

foreach ($_POST['check'] AS $value) {

$query=mysql_query("INSERT INTO tb_signup (userid) VALUES ('$value')");

}

mysql_close($db);

?>

All you need to do is in the for loop that you have, make the name an array. Just place [] on the end.

 

Like so:

<?php
    $num_rec = $_POST['num_rec'];

  for($x=0;$x<$num_rec;$x++){
  ?>
  <input type="text" name="check[]"  />

   <?
  // end of for loop
  }
  ?>

Hi

It worked. But one more doubt..

I want to enter multiple fields, then how will it be/

I used

foreach ($_POST['check'] AS $value) {

$email=$_POST['email'];

$query=mysql_query("INSERT INTO tb_signup (userid,email) VALUES ('$value','$email')");

}

But it inserts 'array' instead of email.

Any help, please...

If both $_POST['check'] and $_POST['email'] will have the exact same amount of elements, every time, do this:

 

for($i = 0; $i < count($_POST['check']); $i++){
$query=mysql_query("INSERT INTO tb_signup (userid,email) VALUES ('".$_POST['check'][$i]."','".$_POST['email'][$i]."')");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.