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
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.)

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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
  }
  ?>

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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]."')");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.