php ajax评论系统不起作用查询不插入任何内容

标签 php mysql ajax

我的评论系统使用以下代码 当我输入评论和名称并单击提交按钮时,我什么也没得到没有错误或警告没有插入任何内容有人可以找到代码有什么问题

index.php

<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script type="text/javascript">
            function sendPost() {
                var comment = document.getElementById("comment").value;
                var name = document.getElementById("username").value;
                if (comment && name) {
                    $.ajax
                    ({
                        type: 'POST',
                        url: 'post_comment.php',
                        data: {
                            user_comm: comment,
                            user_name: name
                        },
                        success: function (response) {
                            console.log(response);
                            document.getElementById("all_comments").innerHTML = response + document.getElementById("all_comments").innerHTML;
                            document.getElementById("comment").value = "";
                            document.getElementById("username").value = "";
                        }
                    });
                }
                return false;
            }
    </script>
</head>
<body>
<form method='post' action="" onsubmit="return sendPost();">
    <textarea id="comment" name="comment" placeholder="Write Your Comment Here....."></textarea>
    <input type="text" id="username" name="username" placeholder="Your Name">
    <input type="submit" value="Post Comment">
</form>
<div align="center" id="all_comments">
    <?php
    include_once 'dbConfig.php';
    $comm = $connect->query("select name,comment,post_time from comments order by post_time desc");
    while($row=$comm->fetch_array(MYSQLI_ASSOC))
    {
        $name=$row['name'];
        $comment=$row['comment'];
        $time=$row['post_time'];
        ?>
        <div class="comment_div">
            <p class="name">Posted By:<?php echo $name;?></p>
            <p class="comment"><?php echo $comment;?></p>
            <p class="time"><?php echo $time;?></p>
        </div>
        <?php
    }
    ?>
</div>
</body>
</html>

post_comment.php

<?php
include_once 'dbConfig.php';
if(isset($_POST['user_comm']) && isset($_POST['user_name']))
{
    $comment=$_POST['user_comm'];
    $name=$_POST['user_name'];
    $insert=$connect->query("insert into comments values('','$name','$comment',CURRENT_TIMESTAMP)");
    $id= $connect->insert_id;
    $select=$connect->query("select name,comment,post_time from comments where name='$name' and comment='$comment' and id='$id'");
    if($row=$select->fetch_array(MYSQLI_ASSOC))
    {
        $name=$row['name'];
        $comment=$row['comment'];
        $time=$row['post_time'];
        ?>
        <div class="comment_div">
            <p class="name">Posted By:<?php echo $name;?></p>
            <p class="comment"><?php echo $comment;?></p>
            <p class="time"><?php echo $time;?></p>
        </div>
        <?php
    }
    exit;
}
else{
    echo "No Data Is set";
}

?>

最佳答案

尝试使用

data: {
    "user_comm": comment,
    "user_name": name
}

并且您的插入查询缺少列名称:

/* JUST REPLACE THE NECESSARY COLUMN NAME */
$insert = $connect->query("INSERT INTO comments (column1, column2, column3, column4) VALUES ('','$name','$comment',CURRENT_TIMESTAMP)");

要进一步排查问题,请尝试在 success: function(response){} 中使用 console.log('success'); 显示任何数据。如果它没有显示success消息,那么您的post_comment.php就有问题。

关于php ajax评论系统不起作用查询不插入任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35931592/

相关文章:

php - SQL 更新、设置、案例 : What am I doing wrong?

mysql - mysql 多个最高记录

php - 来自浏览器的两个并行请求

php - 如何查找两个不同年份和月份之间的记录?

php - 在 PHP : fast, 中加密和解密简短、有效的 HTML id 和 URL 安全

javascript - 在 Ajax 中测试 PHP 响应

php - 如何使用 PHP Ajax 编辑 Bootstrap Tokenfield 标签数据?

PHP POST 和 GET 在同一页面上

php - 加速 MySQL 查询,尝试了普通查询和连接 - 速度相同

php中的javascript数组在提交时使用Ajax不起作用