首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP评论系统

PHP评论系统
EN

Stack Overflow用户
提问于 2018-02-12 12:22:37
回答 1查看 80关注 0票数 0

我试图用PHP和Mysql建立一个评论系统。我想链接一个有很多评论的帖子。所以我有一个posts表和一个comments表。comments表有一个外键post_id,它类似于posts表的id列(这是posts表中的主键)。管理员添加帖子后,将使用该帖子的id插入post_id列。这意味着当添加注释时,某人必须更新comments表,在我的例子中,它不需要更新。

代码语言:javascript
复制
if(isset($_POST['cmt-btn'])){
if(!empty($_GET['id'])){
    $current_id = $_GET['myid'];
    echo $current_id;
    $comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
    $author = filter_var($_POST['author_name'], FILTER_SANITIZE_STRING);
    $post_id = $current_id;
    $time = now();  
    $sql = "UPDATE comments SET comments = '$comment', author = '$author', timeposted = '$time' WHERE post_id = '$current_id'";
    $result = $connection->query($sql);
}

comment表只是用NULL填充空列。我似乎不知道问题出在哪里。

EN

回答 1

Stack Overflow用户

发布于 2018-02-12 14:34:02

首先,您需要插入而不是更新,并使用prepare语句来防止SQL注入。请找到代码来执行此操作

代码语言:javascript
复制
$comment = "This is Comment";
$author = "Nikita";
$post_id = 1;
$time = "12:00:00";

function insertComment($comment,$author,$post_id,$time){
  global $mysqli;
  $stmt = $mysqli->prepare("INSERT INTO comments(
    comments,
    author,
    timeposted,
    post_id
     )VALUES (
      ?,
      ?,
      ?,
      ?
    )");
    $stmt->bind_param("ssss",$comment,$author,$post_id,$time);
    $stmt->execute();
    $inserted_id = $mysqli->insert_id;
    return $instered_id;
 }
 $sql = insertComment($comment,$author,$post_id,$time);
 echo $sql;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48739510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档