glen-rogers Posted April 25, 2013 Share Posted April 25, 2013 Hi, I have an html form that has 10 text field for trhe user too enter dates into. When the user clicks the submit button each text field should be added as a new record to a mysql table. The way I have it now if only 2 are filled in, rateher than making only 2 entries with the post data, it makes 20: the 2 that should be there and 8 empty ones, then repeats this, making 20. Can anyone offer assistance? Here is my code as it stands. <form class='form1' action='appointmentaddform.php' method='post' enctype='multipart/form-data' name='add_news-form' id='add_news_form'> <p><b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[0]' /><br /> <b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[1]' /><br /> <b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[2]' /><br /> <b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[3]' /><br /> <b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[4]' /><br /> <b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[5]' /><br /> <b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[6]' /><br /> <b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[7]' /><br /> <b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[8]' /><br /> <b><span class='text'>Date + Time<span></b> <input type=text size='50' name='adate[9]' /></p> <br /> <input type='submit' id='submit' name='submit' value='Add Appointments' /> <input type='hidden' value='new' /> </form> <?php include '../inc/connect.php'; if (isset($_POST['submit'])){ while(!empty($_REQUEST['adate'])){ foreach($_REQUEST['adate'] as $adate ){ mysql_query("INSERT INTO appointments VALUES ('', '$adate')"); } } } ?> Thanks Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 This is a very interesting method you have created. The first thing that stands out to me is that your INSERT query is inside of your while() loop. That is a bad practice. INSERT commands are designed so you don't have to loop data in order to make them work. Just INSERT all of the values and query the database. No need to loop it. Quote Link to comment Share on other sites More sharing options...
glen-rogers Posted April 25, 2013 Author Share Posted April 25, 2013 Hi thanks for the reply. I have changed my php to this if (isset($_POST['submit'])){ foreach($_REQUEST['adate'] as $adate ){ mysql_query("INSERT INTO appointments VALUES ('', '$adate')"); } } Is this any closer? Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 You need to name a column in your query... mysql_query("INSERT INTO appointments(column1, column2) VALUES ('', '$adate')"); I am not sure why you have an empty value that you are inserting into your database. I would drop that and column2 if they are unnecessary. Reference is below.http://stackoverflow.com/questions/15011108/php-foreach-loop-inserting-into-database 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.