Ramkumar M
Ramkumar M

Reputation: 119

How to Store Multi Select Box in php?

am having select box with multiple options, like option1,option2,option3,option4,option5.Now i want to select more than one option.


For Example: Select option1,option5,option3.and click submit.after submitting the select box need to show the selected items.


in which format to be stored in mysql and what's the exact coding to do that?please help me to get rid of this problem...

Upvotes: 0

Views: 8195

Answers (3)

Ivo San
Ivo San

Reputation: 1253

use implode

$var = implode ( "|", $_POST['selectname']);

Upvotes: 2

Dan
Dan

Reputation: 1244

You could serialize the data in and out.

Use name[] as your name which results in an array in PHP.

$_POST['name'];
serialize($name);
//now insert into DB

Do the opposite to retrieve:

unserialize($name);

But to be honest you are better off looking at more granular methods of storing the data, rather than trying to stuff it all into one field in a database. You could take the array and loop through it storing each choice in a separate row or field.

foreach($name as $value){
//insert $value into db
}

Upvotes: 4

Related Questions