How to insert checkbox data into MySQL?
Posted: Tue Apr 28, 2009 5:25 am
(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:
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.):
Thanks!
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>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');
}
}
}
}
?>