Jump to content

Inserting Checkbox Data into DB


Adastra

Recommended Posts

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 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.";
}
/*}*/


}
[/code]

Thanks in advance :)
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[!--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 :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.