littlevisuals Posted July 29, 2009 Share Posted July 29, 2009 Hi guys, I seem to be doing something wrong. At current the script inserts a checked category number into the table images_catlu into the cat_ID field and the image_ID field. How do I modyfy it to insert multiple checked data into the table? Not in the same field, but multiple rows of all the checked data Here is the code <?php include("connect.php"); mysql_select_db("work"); $cat_ID = $_POST['cat_id']; $image_ID = $_POST['image_id']; $result = mysql_query("SELECT cat_ID FROM categories"); $result = mysql_query("SELECT image_ID FROM categories"); $query = "INSERT INTO image_catlu (cat_ID, image_ID) VALUES ('$cat_ID', '$image_ID')"; <?php if (isset($_POST['cat_id', 'image_id'])) { $categories = ""; for ($i=0; $i < count($_POST['cat_id']); $i++) { $categories = $categories . $_POST['cat_id'][$i] . " "; } $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> Quote Link to comment https://forums.phpfreaks.com/topic/167945-solved-inserting-many-values-using-a-loop/ Share on other sites More sharing options...
MadTechie Posted July 29, 2009 Share Posted July 29, 2009 your need to change the html so the checkbox becomes an array change the name value like name="cat_id[]" then of course $_POST['cat_id']; will also be an array, so you can loop that, for example foreach($_POST['cat_id'] as $cat_ID) { $query = "INSERT INTO image_catlu (cat_ID, image_ID) VALUES ('$cat_ID', '$image_ID')"; $results = mysql_query($query) } however i have no idea where $image_ID is coming from or what your attempting to do here $sql = "SELECT id,cat FROM catorgories ORDER by id ASC"; "SELECT id,id FROM gallery"; Quote Link to comment https://forums.phpfreaks.com/topic/167945-solved-inserting-many-values-using-a-loop/#findComment-885762 Share on other sites More sharing options...
littlevisuals Posted July 29, 2009 Author Share Posted July 29, 2009 Thankyou for the reply, this is a snippet of the whole script it belongs to. The rest of the form is for uploading an image then adding details like title, description etc and I wanted to have multiple categories with them The image_ID will be a value when the rest of the form is processed so in image_catlu I have something like this cat_ID Image_ID 5 107 6 107 5 108 Then the image can have multiple cats. Im just going to make the changes you have posted Quote Link to comment https://forums.phpfreaks.com/topic/167945-solved-inserting-many-values-using-a-loop/#findComment-885766 Share on other sites More sharing options...
littlevisuals Posted July 29, 2009 Author Share Posted July 29, 2009 Hi MadTechie, Have tried the following and I get Parse error: syntax error, unexpected '}' in /Library/WebServer/Documents/checkbox.php on line 14 <?php include("connect.php"); mysql_select_db("work"); $cat_ID = $_POST['cat_id']; $image_ID = $_POST['image_id']; $result = mysql_query("SELECT cat_ID FROM categories"); $result = mysql_query("SELECT image_ID FROM categories"); foreach($_POST['cat_id'] as $cat_ID) { $query = "INSERT INTO image_catlu (cat_ID, image_ID) VALUES ('$cat_ID', '$image_ID')"; $results = mysql_query($query) } $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 categories 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> Quote Link to comment https://forums.phpfreaks.com/topic/167945-solved-inserting-many-values-using-a-loop/#findComment-885771 Share on other sites More sharing options...
MadTechie Posted July 29, 2009 Share Posted July 29, 2009 your missing a semi-colon $results = mysql_query($query); //<--here Quote Link to comment https://forums.phpfreaks.com/topic/167945-solved-inserting-many-values-using-a-loop/#findComment-885773 Share on other sites More sharing options...
littlevisuals Posted July 29, 2009 Author Share Posted July 29, 2009 Sorry I realised that straight after posting. Ran the script and works like a dream! Thankyou once again your time and effort. Quote Link to comment https://forums.phpfreaks.com/topic/167945-solved-inserting-many-values-using-a-loop/#findComment-885777 Share on other sites More sharing options...
MadTechie Posted July 29, 2009 Share Posted July 29, 2009 Well technically I forgot but i never test my code here, and your welcome Quote Link to comment https://forums.phpfreaks.com/topic/167945-solved-inserting-many-values-using-a-loop/#findComment-885779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.