473,320 Members | 2,052 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.

Multiple Select box to MySQL PHP

I want a user to choose values from the Multiple Select box:

Expand|Select|Wrap|Line Numbers
  1. <select name="service[]" id="service" size="6" multiple="multiple">
  2. <option value="histopathology">Histopathology</option>
  3. <option value="cytology">Cytology</option>
  4. <option value="endometrial biopsy">Endometrial Biopsy</option>
  5. <option value="joint fluid">Joint Fluid</option>
  6. <option value="bone marrow">Bone Marrow</option>
  7. <option value="other">Other</option>
  8. </select>
the table is:

Expand|Select|Wrap|Line Numbers
  1. services CREATE TABLE `services` (
  2. `service_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. `form_number` int(20) NOT NULL,
  4. `histo` tinyint(1) DEFAULT NULL,
  5. `cytology` tinyint(1) DEFAULT NULL,
  6. `endo` tinyint(1) DEFAULT NULL,
  7. `joint_fluid` tinyint(1) DEFAULT NULL,
  8. `bone_marrow` tinyint(1) DEFAULT NULL,
  9. `other` varchar(50) DEFAULT NULL,
  10. PRIMARY KEY (`service_id`)
  11. ) ENGINE=MyISAM DEFAULT CHARSET=latin1

im not sure how to insert the information as one record with the selections they choose.
May 31 '10 #1

✓ answered by Atli

Hey.

When you submit a select like that, the selected options are available in your PHP code as an array, found in the $_POST super-global. Like in your code, if I were to select "histopathology" and "other", PHP would make those choices available in this format:
Expand|Select|Wrap|Line Numbers
  1. $_POST => Array
  2. (
  3.     [service] => Array
  4.         (
  5.             [0] => histopathology
  6.             [1] => other
  7.         )
  8.  
  9. )
  10.  
So to figure out which choices were selected, you simple check if the choice exists in that array. To do that, you can use the in_array function.
Expand|Select|Wrap|Line Numbers
  1. $fieldExists = in_array("fieldName", $_POST['service']);
Then you use that to build your query. For example:
Expand|Select|Wrap|Line Numbers
  1. // Create the top of the SQL query
  2. $sql = "INSRT INTO `services` SET\n";
  3.  
  4. // Find out which choices were selected.
  5. $data = array(
  6.     'histo' =>       in_array('histopathology', $_POST['service']),
  7.     'cytology' =>    in_array('cytology', $_POST['service']),
  8.     'endo' =>        in_array('endometrial biopsy', $_POST['service']),
  9.     'joint_fluid' => in_array('joint fluid', $_POST['service']),
  10.     'bone_marrow' => in_array('bone marrow', $_POST['service']),
  11.     'other' =>       in_array('other', $_POST['service']),
  12. );
  13.  
  14. // Add each option to the SQL query.
  15. foreach($data as $_fieldName => $_fieldValue) {
  16.     $_fieldValue = $_fieldValue ? "TRUE" : "FALSE";
  17.     $sql .= "    `{$_fieldName}` = $_fieldValue\n";
  18. }
  19.  
  20. // Echo the query
  21. echo $sql;
This might print something like:
Expand|Select|Wrap|Line Numbers
  1. INSRT INTO `services` SET
  2.     `histo` = FALSE
  3.     `cytology` = TRUE
  4.     `endo` = FALSE
  5.     `joint_fluid` = TRUE
  6.     `bone_marrow` = FALSE
  7.     `other` = TRUE

2 2926
Atli
5,058 Expert 4TB
Hey.

When you submit a select like that, the selected options are available in your PHP code as an array, found in the $_POST super-global. Like in your code, if I were to select "histopathology" and "other", PHP would make those choices available in this format:
Expand|Select|Wrap|Line Numbers
  1. $_POST => Array
  2. (
  3.     [service] => Array
  4.         (
  5.             [0] => histopathology
  6.             [1] => other
  7.         )
  8.  
  9. )
  10.  
So to figure out which choices were selected, you simple check if the choice exists in that array. To do that, you can use the in_array function.
Expand|Select|Wrap|Line Numbers
  1. $fieldExists = in_array("fieldName", $_POST['service']);
Then you use that to build your query. For example:
Expand|Select|Wrap|Line Numbers
  1. // Create the top of the SQL query
  2. $sql = "INSRT INTO `services` SET\n";
  3.  
  4. // Find out which choices were selected.
  5. $data = array(
  6.     'histo' =>       in_array('histopathology', $_POST['service']),
  7.     'cytology' =>    in_array('cytology', $_POST['service']),
  8.     'endo' =>        in_array('endometrial biopsy', $_POST['service']),
  9.     'joint_fluid' => in_array('joint fluid', $_POST['service']),
  10.     'bone_marrow' => in_array('bone marrow', $_POST['service']),
  11.     'other' =>       in_array('other', $_POST['service']),
  12. );
  13.  
  14. // Add each option to the SQL query.
  15. foreach($data as $_fieldName => $_fieldValue) {
  16.     $_fieldValue = $_fieldValue ? "TRUE" : "FALSE";
  17.     $sql .= "    `{$_fieldName}` = $_fieldValue\n";
  18. }
  19.  
  20. // Echo the query
  21. echo $sql;
This might print something like:
Expand|Select|Wrap|Line Numbers
  1. INSRT INTO `services` SET
  2.     `histo` = FALSE
  3.     `cytology` = TRUE
  4.     `endo` = FALSE
  5.     `joint_fluid` = TRUE
  6.     `bone_marrow` = FALSE
  7.     `other` = TRUE
May 31 '10 #2
thanks so much for your reply...it helps out for my project...
May 31 '10 #3

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

Similar topics

2
by: murraymiken | last post by:
I'm looking to have multiple multiple-select-boxes on a page. But I can only get the contents from the last selected value within a box, via PHP. I've tried numerous methods. What am I doing...
1
by: abhishekhs | last post by:
Hi all I have more than one multiple select lists in a page. Something like this <tr> <td> <select NAME="StrainList" ID="StrainList" SIZE="5" multiple="multiple" style="width: 150px"> <?...
3
by: scott | last post by:
Hello all, I am ripping my hair out over this and maybe someone could help. I have a site that has groups and subgroups of those groups that all have int id's. I am trying to have a...
7
by: ozzii | last post by:
Hi I have a HTML form with a multiple select box. The multiple select box is populated from a database. A user can select multiple options form this select box and the infomrtaion is then stored...
0
by: sundsx | last post by:
Hi, i would sen multiple select to mysql by form, my prb is: file connect.php <?php $user="sundsx"; $pass="password"; $db="testphp"; $val_form=$_POST; $service =$_POST;
2
by: sathyashrayan | last post by:
Dear group, I have a task where I have to fetch datas from mysql and display it in the multiple select list box. I have fetched the datas and it is getting diplayed correctly in the multiple...
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have coded multiple select statements in a single stored procedure, and when I execute this procedure on SQL Server Management Express, I correctly get multiple result sets. But, if I try to add...
3
by: fishctr | last post by:
Hi There, I am creating a survey that asks users what countries they have worked in, and which regions in those countries. Because there can be many answers, I was thinking I could populate a...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
6
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.