We help IT Professionals succeed at work.

Multiple Image Delete in PHP

echocpt
echocpt asked
on
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:
get the image file name from DB with respect to id for deletion...

I think the "image" after the  "// Delete the records" part is the image name..

if so, use following code part  :

// Delete the records
$sql = "SELECT image FROM gallery WHERE image IN ($cat_sql)";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
       unlink("relative/path/of/files/".$row['image']);
}


Hope this helps

--
YRuZ!
      unlink("gallery/".$cat_sql."");

Author

Commented:
Hi, thanks that helped solve the problem along with me realising i had left a column in from old delete page woops.

Solution solved, code attached

Thanks Alex
<?php 
require_once'../connection.php';
require_once'functions.php';
cookie();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php displayHomeTitle(); ?> - Admin</title>
<link href="../stylesheet.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="toprun"></div id="toprun">
<div id="outer-main">
<div id="inner-bg">
<div id="inner-left">
  <div id="pagetitle"><?php displayHomeName(); ?></div>
  <div id="navbar"></div>
<div id="text">
<?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['image'];
}
// 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
$sql3 = "SELECT image FROM gallery WHERE image IN ($cat_sql)";
$result3 = mysql_query($sql3);
while($row = mysql_fetch_array($result3)){
       unlink("".$row['image']);
}
	  
$sql1 = "DELETE FROM gallery WHERE image IN ($cat_sql)";
$result1 = mysql_query($sql1);



}
?>
</table>
</form>
</div>
<br />
</div id="inner-left">
<div id="inner-right">
<span class="h9">Navigation</span><br /><br />
<?php displayMenu() ?>
</div>
</div id="inner-bg">
</div id="outermain">
</body>
</html>

Open in new window