By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,886 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,886 IT Pros & Developers. It's quick & easy.

How insert multiple values into mysql from multiple select option form

P: 1
Am trying to insert multiple fees into my database from a select option, but when I send, only one record is inserted which is the first selection. I also want it in such a way that when I insert, the selected items will be inserted into different rows not in one row.

This is the HTML form
Expand|Select|Wrap|Line Numbers
  1. <form method="post" action="fee_allocation.php" class="box validate">
  2.  
  3.                 <div class="header">
  4.                     <h2>Student Fees Settings</h2>
  5.                 </div>
  6.  
  7.                 <div class="content">
  8.  
  9.                 <fieldset>
  10.                     <p class="_100 small">
  11.                         <label >Class Name:</label>
  12.  
  13.                         <select   class="search" data-placeholder="Choose a Class" name="class_name">
  14.                         <option value=""></option>
  15.                             <option value="1">Class One</option>
  16.                             <option value="2">Class Two</option>
  17.                             <option value="3">Class Three</option>
  18.                             <option value="4">Class Four</option>
  19.                             <option value="5">Class Five</option>
  20.                             <option value="6">Class Six</option>
  21.                             <option value="7">JHS One</option>
  22.                             <option value="8">JHS Two</option>
  23.                             <option value="9">JHS Three</option>
  24.                         </select>
  25.                     </p>
  26.                     <p class="_75 small">
  27.                         <label>Fee Discription:</label>
  28.                         <select  multiple="multiple" class="search" data-placeholder="Choose a fee" name="fee_description[]">
  29.                         <?php
  30.                         $tempholder = array();
  31.                         $query = mysql_query("SELECT Fee_ID,Fee_Description FROM ViewFees");
  32.                         $nr = mysql_num_rows($query);
  33.                         for($i=0; $i<$nr; $i++) {
  34.                         $row = mysql_fetch_array($query);
  35.                         if(!in_array($row['Fee_Description'],$tempholder)) 
  36.                         {
  37.                         echo"<option></option>";
  38.                         echo'<option value="'.$row['Fee_ID'].'" >'.$row['Fee_Description'].'</option>';
  39.  
  40.                     }
  41.                 }
  42.  
  43.                         ?>
  44.  
  45.                     </select>
  46.                     </p>
  47.                     </fieldset>
  48.                     <fieldset>
  49.                     <p  class="_25 small" style="padding-bottom: 10px;">
  50.                     <label >Start Date</label><br>
  51.                     <input type="date"  id="startdate" name="sdate" />
  52.                     </p>
  53.                     <p  class="_25 small" style="padding-bottom: 10px;">
  54.                     <label > End Date</label><br>
  55.                     <input type="date" id="end-date" name="edate" />
  56.                     </p>
  57.                     <p  class="_25 small" style="padding-bottom: 10px;">
  58.                     <label > Due Date</label><br>
  59.                     <input type="date" id="due-date" name="ddate" />
  60.                     </p>
  61.                     </fieldset>
  62.                 </div><!-- End of .content -->
  63.  
  64.                 <div class="actions">
  65.                     <div class="left">
  66.                         <input type="reset" value="Cancel" />
  67.                     </div>
  68.                     <div class="right">
  69.                         <input type="submit" value="Send"  id="fee-submit" />
  70.                     </div>
  71.                 </div><!-- End of .actions -->
  72.             <div id="message"></div>
  73.             </form>
The php code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include_once("database_connection.php");
  3. ?>
  4.  
  5. <?php
  6. $classid = $_POST['class_name'];
  7. $fee_desc = $_POST['fee_description'];
  8. $edate = $_POST['edate'];
  9. $ddate = $_POST['ddate'];
  10. $sdate = $_POST['sdate'];
  11.  
  12. foreach($fee_desc as $fee_description) {
  13.        $sql = "INSERT INTO Class_Fee VALUES (NULL, '$classid','$fee_description',STR_TO_DATE('$sdate', '%m/%d/%Y'),
  14.         STR_TO_DATE('$edate', '%m/%d/%Y'),STR_TO_DATE('$ddate', '%m/%d/%Y'))";
  15.     }
  16. $result = mysql_query($sql);
  17.  
  18. if($result) 
  19. {
  20. header("location: fee_settings.php");
  21. }
  22. else 
  23. {
  24.     echo mysql_error();
  25. }
  26.  
  27. ?>
The image of the form is attached
Please help. Thanks
Attached Images
File Type: png formView.png (21.6 KB, 4545 views)
Apr 26 '14 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
but when I send, only one record is inserted which is the first selection
in your while() loop the data overwrite each other, so that only the last one makes it to the actual DB query.
Apr 28 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.