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 Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/ 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, Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630027 Share on other sites More sharing options...
phpbeginner Posted August 30, 2008 Author Share Posted August 30, 2008 And for the ARRAY issue? Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630028 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++); Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630032 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 Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630035 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 Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630041 Share on other sites More sharing options...
Barand Posted August 31, 2008 Share Posted August 31, 2008 post the code Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630046 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]) Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630047 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>'; Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630054 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); } Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630057 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 Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630076 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>'; Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630293 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. Link to comment https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/#findComment-630314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.