Jump to content

Multiple Select Drop Down Data Saved to MySQL - how?


sleepyw

Recommended Posts

I have an HTML form with a multiple select drop down field. I'm trying to figure out 2 things:

 

1. If the user selects multiple items from the drop down box, how to I write that data to MySQL on the submit page? Can I have it separated by commas?

 

2. If a user goes back to edit that item's data from that drop down box, how do I script the form's select and options fields to pull all items from that multi select array?

 

I'm not a PHP guru by any means and haven't dealt with a lot of arrays in this fashion.

 

Here's an example of the drop down HTML form (I am pulling the option values from another table to populate the drop- down list):

 

<select name="Type" multiple size="4">
               				 		<option value="<? echo $rows['Type']; ?>" selected><? echo $rows['Type']; ?></option>
               				 		<? $Type_sql="SELECT DISTINCT Type FROM Table ORDER by Type";
               				 		$Type_result=mysql_query($Type_sql);
               				 		while($Type_rows=mysql_fetch_array($Type_result)){
               				 		echo "<option value='"; 
               				 		echo $Type_rows['Type']; 
               				 		echo "'>";
               				 		echo $Type_rows['Type'];
               				 		echo "</option>";
               				 		} ?>
               				 	</select>

 

Now on the submission page, I have a simple

UPDATE TABLE SET Type=$_POST['Type']

kind of thing. How would I modify that to insert all multi select items? And then how do I read that data to alter the above form code to pull all options in the db and show them as selected in the drop down?

 

Help is very much appreciated.

Link to comment
Share on other sites

If you want to store them as separate records (I.e. a new row in your mysql for each drop down thing selected) you can also do something like this:

 

<select name="ingred[]" size=35>
<option value="foo">Foo</option>
<option value="foo2">Foo2</option>
<option value="foo3">Foo3</option>

 

And then process it like this:

 

for ($i = 0; $i < 16; $i++) {
              if ($ingred[$i] != "") {
                 $sql = "insert into ingredients (ingredient) values (''$ingred[$i]')";
                 $result = mysql_query($sql ,$db);
              }
	   }

 

That will make a new record for every choice they selected.

Link to comment
Share on other sites

you need to use array type variable, like,

<select name="Type[]" multiple size="4">

 

after submitting form, you can get values like this

 

$type = implode(",",$_POST['Type']); //here i am converting array to string with comma separted

 

That works perfectly!

 

Now, the only other thing is how do I extract those items to be selected in the form drop down for someone that wants to edit the entry (removing the comma, of course)? It should have each item selected in the drop down. I'm trying to think of how to write something that says if the item in the db matches an item in the option field, then it should be "selected" in the option tag.

 

Jax - thanks for the help, but not quite what I'm trying to do.

Link to comment
Share on other sites

I think I got it working. I did this to select the items exploded from the db into a selected option drop down:

$Type_exploded = explode(",", $rows['Type']);
if(is_array($Type_exploded)) {
while (list ($key, $Type_val) = each ($Type_exploded)) {
echo "<option value=" . $Type_val . " selected>" . $Type_val . "</option>";
}
}

Thanks again for the help!

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.