We help IT Professionals succeed at work.
Get Started

Multiple Image Delete in PHP

echocpt
echocpt asked
on
329 Views
Last Modified: 2013-12-12
Hi, i have some script that another expert showed me for deleting multiple rows from tables. It works well for the original use, however i have tried to manipulate it for another delete function but with no luck.

I am trying to delete images, and the files are stored in a folder on the server and the link to them along with some other info is stored in a table.

When the user goes on the page, all images are shown in a table, with checkboxes to the left, that the user selects and then hits delete. The script attached then runs and i would like it to delete all images from the folder that have been selected and then delete all rows that have been selected.

Any help is appreciated.

Thanks Alex
<?php
$tbl_name="gallery";
$sql="SELECT * FROM `$tbl_name` ORDER BY title ASC";
$result=mysql_query($sql);
$count = mysql_num_rows($result);
?>
<form name="form1" method="post" action="">
<table width="598" border="1" bordercolor="#e5e5e5" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Image Article</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Image</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['title']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['content']; ?></td>
<td bgcolor="#FFFFFF"><img class="imgsmall" src="<?php echo "".$rows['image']."" ?>"/></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>



<?php
// Check if delete button active, start this
if(isset($_POST['delete'])) { 

// Set arrays
$id_list = array();
$cat_list = array();
// Add checked items to $id_list Array
for($i=0;$i<$count;$i++){
	$del_id = $_POST['checkbox'][$i];
	if($del_id != NULL){
		$id_list[] = $del_id;
	}
}
// Build SQL WHERE IN $id_sql
$id_sql = "";
$a = "0";
foreach ($id_list as $value) {
	if($a>0) $id_sql .= ",";
	$id_sql .= $value;
	$a++;
}
// Get Category Names
$sqla="SELECT image FROM `gallery` WHERE id IN ($id_sql)";
$resulta = mysql_query($sqla);
// Add Category Names to $cat_list Array
while($rslt = mysql_fetch_array($resulta)){
	$cat_list[] = $rslt['category'];
}
// Build SQL WHERE IN $cat_sql
$cat_sql = "";
$a = "0";
foreach ($cat_list as $value) {
	if($a>0) $cat_sql .= ",";
	$cat_sql .= "'".$value."'";
	$a++;
}

// Delete the records
$sql = "SELECT image FROM gallery WHERE image IN ($cat_sql)";
$result = mysql_query($sql);
      unlink("gallery/".$cat_sql."");
	  
$sql1 = "DELETE FROM gallery WHERE image IN ($cat_sql)";
$result1 = mysql_query($sql1);



}
?>
</table>
</form>

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE