嗨,大家好我即將嘗試創建一個ajax刪除腳本爲我的畫廊,但它似乎並沒有刪除,而是它給出了以下錯誤。刪除照片腳本給出的錯誤在php和mysql
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
刪除腳本如下:
<?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");
}
*/
的AJAX邏輯如下:
$("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);});
}
);
});
你錯過了'在session_start();'在你的代碼的開頭。 PS。 '$ dir/$ row1 ['filename']'不會做你認爲它做的事。 – user5173426
但是session_start包含在dbconfig.inc.php @HawasKaPujaari中,並且它使用print_r()正確打印。 – jackal4me
您的代碼旨在刪除一條記錄,但您的查詢可能會返回多條記錄。請提供var_dump($ row1)的輸出;這樣你可以首先確認你的查詢甚至是檢索數據。 –