homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

multiple drop down boxes - adding values to the database

 8:55 pm on Nov 13, 2012 (gmt 0)

So we have:

<form method="post">
<select multiple="multiple" name="multi">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<input name="submit" "type="submit" value="submit" />


if(isset($_POST['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?



 1:29 am on Nov 15, 2012 (gmt 0)

$_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);
$sql="INSERT .......".implode(",",$data)";

That way, only data which is a positive integer will be inserted.


 7:14 am on Nov 15, 2012 (gmt 0)

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?


 2:19 pm on Nov 15, 2012 (gmt 0)

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.


 2:38 pm on Nov 15, 2012 (gmt 0)

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.


 6:58 pm on Nov 15, 2012 (gmt 0)

It's for tags, I want to be able to tag categories for a page.


 12:13 am on Nov 16, 2012 (gmt 0)

Sounds like an N:N relation even.

What I'd model are 3 tables:

- widgetId
- widgetName
- ...
These essentially descibe a widget for each row, leaving out all info about tags

- tagID
- tagName
- ...
These essentially define a tag without thinking to what widget they apply

- widgetId
- tagId
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)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved