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 ! Quote Link to comment 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.) Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted August 1, 2008 Share Posted August 1, 2008 Yup, what he ^ said. Problem solved Quote Link to comment 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. Quote Link to comment 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); ?> Quote Link to comment 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 } ?> Quote Link to comment Share on other sites More sharing options...
coder500 Posted August 1, 2008 Author Share Posted August 1, 2008 Thanks ProjectFear ! Quote Link to comment 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... Quote Link to comment 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]."')"); } Quote Link to comment Share on other sites More sharing options...
coder500 Posted August 2, 2008 Author Share Posted August 2, 2008 Yes ProjectFear! Thanks a lot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.