Page 1 of 1

Can't get checkbox selections to delete SQL records with php

Posted: Sat Dec 03, 2011 10:57 pm
by orbdrums
I'm having a tough time getting this code to delete records from my SQL table. Once again, I know it's something I'm doing wrong but I can't figure it out. Everything is displayed properly, including the checkboxes and the delete button but when I select one of the checkboxes and click on the delete button the page just refreshes. I hope someone can help. Thanks.

Code: Select all

 <table>
<tr>
<td><form name="form1" method="post" action="">
<table>
<?php

if(mysql_num_rows($result) == 0) 
{
	echo "<h5 align=\"center\">","None","</h5>";
} else {
	$r_n = 1;
	while ($row = mysql_fetch_array($result)) 
	{
		$imgs_img_name = $row['img_name'];
		$imgs_img_desc = $row['img_desc']; 
		$imgs_img_date = $row['img_date'];
		$imgs_img_date = strtotime( $imgs_img_date );
		$imgs_img_date = date( 'm-d-Y', $imgs_img_date );
		?>
		
		<h7>Delete? 
		<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['img_name']; ?>"><br />

		<?php
		echo substr($imgs_img_name, 11, 50),"</h7>";
		echo "<img align=\"center\" src=\"/uploader/uploaded_files/$imgs_img_name\" HEIGHT = \"75\" WIDTH = \"125\">";
		echo "<h5 align=\"right\">";
		echo $imgs_img_desc,"<br />","</h5>";
		echo "<h4 align=\"left\">","Uploaded: ",$imgs_img_date,"</h4><br />";
					
		$r_n++;
	}

	echo "<input name=\"delete\" align=\"center\" type=\"submit\" id=\"delete\" value=\"Delete\">";

	if($delete)
	{

		for($i=0;$i<$count;$i++)
		{
			$del_id = $checkbox[$i];
			$sql = "DELETE FROM Images.$curr_mbr WHERE `img_name`='$del_id'";
			$result = mysql_query($sql);
		}

		if($result)
		{
			echo "<meta http-equiv=\"refresh\" content=\"0;URL=member-profile.php\">";
		}
	}
}
mysql_free_result($result);
?>

</table>
</form>
</td>
</tr>
</table>

Re: Can't get checkbox selections to delete SQL records with

Posted: Sun Dec 04, 2011 6:04 am
by maxx99

Code: Select all

$del_id = $checkbox[$i];
$sql = "DELETE FROM Images.$curr_mbr WHERE `img_name`='$del_id'";
$result = mysql_query($sql);
add:

Code: Select all

echo $sql;
Post the results ;)

Re: Can't get checkbox selections to delete SQL records with

Posted: Sun Dec 04, 2011 7:21 am
by twinedev
Also, hopefully you are manually assigning $delete and more importantly $checkbox and validating the information contained in them. Would suck for someone to just come along and post $checkbox[] = % There goes all your records.

-Greg

Re: Can't get checkbox selections to delete SQL records with

Posted: Sun Dec 04, 2011 8:00 pm
by orbdrums
echo $sql; displays nothing.
I can completely re-write this code but to give you an idea of what I'm trying to do, in the "while" loop I display images from a table that is specific to a user. I also display a checkbox to allow the user to decide which images to delete. The delete button is at the bottom of the images. I am lost when it comes to how the checkboxes and delete button are processed. I know how to validate forms but I don't know what to do when it comes to checkboxes. I can create a "processor" file to delete the server image files and the SQL records but I don't know what to look for in the "processor" file. In other words, how do I store the checkboxes that have been checked? And where do I store that information in the "while" loop? I'm still new at this so please be patient with me and thanks for your help.

Clark