Page 1 of 1

How to insert checkbox data into MySQL?

Posted: Tue Apr 28, 2009 5:25 am
by anivad
(Tried googling, but an amazing number of threads on this are either unanswered or involve code that I don't understand.)

Basically I'm trying to upload details of articles onto my site, and each article has certain tags relating to its subject, which can be selected via checkbox. Simplified:

Code: Select all

<form name="update" method="post" action="updatetest.php">
<table border="0">
<tr><td>Article id: <input type="text" name="id"></td></tr>
 
<tr><td><input type="checkbox" name="tags[]" value="first.htm">Stuff
<br><input type="checkbox" name="tags[]" value="more.htm">Different Stuff
<br><input type="checkbox" name="tags[]" value="etc.htm">Another Subject
 
</td></tr>
<tr><td><input type="submit" value="submit"></td></tr>
</form>
And the code I'm trying to use, compiled and modified from various places; (This is my first experience with a for loop, and I have no idea how it works; as such I think the problem probably lies there.):

Code: Select all

<?
 
if (isset($_POST['submit'])) {
 
include '../db.php';
include '../common.php';
 
$id = $_POST['id'];
 
if (!empty($_POST['tag'])) {
 
for($i=0; $i<count($_POST['tag']); $i++) {
 
$tag = $_POST['tag'][$i];
 
$sql = "INSERT INTO tagstest SET id='$id', tag='$tag'";
$result = mysql_query($sql);
 
    if ($result) {
    error('Tag details added');
    }   
    else {
    error('Error connecting to database');
    }
}
}
}
 
?>
Thanks!

Re: How to insert checkbox data into MySQL?

Posted: Tue Apr 28, 2009 6:17 am
by lettie_dude
First thing to change is your $_POST['tag'] variable. You have the form set up as tags not tag.

Re: How to insert checkbox data into MySQL?

Posted: Tue Apr 28, 2009 6:21 am
by anivad
Argh, didn't notice that. Changed it, but still the same problem.

I get a blank page; neither one of the messages for ($result) or (!$result) are popping up.

Re: How to insert checkbox data into MySQL?

Posted: Tue Apr 28, 2009 6:39 am
by jayshields
What does error() do? Use echo().

Re: How to insert checkbox data into MySQL?

Posted: Tue Apr 28, 2009 6:42 am
by anivad
error() calls up a Javascript dialog box; it's always worked, so that's not the problem.

Changed it to echo(); still nothing.

Re: How to insert checkbox data into MySQL?

Posted: Tue Apr 28, 2009 7:06 am
by lettie_dude
Not quite sure what you are trying to do with the data you're collecting but this should create a string from your array and then add it to the db. I've taken out the db update code from the for loop as having it there will just keep updating one array item at a time over-writing the previous one.

Code: Select all

# <?
  
 if (isset($_POST['submit'])) {
  
 include '../db.php';
 include '../common.php';
  
 $id = $_POST['id'];
  
 if (isset($_POST['tags'])) {
  $tags = "";
 for($i=0; $i<count($_POST['tags']); $i++) {
  
 $tags .= $_POST['tags'][$i];
 $tags .= ', ';
  
 }
 
$tags = substr($tags, 0, -2); // removes last dilimeter ', '
 
 $sql = "INSERT INTO tagstest SET id='$id', tag='$tags'";
 $result = mysql_query($sql);
  
     if ($result) {
     echo 'Tag details added';
     } else {
     echo 'Error connecting to database';
     }
 
 }
 }

Re: How to insert checkbox data into MySQL?

Posted: Tue Apr 28, 2009 8:18 am
by anivad
EDIT: changed it to 'if ($_SERVER['REQUEST_METHOD'] == 'POST'){' so it works now. Thanks!

How do I change it to get the data inserted in separate rows for each tag? I want to have pages that can display a list of all the articles tagged with a certain subject; currently I've been doing that the hard way via HTML.

Re: How to insert checkbox data into MySQL?

Posted: Wed Apr 29, 2009 3:08 am
by lettie_dude
If I am understanding what you want to do correctly. You want to have seceral tags referencing 1 article. Is this correct? If so the way I would set it up is to create a seperate database for the tags with and id, tagname, and articleid and perhaps a date field for reference. Then associate the tags with the articleid which should be unique Inserted from the article table. Then when you list the tags use a WHERE clause to establish any tags which belong to the article id.

Ps are you filtering your form input data? If not you need to check out any of the security forum threads on sql injection otherwise you are potentially opening yourself up to a whole world of hurt!

Hope this helps

Re: How to insert checkbox data into MySQL?

Posted: Wed Apr 29, 2009 3:10 am
by anivad
Yeah, I finally worked that out. Thanks for your help! :)