Worth2Read
Home

Forum

PHP Tutorials
Blogs
Children
Article Directory

Contact Us

Disclaimer
Reliable $1 Web Hosting by 3iX
 

If you have any doubts with your PHP code please discuss it in the PHP Forum

INSERTING MULTIPLE ROWS AT THE SAME TIME

There may be situations to insert multiple records simultaneously to the database. For example many students may register for a program. In such cases we may not know how many students may be registering. The following code produces an output like this :

The number of records to be inserted : [ here there will be a text box to enter the number of records]

Depending on the number of records the text boxes to enter records will appear.

Finally, after submitting the insert button the data will be entered to the database.

<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>
</form>
<form name="form2" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
// start of dynamic form
$num_rec = $_POST['num_rec'];
for($x=0;$x<$num_rec;$x++){
?>
<input type="text" name="check[]" /><input type="text" name="email[]" /><br />
<?php
}
?>
<p>
<?php
if($num_rec>0)
{ ?>
<input type="submit" name="Insert" value="Insert">
<?php
}
?>
</p>
</form>
<?php
$db = mysql_connect ( "localhost" , "username" , "password" )or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("databasename");
for($i = 0; $i < count($_POST['check']); $i++)
{
$query=mysql_query("INSERT INTO tb_signup (userid,email) VALUES ('".$_POST['check'][$i]."','".$_POST['email'][$i]."')");
}
mysql_close($db); ?>

Untitled Document
Copyright © 2008 www.worth2read.org. All rights Reserved. Use of this Site is subject to our Privacy Statement