dongyao8698 2016-05-04 14:51
浏览 78
已采纳

评论系统PHP和MySQL

I found nice comment system script on the internet, but I have no idea how to add an author to the comment. Could you help me? I tried to do it but I failed. Below is some code of the functionality of the comment system.

<?php
    function get_comments($file_id) {
        include 'database.php';
        $result = mysqli_query($connect, "SELECT * FROM `comments` WHERE `file_id`='$file_id' AND `is_child`=FALSE ORDER BY `date` DESC");
        $row_cnt = mysqli_num_rows($result);
        echo '<h1>Comments ('.$row_cnt.')</h1>';
        echo '<div class="comment">';
            new_comment();
        echo '</div>';
        foreach($result as $item) {
            $date = new dateTime($item['date']);
            $date = date_format($date, 'M j, Y | H:i:s');
            $auth = $item['author'];
            $par_code = $item['com_code'];
            $chi_result = mysqli_query($connect, "SELECT * FROM `comments` WHERE `par_code`='$par_code' AND `is_child`=TRUE");
            $chi_cnt = mysqli_num_rows($chi_result);
            echo '<div class="comment" name="'.$item['com_code'].'">'
                    .'<span class="author">'.$auth.'</span><br />'
                    .$item['comment'].'<br />'
                    .'<span class="date">Posted: '.$date.'</span><br />';
                    if($chi_cnt == 0) {
                        echo '<span class="replies">No replies</span>'
                            .'<span class="replies">&emsp;Reply</span>';
                    } else {
                        echo '<span class="replies">[+] '.$chi_cnt.' replies</span>'
                            .'<span class="replies"&emsp;Reply</span>';
                            add_comment($item['author'], $item['com_code']);
                        echo '<div name="children" id="children">';
                        foreach($chi_result as $com) {
                            $chi_date = new dateTime($com['date']);
                            $chi_date = date_format($chi_date, 'M j, Y | H:i:s');
                            echo '<div class="child" name="'.$com['com_code'].'">'
                                    .'<span class="author">'.$com['author'].'</span><br />'
                                    .$com['comment'].'<br />'
                                    .'<span class="date">Posted: '.$chi_date.'</span><br />'
                                .'</div>';
                        }
                        echo '</div>';
                    }
                echo '</div>';
        }
        mysqli_close($connect);
    }
    function add_comment($reply, $code) {
        echo '<form action="reply.php" method="post" enctpye="" name="new_comment">'
                .'<input type="hidden" name="par_code" value="'.$code.'" />'
                .'<textarea class="text_cmt" name="text_cmt" placeholder="Reply to '.$reply.'"></textarea><br />'
                .'<input type="submit" value="Reply" />'
            .'</form>';
    }
    function new_comment() {
        echo '<form action="new.php" method="post" enctpye="" name="new_comment">'
                .'<textarea class="text_cmt" name="text_cmt" placeholder="Post a new comment"></textarea><br />'
                .'<input type="submit" value="Post" />'
            .'</form>';
    }
    function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $characterLength = strlen($characters);
        $randomString = '';
        for($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $characterLength - 1)];
        }
        return $randomString;
    }
    function checkString($com_code) {
        include 'database.php';
        $rand = generateRandomString();
        $result = mysqli_query($connect, "SELECT * FROM `comments` WHERE `com_code`='$com_code'");
        $row_cnt = mysqli_num_rows($result);
        if($row_cnt != 0) {
            return $rand;
        } else {
            checkString($rand);
        }
    }
?>

And here is the function of a new comment

<?php
    require_once 'database.php';
    require_once 'functions.php';

    $reply = $_REQUEST['text_cmt'];
    $date = date('Y-m-d H:i:s');

    $rand = generateRandomString();
        #checkString($rand);

    mysqli_query($connect, "INSERT INTO `comments` (`file_id`, `comment`, `com_code`, `is_child`, `par_code`, `author`, `date`) VALUES ('1234', '$reply', '$rand', '', '', 'guest', '$date')");

    header("Location: http://orfi.uwm.edu.pl/~s119434/przepis1");
?>
  • 写回答

1条回答 默认 最新

  • doukunsan5553 2016-05-04 18:14
    关注

    Add this:

    function new_comment() {
        echo '<form action="new.php" method="post" enctpye="" name="new_comment">'
                .'<textarea class="text_cmt" name="text_cmt" placeholder="Post a new comment"></textarea><br />'
                .'<input type="text" name="text_author" /><br/>'
                .'<input type="submit" value="Post" />'
            .'</form>';
    }
    

    And this:

    $reply = $_REQUEST['text_cmt'];
    $reply_author = $_REQUEST['text_author'];
    
    ...
    
    mysqli_query($connect, "INSERT INTO `comments` (`file_id`, `comment`, `com_code`, `is_child`, `par_code`, `author`, `date`) VALUES ('1234', '$reply', '$rand', '', '', '$reply_author', '$date')");
    

    It will probably work, but it's hard to know without knowing the entire application.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效