0

I'm trying to make my webpage delete some database entries and with that also a picture uploaded to ftp with the name set into the database row called filename.

So far I got the delete function working for database entries with a checkbox form so you can delete multiple entries at once, but I need to make it the way that also the image from filename is also removed from the ftp (content/uploads).

Been trying some things I found on the internet but couldn't get it working, most of the errors I got removed but I keep on getting these last few errors in my script:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /sites/mianautoservice.nl/www/delete_multiple.php on line 10 Warning: ftp_delete(): content/uploads/: No such file or directory in /sites/mianautoservice.nl/www/delete_multiple.php on line 23 could not delete content/uploads/

I think the error on line 23 is because it tries to get the results from the delete function before pressing delete but I just can't seem to figure it out...

This is my script so far:

<?php error_reporting(E_ALL); ini_set('display_errors', 1);
// Get our database connector
require("includes/conn.php");
?>
<?
if(isset($_POST['delete'])){ // Check delete button is clicked
  foreach($_POST['checkbox'] as $del_id){ // loop only checked items and delete
   $sql = "DELETE FROM people WHERE id='$del_id'"; 
$result = mysql_query($sql);
while($rows=mysql_fetch_array($result)){
}
$file = "content/uploads/" . $rows['filename'];
$ftp_server = "ftpserver";
$ftp_user_name = "username;
$ftp_user_pass = "password";
// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to delete $file
if (ftp_delete($conn_id, $file)) {
 echo "$file deleted successful\n";
} else {
 echo "could not delete $file\n";
}

// close the connection
ftp_close($conn_id);

   $result = mysql_query($sql);
  }
}
// if successful redirect to delete_multiple.php 
if($result){
}
?>
<?php
$sql="SELECT * FROM people ORDER BY ID";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Selecteer welke auto('s) verkocht zijn</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Foto</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Merkt/Type</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Beschrijving</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){

echo "<div class=\"picture\">";
                    echo "<p>";
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo "<img src=\"content/uploads/" . $rows['filename'] . "\" alt=\"\" height=\"125\" width=\"200\" /><br />" . "<br />"; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['fname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lname']; ?></td>
</tr>

<?php 
                    echo "</p>";
                    echo "</div>";
}
?>

<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 



mysql_close();
?>

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

<a href="indexupload.php" title="Back">Back</a>
Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
FGOD
  • 103
  • 2
  • 10
  • You're running `DELETE` queries, which do **NOT** return result sets. You are also vulnerable to [sql injection attacks](http://bobby-tables.com). It is literally impossible for this code to have ever worked. – Marc B Nov 19 '14 at 14:13
  • Your closing brace for your `while` loop needs to be moved. Plus, make sure that short open tags are set. If not, change `` to ` – Funk Forty Niner Nov 19 '14 at 14:15
  • no idea what you mean with the fact that i don't let the results return, cause for database entries it does work perfectly... or did you mean the ftp part? and why do you say i'm vulnerable for sql injections if i may ask as this only deletes entries, not add anything that can corrupt my database... – FGOD Nov 19 '14 at 14:20
  • @Fred-ii- it does take away the error in line 23 but for some reason it still doesn't delete the file from ftp and i still am having the error at line 10 – FGOD Nov 19 '14 at 14:23
  • Did you change `while($rows=mysql_fetch_array($result)){ } $file = "content/uploads/" . $rows['filename'];` to `while($rows=mysql_fetch_array($result)){ $file = "content/uploads/" . $rows['filename']; }`? Also `` to ` – Funk Forty Niner Nov 19 '14 at 14:25
  • @Fred-ii- sorry misplaced the closing brace, but placing it like you say leaves me with this error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /sites/mianautoservice.nl/www/delete_multiple.php on line 10 Notice: Undefined variable: file in /sites/mianautoservice.nl/www/delete_multiple.php on line 23 Warning: ftp_delete(): Invalid number of arguments in /sites/mianautoservice.nl/www/delete_multiple.php on line 23 Notice: Undefined variable: file in /sites/mianautoservice.nl/www/delete_multiple.php on line 26 could not delete – FGOD Nov 19 '14 at 14:30
  • Well, errors can mean progress (success in disguise), ones you did not get before. Your DB connection, is it `mysql_` - `mysqli_` - PDO? – Funk Forty Niner Nov 19 '14 at 14:31
  • @Fred-ii- still mysql, although i will update it to mysqli when i get everything working as i still need to learn mysqli and PDO and from what i heard it is not much different – FGOD Nov 19 '14 at 14:39
  • @Fred-ii- if i load it the page it gives no errors, but when deleting it gives me the errors – FGOD Nov 19 '14 at 15:03
  • It may have something to do with the path setting then. FTP on many systems, require something like `/var/user/www/folder/file` instead of `folder/file`. It's happened to me before. – Funk Forty Niner Nov 19 '14 at 15:26
  • @Fred-ii- changed it to your suggestion but he keeps saying something about undifined variable file on line 23 (if (ftp_delete($conn_id, $file)) {) and line 26 ( echo "could not delete $file\n";) while that variable is set on line 12 ($file = "/var/user/www/folder/content/uploads/" . $rows['filename'];}) basicly the same error as above... – FGOD Nov 20 '14 at 12:18

0 Answers0