Php not inserting checkbox data in 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
helloworld
Forum Newbie
Posts: 3
Joined: Tue Jul 28, 2009 9:40 am

Php not inserting checkbox data in mysql

Post by helloworld »

Hi all im having serious issues with this script. At current the script inserts a checked category number into the table images_catlu into the cat_ID field.
How do I modyfy it to insert multiple checked data into the table?

Here is the code, any help would be awesome :P

Code: Select all

<?php
include("connect.php");
   mysql_select_db("artists");
    $cat_ID = $_POST['cat_id']; 
    $image_ID = $_POST['image_id'];
$result = mysql_query("SELECT cat_ID FROM catorgories"); 
$result = mysql_query("SELECT image_ID FROM catorgories");    
    $query = "INSERT INTO image_catlu (cat_ID, image_ID)
   VALUES ('$cat_ID', '$image_ID')";
 
 
 
                        
   $results = mysql_query($query) or die 
   ("Could not execute query : $query." . mysql_error());
   
   
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CHECKBOX</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
 
<table>
<tr>
</tr>
<?php
$sql = "SELECT id,cat FROM catorgories ORDER by id ASC";
       "SELECT id,id FROM gallery";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
while(list($id,$cat)=mysql_fetch_row($result)){
 
    echo '<tr><td>'.$cat.'</td><td><input type="checkbox" name="cat_id" value="'.$id.'" '.$image_ID.'/></td></tr>'."\n";
 
}
?>
<tr><td colspan="2"><input type="submit" name="submit" value="add" /></td></tr>
</table>    
</form>
</body>
</html>
helloworld
Forum Newbie
Posts: 3
Joined: Tue Jul 28, 2009 9:40 am

Re: Php not inserting checkbox data in mysql

Post by helloworld »

Any ideas? I think to insert multiple info I need to use something like this

Code: Select all

 
        if (isset($_POST['cat_id', 'image_id'])) {
                $categories = "";
 
                for ($i=0; $i < count($_POST['cat_id']); $i++) {
                        $categories = $categories . $_POST['cat_id'][$i] . " "; 
                }
 
 
or a foreach?? :roll:
Post Reply