Php – Inserting an array into a thesql database column

arraysdatabaseMySQLPHP

I am trying to insert values from a multi select drop down in a form to a mysql db column. For example: The drop down would have one or more choices chosen then when the form is posted it would insert the data into one column in a mysql db. I'm stuck at how to insert the data.

Best Answer

If you want to insert in single row then you can use implode() to generate comma separated data, or you can do json_encode() and add to your colum.

Say you get the data as

$data = array("one", "two", "tree");

// output one, two, three
$insert_data = implode(",", $data);


or  

$insert_data = json_encode($data);

Thats for inserting data in single column. While retrieving you can do explode() or json_decode() to get the return data and can use them in the multi-select again.

If you want one row for each item then just loop through the array and add them