How to insert checkbox data into MySQL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

How to insert checkbox data into MySQL?

Post 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!
Last edited by Benjamin on Tue Apr 28, 2009 1:27 pm, edited 1 time in total.
Reason: Changed code types from text to html, php.
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: How to insert checkbox data into MySQL?

Post by lettie_dude »

First thing to change is your $_POST['tag'] variable. You have the form set up as tags not tag.
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: How to insert checkbox data into MySQL?

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: How to insert checkbox data into MySQL?

Post by jayshields »

What does error() do? Use echo().
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: How to insert checkbox data into MySQL?

Post 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.
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: How to insert checkbox data into MySQL?

Post 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';
     }
 
 }
 }
Last edited by Benjamin on Wed Apr 29, 2009 4:26 am, edited 2 times in total.
Reason: Changed code type from text to php.
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: How to insert checkbox data into MySQL?

Post 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.
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: How to insert checkbox data into MySQL?

Post 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
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: How to insert checkbox data into MySQL?

Post by anivad »

Yeah, I finally worked that out. Thanks for your help! :)
Post Reply