|multiple drop down boxes - adding values to the database|
So we have:
<select multiple="multiple" name="multi">
<input name="submit" "type="submit" value="submit" />
$multi = $_POST['multi'];
So if multiple selections are made, then how do we take these values, I assume we need to create an array, but not sure how this is done.
Also how do we input them into the database, separated by commas?
$_POST['multi'] will already be an array; if you want to separate them by commas, implode: implode(",",$_POST['multi']);
However: if this is a public facing system, then please do some careful checks on valid values... I do it like this:
foreach ($_POST['multi'] as $k=>$v) if (intval($k)) $data=intval($k);
That way, only data which is a positive integer will be inserted.
And it would be best in the database like 1,2,3, etc.. with the commas? would that be the easiest way to manage them?
As for the datamodel I''m by far not convinced imploding into one field is a smart choice.
It seems to me you have a 1:N relation, and should model it appropriately to normalize your data and as such maximise the use of what your relational database can offer.
It really depends on what you want to do with the data later. Comma separated strings are not convenient or efficient for searching by, but if it's just retrieval and you'll always want to retrieve the full set - then yes, do use comma separated strings. Otherwise, what swa66 said.
It's for tags, I want to be able to tag categories for a page.
Sounds like an N:N relation even.
What I'd model are 3 tables:
These essentially descibe a widget for each row, leaving out all info about tags
These essentially define a tag without thinking to what widget they apply
These link the tags and widgets together.
A tag can be used on multiple widgets and a widget can have multiple tags.
That way a query to find all widgets with a given tag can be made, a query to know all possible tags, en query to find what tags a widget has are all eauqlly easy (just a couple of joins in one select statement)