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 !
Are you using MySQL or PG... MySQL, I guess. the mysql_query() function handles only one query at once, so either insert the records using one query or use multiple mysql_query(); Link: http://us2.php.net/mysql_query
I am thinking about using for loop in php with single querry. But I could not so far. I am posting the code here <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 $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); ?>