c语言编程笔录

您现在的位置是:首页 > 编程 > 编程经验

编程经验

使用PHP实现问题答案评论和点赞功能

林雅南 2023-09-10编程经验
首先,我们需要创建问题答案评论和点赞功能的数据库表。我们可以创建如下的`answers`和`comments`表和`likes`表:answers表CREATE
首先,我们需要创建问题答案评论和点赞功能的数据库表。我们可以创建如下的`answers`和`comments`表和`likes`表:

answers表

CREATE TABLE answers (
    id INT(11) NOT NULL AUTO_INCREMENT,
    question_id INT(11) NOT NULL,
    user_id INT(11) NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    FOREIGN KEY (question_id) REFERENCES questions(id) ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE
);

comments表

CREATE TABLE comments (
    id INT(11) NOT NULL AUTO_INCREMENT,
    answer_id INT(11) NOT NULL,
    user_id INT(11) NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    FOREIGN KEY (answer_id) REFERENCES answers(id) ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE
);

likes表

CREATE TABLE likes (
    id INT(11) NOT NULL AUTO_INCREMENT,
    answer_id INT(11) NOT NULL,
    user_id INT(11) NOT NULL,
    like_status TINYINT(1) NOT NULL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    FOREIGN KEY (answer_id) REFERENCES answers(id) ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE
);
我们使用了外键和多对一的关系来实现问题、答案、评论和点赞之间的关联。 接下来,我们编写PHP代码,实现问题答案的评论和点赞功能。首先是问题答案的评论功能:

问题答案评论功能示例代码

if(isset($_POST['submit_comment'])){
    $answer_id = $_POST['answer_id'];
    $user_id = $_SESSION['user_id'];
    $content = $_POST['comment'];

    $query = "INSERT INTO comments (answer_id, user_id, content) VALUES ('$answer_id', '$user_id', '$content')";
    $result = mysqli_query($conn, $query);

    if($result){
        header("Location: answer.php?id=".$answer_id);
        exit();
    }else{
        echo "评论失败。";
    }
}
这段代码首先检查用户是否提交了评论表单,然后获取答案ID、用户ID和评论内容,并将它们插入到`comments`表中。如果插入成功,则重定向到答案页面,否则显示错误消息。 接下来是问题答案的点赞功能:

问题答案点赞功能示例代码

if(isset($_POST['submit_like'])){
    $answer_id = $_POST['answer_id'];
    $user_id = $_SESSION['user_id'];
    $status = $_POST['like_status'];

    $query = "INSERT INTO likes (answer_id, user_id, like_status) VALUES ('$answer_id', '$user_id', '$status') ON DUPLICATE KEY UPDATE like_status='$status'";
    $result = mysqli_query($conn, $query);

    if($result){
        header("Location: answer.php?id=".$answer_id);
        exit();
    }else{
        echo "点赞失败。";
    }
}
这段代码也首先检查用户是否提交了点赞表单,然后获取答案ID、用户ID和点赞状态,并将它们插入到`likes`表中。如果该用户已经点过赞,那么就更新点赞状态。如果插入或更新成功,则重定向到答案页面,否则显示错误消息。 最后,我们还需要查询数据库,显示问题答案的评论和点赞数量。以下是一个简单的示例代码:

查询问题答案的评论和点赞数量示例代码

//查询问题答案及其评论数量
$query = "SELECT a.*, COUNT(c.id) AS comment_count FROM answers a LEFT JOIN comments c ON a.id=c.answer_id WHERE a.id='$answer_id' GROUP BY a.id";
$result = mysqli_query($conn, $query);
$answer = mysqli_fetch_assoc($result);

//查询问题答案的点赞数量
$query = "SELECT COUNT(*) AS like_count FROM likes WHERE answer_id='$answer_id' AND like_status='1'";
$result = mysqli_query($conn, $query);
$likes = mysqli_fetch_assoc($result);

echo "评论:".$answer['comment_count']."
"; echo "点赞:".$likes['like_count'];
这段代码查询特定答案的评论数和点赞数,并显示它们。 总之,我们可以使用PHP和MySQL实现问题答案的评论和点赞功能。我们需要在数据库中创建相关表格,并编写代码,以便用户可以提交评论和点赞,并查询已有的评论和点赞数量。

文章评论