richarro1234 Posted January 6, 2009 Share Posted January 6, 2009 hey all, I have thispeice of code that displays images on a wenpage for approval, it all works fine for single file deletion, but as soon as you select more then one image to be deleted it doesnt work. At the moment it will delete the file from the server and delete the imagefrom the database, but multiples wont seem to work. Can someone help me as to why it wont work. Thanks <?include("header.php");?> <? if ($_GET['note'] == 'delsucmulti') { $note = "<br><center> <table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' width='75%' id='AutoNumber1' bordercolor='red' bgcolor='#000000'> <tr> <td width='100%' class=trnext align='center'><font color='red' size='2'><b>Your messages have been deleted successfully!</b></font></td> </tr> </table> </center><br>"; } include("data.php"); mysql_connect($server,$anvandare, $losen); mysql_select_db($databas); $query = mysql_query("SELECT * from users WHERE username = '".$_COOKIE["twusername"]."'"); while ($r = @mysql_fetch_array($query)) { $approv = $r['id'];?> <? if ($_GET['action'] == 'deletemulti') { $delete = $_POST["file_name"]; $deleted_items = join(', ', $_POST["deleted_items"]); $deleted_items1 = join(', ', $_POST["deleted_items1"]); chdir('../userimages/'); $do = unlink($deleted_items); if($do=="1"){ echo "The file was deleted successfully."; } else { echo "There was an error trying to delete the file."; } $query = mysql_query("DELETE FROM userimg WHERE id = ($deleted_items1)"); header ("Location: approveimg.php?note=delsucmulti"); die(); } ?> <?}?> <center> <?=$note?></center> <br> <div align="center"> <center> <div align="center"> <center> <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#5D6765" width="95%" id="AutoNumber1" bgcolor="#131313"> <tr> <td width="5%" align="center" bgcolor="#000000"><b>Delete</b></td> <td width="16%" align="center" bgcolor="#000000"><b>Picture</b></td> <td width="16%" align="center" bgcolor="#000000"><b>User Edited</b></td> <td width="16%" align="center" bgcolor="#000000"><b>Value Edited</b></td> <td width="16%" align="center" bgcolor="#000000"><b>Original Value</b></td> <td width="15%" align="center" bgcolor="#000000"><b>New Value</b></td> <td width="16%" align="center" bgcolor="#000000"><b>Date</b></td> </tr> <tr> <td width="100%" align="center" colspan="6"> </td> </tr> <form action="approveimg.php?action=deletemulti" name="approv" method="post"> <? $results= mysql_query("SELECT * FROM userimg WHERE approved = 'no'"); $id = "id"; $filename = "file_name"; $filesize = "filesize"; $filetype = "filetype"; $desc = "description"; $ownerid = "ownerid"; echo mysql_error(); if (mysql_Numrows($results)>0) //if there are records in the fields { $numrows=mysql_NumRows($results); //count them $x=0; while ($x<$numrows){ //loop through the records $theid=mysql_result($results,$x,$id); $thename=mysql_result($results,$x,$filename); $thesize=mysql_result($results,$x,$filesize); $theowner=mysql_result($results,$x,$ownerid); $thetype=mysql_result($results,$x,$filetype); $thedesc=mysql_result($results,$x,$desc); $thename1 = "<img src='../userimages/$thename' height='100' width='100'>"; ?> <tr> <input type="hidden" class="normal" value="<?=$theid;?>" name="deleted_items1[]"> <td width="5%" align="center"><input type="checkbox" class="normal" value="<?=$thename?>" name="deleted_items[]"> <td width="16%" align="center"><a href="../userimages/<?=$thename;?>"><img src="..//userimages/<?=$thename?>" width="100" height="100"></a> </td> <td width="16%" align="center"><a href="profile.php?id=<?=$memlink['id'];?>"><?=$theusername?></a> </td> <td width="16%" align="center"><?=$theaction?> </td> <td width="16%" align="center"><?=$theorigvalue?> </td> <td width="15%" align="center"><?=$thenewvalue?> </td> <td width="16%" align="center"><?=$logdate?> </td> </tr> <? $x++; } ?> <td width="5%" align="center" bgcolor="#000000"><input type="submit" value="DEL"></td> <td width="95%" align="center" colspan="3" bgcolor="#000000"><font size="2"><b><a href="#" onclick="setCheckboxes('approv', true); return false;">Select All</a> | <a href="#" onclick="setCheckboxes('approv', false); return false;">Unselect All</a></b></font></td> </form </table> </center> </div> <?php } else { ?> </center> <center><b>There Are No New Images to Approve!</b></center> <br> <? } ?> </td> </tr> </table> </center> </div> <br> <?include("footer.php");?> Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/ Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 To unlink and item, I think you will have to loop through each individual record. I do not tink you can just seperate each file by a comma and have it work. $deleted_items = $_POST["deleted_items"]; foreach ($deleted_items as $filename) unlink($filename); Unsure if that will work since I cannot see the form, but yea. Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730413 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 yea that deleltes the files form the server, but it wont delete it form the database, thats what the comma seperated was for. Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730419 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 Ah, sorry mis-read. $query = mysql_query("DELETE FROM userimg WHERE id IN ($deleted_items1)"); Use the IN() operator. Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730420 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 well that deletes the images still, but it doesnt delete from the database, it just gives me this erro: There was an error trying to delete the file.Error: Unknown column 'Array' in 'where clause' Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730425 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 Do a print_r on the $deleted_items1 value. Maybe try using implode instead of join (doubt it matters but yea). It should join them together, not sure why it isn't without seeing the actual form... Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730427 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 ill try the print and report back, the form is under the second sql bit. <form action="approveimg.php?action=deletemulti" name="approv" method="post"> <? $results= mysql_query("SELECT * FROM userimg WHERE approved = 'no'"); $id = "id"; $filename = "file_name"; $filesize = "filesize"; $filetype = "filetype"; $desc = "description"; $ownerid = "ownerid"; echo mysql_error(); if (mysql_Numrows($results)>0) //if there are records in the fields { $numrows=mysql_NumRows($results); //count them $x=0; while ($x<$numrows){ //loop through the records $theid=mysql_result($results,$x,$id); $thename=mysql_result($results,$x,$filename); $thesize=mysql_result($results,$x,$filesize); $theowner=mysql_result($results,$x,$ownerid); $thetype=mysql_result($results,$x,$filetype); $thedesc=mysql_result($results,$x,$desc); $thename1 = "<img src='../userimages/$thename' height='100' width='100'>"; ?> <tr> <input type="hidden" class="normal" value="<?=$theid;?>" name="deleted_items1[]"> <td width="5%" align="center"><input type="checkbox" class="normal" value="<?=$thename?>" name="deleted_items[]"> <td width="16%" align="center"><a href="../userimages/<?=$thename;?>"><img src="..//userimages/<?=$thename?>" width="100" height="100"></a> </td> <td width="16%" align="center"><a href="profile.php?id=<?=$memlink['id'];?>"><?=$theusername?></a> </td> <td width="16%" align="center"><?=$theaction?> </td> <td width="16%" align="center"><?=$theorigvalue?> </td> <td width="15%" align="center"><?=$thenewvalue?> </td> <td width="16%" align="center"><?=$logdate?> </td> </tr> <? $x++; } ?> <td width="5%" align="center" bgcolor="#000000"><input type="submit" value="DEL"></td> <td width="95%" align="center" colspan="3" bgcolor="#000000"><font size="2"><b><a href="#" onclick="setCheckboxes('approv', true); return false;">Select All</a> | <a href="#" onclick="setCheckboxes('approv', false); return false;">Unselect All</a></b></font></td> </form> Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730432 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 this is what i got after running the script again: Array ( [0] => 28 [1] => 26 [2] => 27 [3] => 25 [4] => 19 ) Array ( [0] => f7d1b05822e3ad95d7bb4b617306f7e0.jpg [1] => 7610e445642b00a2ccca976e324ed276.jpg ) Warning: unlink(28) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27 Warning: unlink(26) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27 Warning: unlink(27) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27 Warning: unlink(25) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27 Warning: unlink(19) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27 There was an error trying to delete the file.Error: Unknown column 'Array' in 'where clause' Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730435 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 I am not sure why you are only pulling 2 images when you have 5 ids, but the array seems fine. implode should work on it. Why it is not I have no clue.... Weird stuff....I could be missing something cause I am tried, but yea. Hopefully someone can see the error and help ya. Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730445 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 yea i think thats why i cant find it either lol. well thanks for your help, ill leave it open and hope someone else will see what we cant see. Thanks Rich Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730447 Share on other sites More sharing options...
redarrow Posted January 6, 2009 Share Posted January 6, 2009 When i read the code i go all light headed, I dont understand that code at all, very badly written. i can see what your doing but using sessions so much easier to, control your code structure. all them loops are causing havoc man. Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730449 Share on other sites More sharing options...
redarrow Posted January 6, 2009 Share Posted January 6, 2009 is this valid if (mysql_Numrows($results)>0) this is valid i thort. mysql_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730453 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 is this valid if (mysql_Numrows($results)>0) this is valid i thort. mysql_num_rows They both work...mysql_numrows Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730456 Share on other sites More sharing options...
redarrow Posted January 6, 2009 Share Posted January 6, 2009 try the other way please. Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730463 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 try the other way please. It definitely is not that, it is something with the implode function or how the form is being posted to the script. It is getting skewed cause the value isn't imploding properly to convert it to a string instead of an array. Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730466 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 that wopuld only change the display of it if anything, it wouldnt change the way it handles the php above it (the deletemulti action) Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730475 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 someone help me please. i still cant get it working. Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730544 Share on other sites More sharing options...
timmah1 Posted January 6, 2009 Share Posted January 6, 2009 Where is the delete query? Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730551 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 <? if ($_GET['action'] == 'deletemulti') { $delete = $_POST["file_name"]; $deleted_items = join(', ', $_POST["deleted_items"]); $deleted_items1 = join(', ', $_POST["deleted_items1"]); chdir('../userimages/'); $do = unlink($deleted_items); if($do=="1"){ echo "The file was deleted successfully."; } else { echo "There was an error trying to delete the file."; } $query = mysql_query("DELETE FROM userimg WHERE id = ($deleted_items1)"); header ("Location: approveimg.php?note=delsucmulti"); die(); } ?> There Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730555 Share on other sites More sharing options...
timmah1 Posted January 6, 2009 Share Posted January 6, 2009 Would this work $delete = $_POST["file_name"]; for($i=0;$i < count($delete);$i++ ){ $deleted_items = $_POST["deleted_items"][$i]; $query= sprintf("DELETE FROM userimg WHERE id = '%s'", Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730557 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 tried it, i got this: Warning: unlink(3ffa3088b997d3bd87fbc1d7a21a0c2e.jpg, a35943485828735935b6fdb33b41f0fb.jpg, 83dbb2421835b36a457fc05d635f97d1.jpg) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 26 There was an error trying to delete the file. and yes they do exsist before someone says anything. Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730558 Share on other sites More sharing options...
richarro1234 Posted January 6, 2009 Author Share Posted January 6, 2009 Still havent fixed it yet, any one got any more suggestions? Thanks Rich Quote Link to comment https://forums.phpfreaks.com/topic/139612-unlink-help-need-please/#findComment-730702 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.