0

hi guys im trying to create a ajax delete script for my gallery but it doesn't seem to be deleting, instead it gives the following errors.

Warning: Division by zero in /opt/lampp/htdocs/project/others/delete_photo.php on line 20

Warning: unlink(): No such file or directory in /opt/lampp/htdocs/project/others/delete_photo.php on line 20

the delete script is as follows:

<?php
require $_SERVER["DOCUMENT_ROOT"].'/project/includes/dbconfig.inc.php';
$session=  htmlentities($_SESSION['uname']);
$sess_uname=  stripslashes($session);
$id0=  htmlentities($_POST['id']);
$id= stripslashes($id0);
$sql="DELETE FROM photos WHERE id=':id' LIMIT 1";
$stmt=$conn->prepare($sql);
$stmt->bindparam(":id",$id);
$stmt->execute();
$sql1 = "SELECT * FROM photos WHERE user=':session' LIMIT 30";
$stmth=$conn->prepare($sql1);
$stmth->bindparam(":session",$sess_uname);
$stmth->execute();
$dir="user/$sess_uname";
$count0=$stmth->fetch(PDO::FETCH_ASSOC);
$count=count($count0);
$row1 = $stmth->fetch(PDO::FETCH_ASSOC);
if ($count>0) {
    unlink($dir/$row1['filename']);
}
 /*if (isset($_SESSION['app'])&&$_SESSION['uname']!="") {
    header("location: ../home.php?u={$_SESSION['uname']}");             
                  } else {
                      header("location: ../index.php?usernotfound?id=017");
                  }
 */

the ajax logic is as follows:

$("button.delete_photo").click(function(){

    var del_id = $(this).attr('id');
var del_attr=$(this).attr('attr');
 $.post("others/delete_photo.php",
 {id:del_id},function(data){
$("."+del_id).slideUp('slow', function() {$(this).remove(data);});

    }
 );   


});
6
  • you're missing session_start(); in the beginning of your code. Ps. $dir/$row1['filename'] does not do what you think it does. Commented Sep 27, 2015 at 7:35
  • but session_start is included in the dbconfig.inc.php @HawasKaPujaari and it prints fine with print_r(); Commented Sep 27, 2015 at 7:36
  • Your code is designed to delete one record, but your query can return multiple. Please provide the output of var_dump($row1); That way you can first confirm your query is even retrieving data. Commented Sep 27, 2015 at 7:37
  • it gives "bool(false)" @DanBelden Commented Sep 27, 2015 at 7:40
  • That would indicate your query to fetch data is not running well, slowly step back through your code using var_dump() on the variables until you find a place the code is crashing. You can then re-post or update your question with any code blockers/confusions. I'd also recommend some new line space to pad out your code for visibility sir. Commented Sep 27, 2015 at 7:43

3 Answers 3

1

Starting from the very beginning:

1) echo this and see what you get? Are you getting the desired/expected name?

echo $session=  htmlentities($_SESSION['uname']); 

If so, echo this as well:

$sess_uname=  stripslashes($session);

2) Similarly, if you're getting a expected result by echoing from:

$id0=  htmlentities($_POST['id']);

This means your first query should work.

Now, the query below works if you have already got a expected result from 1)

$sql1 = "SELECT * FROM photos WHERE user=':session' LIMIT 30";

3) This is not the correct way to assign a directory.

$dir="user/$sess_uname";

Rather use the proper concatenation like:

$dir= "user" . "/" . $sess_uname;

4) If for all that works above, this is also incorrect. Either use $_SERVER['DOCUMENT_ROOT'] to construct absolute paths beginning with the root of your website, or else use relative paths, The line written below is the reason you're getting the Warning: Division by zero:

unlink($dir/$row1['filename']);

Which rather should have been:

unlink($dir . "/" . $row1['filename']);

But that is only if $row1 is working, which at the moment clearly is not.

Sign up to request clarification or add additional context in comments.

2 Comments

the row1 is outputting bool(false) the rest of the steps seems to be working well.
@jackal4me Ahh, There's a fault in your query. Check for the proper table name along with the column names echo your query to see what's happening and if it helped you resolve your issues, dont forget to accept my answer ;-) cheers
0

Change this line

 unlink($dir/$row1['filename']);

to

 unlink($dir.'/'.$row1['filename']);

Because instead of providing directory structure, you are dividing the value.

Comments

0

finally found the solution, the problem was with the sql syntax. previously it was like:

$sql="DELETE FROM photos WHERE id=':id' LIMIT 1";

$sql1 = "SELECT * FROM photos WHERE user=':session' LIMIT 30";

i took out the single quotes for id as it was an integer and now it works like a charm. now my code looks like this:

$sql="DELETE FROM photos WHERE id=:id LIMIT 1";
$sql1 = "SELECT * FROM photos WHERE id=:id LIMIT 1";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.