Jump to content


Photo

Inserting Checkbox Data into DB


  • Please log in to reply
2 replies to this topic

#1 Adastra

Adastra
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 05 June 2006 - 09:06 PM

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.

...

# select the categories from the cat table to create a list of checkboxes
include('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.";
}
/*}*/


}

Thanks in advance :)

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 06 June 2006 - 08:55 AM

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:
<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/>
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...
foreach($_POST['colour'] as $c) {
    echo $c."<br/>";
}


#3 Adastra

Adastra
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 06 June 2006 - 01:50 PM

[!--quoteo(post=380527:date=Jun 6 2006, 03:55 AM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Jun 6 2006, 03:55 AM) View Post[/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:
<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/>
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...
foreach($_POST['colour'] as $c) {
    echo $c."<br/>";
}
[/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 :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users