Jump to content

Form to add multiple records to MySql


glen-rogers

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.