lundi 20 avril 2015

Inserting Comments form into mysql and connecting with Messages id. And displaying on profile page

I am working to insert user comments into mySQL. I need each comment to appear below the message being commented on. A very basic Facebook wall. I have the comment going into the database but I'm stuck on how to connect the comment to the message id in that query. I then also need to take the comment out of the database and show it on the profile page below the message that is being commented on.

Here is my current query that is posting to the database (I'm not sure what to associate 'message_id' with in the VALUES part of the query):

function post_comment($post)
    {
        $query = "INSERT INTO comments (user_id, message_id, comment,     created_at, updated_at)
                  VALUES ('{$_SESSION['user_id']}', ''   '{$post['comment']}', NOW(), NOW())";
                  run_mysql_query($query);
                  // header('location: profile.php');
    }

My SQL tables and its columns are:

users = id, first_name, last_name, email, password, created_at, updated_at
messages = id, user_id, message, created_at, updated_at
comments = id, message_id, user_id, comment, created_at, updated_at

My query that I know is not working to update the page is:

SELECT user_id, first_name, last_name, message, comment,
comment.created_at,
FROM users
LEFT JOIN messages
WHERE users.id = messages.user_id
LEFT JOIN comments
WHERE messages.id = comments.message_id

Aucun commentaire:

Enregistrer un commentaire