php ajax评论系统没有工作查询没有插入任何东西

时间:2016-03-11 03:21:12

标签: 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";
}

?>

1 个答案:

答案 0 :(得分:0)

尝试使用

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

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