0

我有一个评论系统,所以当按下提交按钮时,它会将评论发送到数据库,然后将其添加到页面上的评论列表中。

评论.php

    <div id="comments" itemscope itemtype="http://schema.org/UserComments">
                              <?php do { ?>
            <div class="comment shadow effect">
                    <p class="left tip" title="<?php echo $row_getComments['comment_author'];?> Said">
                     <img class="avatar" src="<?php echo $row_getComments['avatarurl'];?>" /></p>
                          <p class="body right" itemprop="creator"><?php echo $row_getComments['comment_entry'];?></p>

                    <div class="details small">
                        <span class="blue"><?php echo timeBetween($row_getComments['communt_date'],time());?></span> · <a class="red" href="#" onclick="$(this).delete_comment(<?php echo $row_getComments['comment_id'];?>); return false;">Remove</a>
                    </div>
                </div>
                            <?php } while ($row_getComments = mysql_fetch_assoc($getComments)); ?>
                            </div>

    //Add Comment//
                            <div class="add_comment">
            <div class="write shadow comment">
                <p class="left">
                    <img class="avatar" src="#" />
                </p>

                <form method="POST" name="addcomment">
<p class="textarea right"><input type="hidden" name="username" value="<?php echo $_SESSION['username'];?>" />
                    <textarea class="left" cols="40" rows="5" name="post_entry"></textarea>
        <input class="left" value="SEND" type="submit" />
                   </p> </form>

            </div>
            <a onclick="$(this).add_comment(<?php echo $row_getSinglePost['post_id'];?>);return false;" class="right effect shadow" href="#">Add Comment</a>
        </div>

ajax.js- 这是它发送和接收信息的方式。我知道提交按钮有效,因为它会变灰

 jQuery.fn.add_comment = function (page_id) {
var that = $(this);

that.hide(10, function () {
    that.prev().show();
});

that.parent().find('input[type=submit]').click(function () {
    var value = $(this).prev().val();
    if (value.length < 3) {
        $(this).prev().addClass('error');
        return false;
    } else {
        var input = $(this);
        input.prev().attr('disabled', true);
        input.attr('disabled', true);
        $.post("ajax.php", {
            post_id: page_id,
            comment: value
        }, function (data) {
            if (data.error) {
                alert("Your Comment Can Not Be Posted");
            } else {
                that.parent().prev('.comments').append('<div class="comment rounded5"><p class="left"><img class="avatar" src="' + data.avatar + '" /></p><p class="body right small">' + data.comment + '<br /><div class="details small"><span class="blue">' + data.time + '</span> · <a class="red" href="#" onclick="$(this).delete_comment(' + data.id + '); return false;">Remove</a></div></p></div>');
                input.prev().val('');
            }
            input.prev().attr('disabled', false);
            input.attr('disabled', false);
        },'json');

    }
    return false;
});

};

ajax.php

require_once('connections/Main.php');
$username = $_SESSION['username'];
mysql_select_db($database_Main);
function getavatar($username){
    $result = mysql_query("SELECT profile_pic FROM `users` WHERE `username` = '$username' LIMIT 1");
    $row = mysql_fetch_row($result);
    return $row[0];
}
    if(isset($_POST['post_id']) and isset($_POST['comment'])){
        $post_id = intval($_POST['post_id']);
        $comment = mysql_escape_string($_POST['comment']);
        $time = time();
        $insertcom = mysql_query("INSERT INTO `blog_comments` (`author`, `post_num`, `comment_entry`, `communt_date`) VALUES ($username, '{$post_id}', '{$comment}', '{$time}')");
        if($insertcom){
            $id = mysql_insert_id();
            exit(json_encode(array(
                'id' => $id,
                'avatar' => getavatar($username),
                'time' => timeBetween($time, time()),
                'comment' => $comment,
            )));
        }
    }

另外,如果说查询不起作用,我将如何错误检查以提交错误?

4

2 回答 2

3
  1. 使用 echo 而不是像 Rafael 所说的那样退出。但是您不需要使用 die() 或 exit() 或其他任何东西。干净的代码不需要使用 die() 或 exit()。
  2. 您在数组中使用了 $comment 后面的逗号,但逗号后面没有条目(在我的代码中'error' => false如下。
  3. 当您询问 mysql_affected_rows 是否大于零(http://www.php.net/manual/en/function.mysql-affected-rows.php)时,您可以检查插入是否成功。mysql_affected_rows 返回受 INSERT、UPDATE 和 DELETE 影响的行。

代码:

if($insertcom){
    //Check if INSERT was successfully
    if(mysql_affected_rows() > 0) {
        $id = mysql_insert_id();
        echo(json_encode(array(
            'id' => $id,
            'avatar' => getavatar($username),
            'time' => timeBetween($time, time()),
            'comment' => $comment, //here is the ,
            //Added line to state if error
            'error' => false
        )));
    } else {
        //Echo to return error when INSERT was unsuccessful
        echo(json_encode(array('error' => true)));
    }
}
于 2012-07-14T15:51:13.560 回答
0

使用 echo() 而不是 exit(json.decode...)

此外,在此回声之后插入 die()。

于 2012-07-14T15:38:06.163 回答