473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How insert multiple values into mysql from multiple select option form

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, 6356 views)
Apr 26 '14 #1
1 26994
Dormilich
8,658 Expert Mod 8TB
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

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

Similar topics

3
by: RC | last post by:
I am try test a simple HTML form with PHP <form method=POST action="testing.php"> Cat <input type="checkbox" name="pet" value="cat"> Dog <input type="checkbox" name="pet" value="dog"> Pig...
3
by: PW | last post by:
I use a multi-select menu in an ASP so the user can pick several values. <select multiple name="lbErrorType" size="12"> However, after I have saved the records to the DB, and the user comes...
7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
1
by: Schultz | last post by:
Is there anyway to include values from 2 (or more) separate db columns into a single DropDownList using VB.NET 03? I would like the visible Data to be in the following format: lname, fname...
1
by: ykong1214 | last post by:
In my project, there is a single select tag, <html:select property="userName" size="7"> <html:options collection="UserList" property="value" labelProperty="label" /> </html:select> ...
3
by: shara | last post by:
Hello there, I want to pass multiple values selected in a select box(HTML) to another php page.I tried doing in several ways but of no use.Can anybody please help me with this. The code i used...
2
by: idorjee | last post by:
hello, i'm trying to write a perl cgi script to insert some values that i get from the my html form. i could manage to get the params from the html and process them (that part is not included in...
0
by: Maric Michaud | last post by:
Le Thursday 28 August 2008 03:43:16 norseman, vous avez écrit : Disctionaries are hash tables with a unique key and constant time lookup. What you want could be implemented as a complex data...
3
by: cubekid | last post by:
Dev't Tool: Visual C# 2003 and Javascript System Type: Web Application One requirement of the system is to dynamically create a table on client-side (Javascripting using createElement method.)...
0
by: brianrpsgt1 | last post by:
I am attempting to insert data from a HTML form using a .psp script. I can not find how to link the data that is inserted into the form to the variables in the .psp script to then insert into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.