使用ajax后php无法连接数据库

所以我想删除网站上的评论,也从数据库中删除,但它只是在网站上找到。点击删除按钮后,评论消失了,但我的数据库中没有任何更改。刷新页面后,我删除的评论再次出现。 所以我想,不知何故,ajax使得php再次与MySQL数据库断开连接。使用ajax后php无法连接数据库

的jQuery:

$(".delete").each(function (index4) { 

$(this).on("click",function (event) {

$(this).parent().parent().load("../public/form/delete_comments.php", {index4:index4}, function() {

$(this).remove();

});

})

PHP:

<?php 

require_once "../../private/initialize.php";

$id = $_SESSION['id'];

$thread_clicked = isset($_POST['index4'])?$_POST['index4']:'';

$req_user = "SELECT * FROM log_in WHERE id='" .$id. "'";

$result_user = mysqli_query($db,$req_user);

$subject_user = mysqli_fetch_assoc($result_user);

$req = "DELETE FROM comments WHERE user=`" .$subject_user['account']. "` AND c_id=`" .$thread_clicked. "`";

$result = mysqli_query($db,$req);

更新:我改变了Ajax:$(this).parent().parent().load("/yyqGS/public/form/delete_comments.php",{index4:index4});

,但仍然没有做任何更改的数据库。

UPDATE:

<?php 

require_once "../../private/initialize.php";

session_start();

$id = $_SESSION['id'];

$thread_clicked = isset($_POST['index4'])?$_POST['index4']:'';

$thread_clicked = $thread_clicked +1;

$req_user = "SELECT * FROM log_in WHERE id='.$id.'";

$result_user = mysqli_query($db,$req_user);

$subject_user = mysqli_fetch_assoc($result_user);

$req = "DELETE FROM comments WHERE user='" .$subject_user['account']. "' AND c_id='" .$thread_clicked. "'";

$result = mysqli_query($db,$req);

if (!$req) {

printf("Error: %s\n", $mysqli_error($db));

}

else{

echo $result;

}

我得到了1次每次我删除一条评论,但是数据库依然不改!

魔法刚刚发生!我甚至不知道我做了什么(我固定引号问题),但它只是知道!

<?php 

require_once "../../private/initialize.php";

session_start();

$id = $_SESSION['id'];

$thread_clicked = isset($_POST['index4'])?$_POST['index4']:'';

$thread_clicked = $thread_clicked +1;

$req_user = "SELECT * FROM log_in WHERE id='".$id."'";

$result_user = mysqli_query($db,$req_user);

$subject_user = mysqli_fetch_assoc($result_user);

$req = "DELETE FROM comments WHERE user='" .$subject_user['account']. "' AND c_id='" .$thread_clicked. "'";

$result = mysqli_query($db,$req);

if (!$req) {

printf("Error: %s\n", $mysqli_error($db));

}

else{

echo $req;

}

?>

回答:

<?php 

require_once "../../private/initialize.php";

$id = $_SESSION['id'];

$thread_clicked = isset($_POST['index4'])?$_POST['index4']:'';

$req_user = "SELECT * FROM log_in WHERE id='$id'";

$result_user = mysqli_query($db,$req_user);

$subject_user = mysqli_fetch_assoc($result_user);

$req = $result = mysqli_query($db,$req);

$req = "DELETE FROM comments WHERE user='"$subject_user['account']"' AND c_id='"$thread_clicked"'";

$result = mysqli_query($db,$req);

尝试为你的PHP验证码。如果它工作正常,我会编辑解释。

回答:

您在删除语句中使用了错误的引号 - 您使用的后退标记用于标识字段而非环绕值。

$req = "DELETE FROM comments WHERE user=`" .$subject_user['account']. "` AND c_id=`" .$thread_clicked. "`"; 

应该

$req = "DELETE FROM comments WHERE user='" .$subject_user['account']. "' AND c_id='" .$thread_clicked. "'"; 

用单引号,而不是反引号。

编辑:也非常有用,从查询检查返回值...

if (!$result) { 

printf("Error: %s\n", $mysqli_error($db));

}

应该让你知道,如果有与删除的任何问题。

以上是 使用ajax后php无法连接数据库 的全部内容, 来源链接: utcz.com/qa/257662.html

回到顶部