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

时间:2017-12-02 18:19:59

标签: php jquery mysql ajax

所以我试图删除网站上的评论以及数据库中的评论,但它只是在网站上查找。单击“删除”按钮后,注释消失,但数据库中没有任何更改。刷新页面后,我删除的评论再次出现。 所以我想,不知何故,ajax让php断开与MySQL数据库的连接。

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});

但仍未对数据库进行任何更改。

更新:

  <?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;
}
?>

2 个答案:

答案 0 :(得分:0)

<?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试试这个代码。如果它工作正常,我将编辑解释。

答案 1 :(得分:0)

您在删除语句中使用了错误的引号 - 您使用的返回标记用于标识字段,而不是环绕值。

$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));
}

如果删除有任何问题,请告知您。