0

am trying to build a commenting system with PHP and Mysql. I want to link one post with many comments. So I have a posts table and a comments table. The comments table has a foreign key of post_id which is similar to the posts table id column(which is the primary key in posts table). Once the admin adds a post, the post_id column is inserted with the id of that post. This means that when adding comments, someone has to update the comments table which in my case, it doesn't. Here is the update query.

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

The comment table simply fills the empty columns with NULL. I don't seem to trace where the problem is.

5
  • How do you have this setup? Each comment should be it's own row in the database. Therefore no need to update but to insert. Unless your "comments" column should actually be singular. Commented Feb 12, 2018 at 4:29
  • what is your DB driver ? what is the meaning of "The comment table simply fills the empty columns with NULL." Commented Feb 12, 2018 at 4:29
  • I am using PHPmyadmin as a local server. Commented Feb 12, 2018 at 6:06
  • I mean this, when I insert a post into the posts table as an admin, I have a separate query that inserts that id in the post_id column in the comments table. The post_id column is the foreign key. I then have a separate query that is to update the comments table when someone comments. When I run this query, it does not fill the other fields in the comment table.they are instead written as NULL Commented Feb 12, 2018 at 6:09
  • So Nick you mean that instead of updating I should simply insert using, "..WHERE post_id = sth.."? How will the comment be related to the post? Commented Feb 12, 2018 at 6:12

1 Answer 1

1

Firstly you need to Insert rather than update and use prepare statement for preventing SQL Injection Please find the code to do so

$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;
Sign up to request clarification or add additional context in comments.

5 Comments

I have done that and its not working. I think all am asking is how will the posts table be linked to the comments table so that when I search for 1 post, it will also show the comments related to it.
Are all the comments already there in a database? And you only want to fetch it ?
As it stands right now, the comments are not getting inserted.
I have updated the code. Please include your config file and check if the data is inserted or not
Thanks a lot. My main concern is in how the posts table will be related to the comments table, not inserting the data into the table. What I want is when I open a particular post, all the comments that were made on that post should appear below the post, just as blogs look like.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.