phpbeginner Posted August 30, 2008 Share Posted August 30, 2008 I am using a form to update my database and within the form there are a large number of fields which I update on GO. Within that form one of the fields I am updating, I would like to have a multiple selection choice for that field as below. <input type=checkbox name=flooring[] value=Hardwood> Hardwood <input type=checkbox name=flooring[] value=Carpet> Carpet <input type=checkbox name=flooring[] value=Linoleum> Linoleum Now when I submit go there are all kinds of things I am inserting including flooring $query = "INSERT INTO styles (flooring) values($flooring) But in the database all that is inserted is ARRAY. Also, what is the best field type to insert multiple selections in? Thanks In Advance Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2008 Share Posted August 30, 2008 what is the best field type to insert multiple selections in? A separate table, properly normalized, Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted August 30, 2008 Author Share Posted August 30, 2008 And for the ARRAY issue? Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted August 31, 2008 Author Share Posted August 31, 2008 I changed the value to $flooring[$i] and added this but will not insert anything..... $flooringCount = count($flooring); for ($i=0; $i < $flooringCount; $i++); Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2008 Share Posted August 31, 2008 it sounds like you want to $flooringTypes = join (',', $floorings); $query = "INSERT INTO styles (flooring) values($flooringTypes)"; I'd recommend foreach ($floorings as $flooringType) { $query = "INSERT INTO styles (flooring) values($flooringType)"; } where the flooring style table has an autoinc id Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted August 31, 2008 Author Share Posted August 31, 2008 Tried option 1 which I was trying to achieve but still just adds ARRAY to the fldflooring Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2008 Share Posted August 31, 2008 post the code Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted August 31, 2008 Author Share Posted August 31, 2008 Form..... <input type=checkbox name=flooring[] value=Hardwood> Hardwood <input type=checkbox name=flooring[] value=Carpet> Carpet <input type=checkbox name=flooring[] value=Linoleum> Linoleum Then if ($action=="go") { $flooringCount = count($flooring); for ($i=0; $i < $flooringCount; $i++) $query = "INSERT INTO styles (flooring) values($flooring[$i]) Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2008 Share Posted August 31, 2008 that should be the same as my option 2 what does this output echo '<pre>', print_r($flooring, 1), '</pre>'; Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2008 Share Posted August 31, 2008 of course, you need to execute the queries foreach ($flooring as $flooringType) { $query = "INSERT INTO styles (flooring) values('$flooringType')"; mysql_query($query); } or in your code for ($i=0; $i < $flooringCount; $i++) { $query = "INSERT INTO styles (flooring) values('$flooring[$i]')"; mysql_query($query); } Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted August 31, 2008 Author Share Posted August 31, 2008 I dunno.....will still only insert 1 selection Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2008 Share Posted August 31, 2008 what does this output echo '<pre>', print_r($flooring, 1), '</pre>'; Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted August 31, 2008 Author Share Posted August 31, 2008 Hi Barand, I got it to work using the following. $flooring=$_POST["flooring"]; $flooringstr=""; for($i=0;$i<sizeof($flooring);$i++){ $flooringstr .= $flooring[$i]." ";} and changing value to ($flooringstr). NOW, I having a tremendous amount of difficulty with populating the checkboxes on my edit page. I am inserting the values in a text type field, and as you can see, seperated by a space. Any ideas on this one? Appreciate your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.