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

I tried changing my php to this

<?php
   include '../inc/connect.php';
   if (isset($_POST['submit'])){  
      foreach($_REQUEST['adate'] as $adate ){
         mysql_query("INSERT INTO appointments  VALUES ('', '$adate')");
      }
   }
?>

But it adds all the inputs, even the empty ones! So it adds 10 records each time even if I fill in only 1 text input field.

How about putting something like

if ($adate != '') {...

into your foreach loop so that it only inserts if $adate has a value?

Thanks very much Rory, that worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.