coder500 Posted August 1, 2008 Share Posted August 1, 2008 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 More sharing options...
bretticus Posted August 1, 2008 Share Posted August 1, 2008 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 https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/#findComment-605311 Share on other sites More sharing options...
dannyb785 Posted August 1, 2008 Share Posted August 1, 2008 Yup, what he ^ said. Problem solved Link to comment https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/#findComment-605313 Share on other sites More sharing options...
JasonLewis Posted August 1, 2008 Share Posted August 1, 2008 Ignore this, I had something but don't worry about it. Link to comment https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/#findComment-605318 Share on other sites More sharing options...
coder500 Posted August 1, 2008 Author Share Posted August 1, 2008 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 https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/#findComment-605333 Share on other sites More sharing options...
JasonLewis Posted August 1, 2008 Share Posted August 1, 2008 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 https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/#findComment-605344 Share on other sites More sharing options...
coder500 Posted August 1, 2008 Author Share Posted August 1, 2008 Thanks ProjectFear ! Link to comment https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/#findComment-605563 Share on other sites More sharing options...
coder500 Posted August 2, 2008 Author Share Posted August 2, 2008 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 https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/#findComment-606038 Share on other sites More sharing options...
JasonLewis Posted August 2, 2008 Share Posted August 2, 2008 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 https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/#findComment-606042 Share on other sites More sharing options...
coder500 Posted August 2, 2008 Author Share Posted August 2, 2008 Yes ProjectFear! Thanks a lot. Link to comment https://forums.phpfreaks.com/topic/117677-inserting-many-rows-at-the-same-time/#findComment-606046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.