Adastra Posted June 5, 2006 Share Posted June 5, 2006 I can't figure out the how to post the checkbox data for multiple categories on a small blog script to a database. I have three tables, the blog table, the blog_cat table with the id's & names of the categories and the blog_catjoin table with the id's of the entry and of the catID. Can someone help me out? My PHP knowledge is still very basic and PHP.net just confuses me. I know it's gotta be something with arrays and for or foreach, but I don't understand how to use them.Relevant parts of the script (with some weird testing-stuff and some stuff commented out):Currently, this version posts both an entry to the blog table, but it only posts one category into the catjoin table (always the one with the highest ID), however, it should of course make seperate entries for each checked category checkbox. [code]...# select the categories from the cat table to create a list of checkboxesinclude('connect.php');$result = mysql_query("SELECT blog_cat.catID, catname FROM blog_cat LEFT JOIN blog_catjoin ON blog_cat.catID=blog_catjoin.catID GROUP BY blog_cat.catID ORDER BY catname ASC") or print ("Can't select categories." . $result . "" . mysql_error());while($row = mysql_fetch_array($result)) {$catID = $row["catID"];$catname = $row["catname"];echo "<.input type=\"checkbox\" name=\"$cat_array\" value=\"$catID\"";echo " /> $catname ";}....<./form>....# insert all the stuff into the database$sql = "INSERT INTO $table (...) VALUES (....)";$result = mysql_query($sql) or print ("Unable to post data." . $sql . "" . mysql_error());if ($result != false) {print "Entry has been posted! ($title)";}$idresult = mysql_query("SELECT id from $table ORDER by id DESC LIMIT 1") or print ("Can't select categories." . $result . "" . mysql_error());while($idrow = mysql_fetch_array($idresult)) {$id = $idrow["id"];}/*foreach ($cat_array as $cat) {*/$query_cat = "INSERT INTO blog_catjoin VALUES ('','$id','$catID')";$result_cat = mysql_query($query_cat);if ($result_cat != false) {echo "All categories successfully added.";}/*}*/}[/code]Thanks in advance :) Quote Link to comment https://forums.phpfreaks.com/topic/11270-inserting-checkbox-data-into-db/ Share on other sites More sharing options...
zq29 Posted June 6, 2006 Share Posted June 6, 2006 First up, on your HTML form all of your checkboxes need the same name, but referenced as an array, this can be done by adding [] onto the end of the name, like so:[code]<input type="checkbox" name="colour[]" value="red"/> Red<br/><input type="checkbox" name="colour[]" value="green"/> Green<br/><input type="checkbox" name="colour[]" value="blue"/> Blue<br/>[/code]They can then be received as an array when fetching with $_POST[], and as you mentioned, looped through with a foreach(). You can do it like so...[code]foreach($_POST['colour'] as $c) { echo $c."<br/>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11270-inserting-checkbox-data-into-db/#findComment-42338 Share on other sites More sharing options...
Adastra Posted June 6, 2006 Author Share Posted June 6, 2006 [!--quoteo(post=380527:date=Jun 6 2006, 03:55 AM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Jun 6 2006, 03:55 AM) [snapback]380527[/snapback][/div][div class=\'quotemain\'][!--quotec--]First up, on your HTML form all of your checkboxes need the same name, but referenced as an array, this can be done by adding [] onto the end of the name, like so:[code]<input type="checkbox" name="colour[]" value="red"/> Red<br/><input type="checkbox" name="colour[]" value="green"/> Green<br/><input type="checkbox" name="colour[]" value="blue"/> Blue<br/>[/code]They can then be received as an array when fetching with $_POST[], and as you mentioned, looped through with a foreach(). You can do it like so...[code]foreach($_POST['colour'] as $c) { echo $c."<br/>";}[/code][/quote]Yay, that's it, thank you very much! :)I didn't know about the $_POST part and I mixed up stuff in the foreach() { }, that's why it didn't work til now :) Quote Link to comment https://forums.phpfreaks.com/topic/11270-inserting-checkbox-data-into-db/#findComment-42410 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.