I have a select list where users can select one or more of the options. I use an array to store these selections in the form and the POST writes them to a MySQL db as a string that I later explode when need.
This works well except for one thing. Once a record has had one or more selection saved to the db I can not re-save a selection of none.
<p><input name="var[]" type="checkbox" value="1">One</p>
<p><input name="var[]" type="checkbox" value="2">Two</p>
<p><input name="var[]" type="checkbox" value="3">Three</p>
In PHP the $_POST['var'] is imploded and saved as a string "1,2,3" or "1,3" whatever.
When I de-select all the checkboxes and submit the form the PHP $_POST['var'] is not there because it has no value (with none selected) so it doesn't over write the db column with, well, nothing...
This is part of a CMS/CRUD system so adding a conditional in the PHP management is not the desired path. The Select form can/will have a different names in different applications.
I have seen it done something like this. I know it works, but am not 100% sure it is the "correct" way to do it...or even if it will work for your needs. If you don't have much flexibility with the sql engine (or don't want to mess with it, I don't blame you...) this is a quick and dirty method to fill an array with empty values. If your sql engine doesn't support writing blank, you will need to find out how to write an "empty" spot in your table. Some use things like var[1] => '~null~'
or var[1] => 'NULL'
and the engine will recognize that string as "empty".
<form>
<input name="var[1]" type="hidden" value="" />
<p><input name="var[1]" type="checkbox" value="1">One</p>
<input name="var[2]" type="hidden" value="" />
<p><input name="var[2]" type="checkbox" value="2">Two</p>
<input name="var[3]" type="hidden" value="" />
<p><input name="var[3]" type="checkbox" value="3">Three</p>
<input type="submit" name="submit" />
</form>
Result
Array
(
[var] => Array
(
[1] => 1
[2] =>
[3] => 3
)
[submit] => Submit
)