Jump to content

[SOLVED] Inserting many values using a loop


littlevisuals

Recommended Posts

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  :confused:

 

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>

 

 

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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  :)

 

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.