Dominique
Dominique

Reputation: 309

Identifying and connecting ID's between tables

I have two tables - one is storing article content and the other is storing article comments

The function I use to display these is:

function list_articles() { 
    include('core/db/db_connection.php');
    $sql = "SELECT blog.content_id, blog.title, blog.content, blog.posted_by, blog.date, article_comments.comments, article_comments.comment_by
            FROM blog LEFT OUTER JOIN article_comments
            ON blog.content_id = article_comments.blog_id
            WHERE blog.content != ''
            ORDER BY blog.content_id DESC";
    $result = mysqli_query($dbCon, $sql);
    
    $previous_blog_id = 0;
    
    while ($row = mysqli_fetch_array($result)) {
        if ($previous_blog_id != $row['content_id']) {
            echo "<h5 class='posted_by'>Posted by {$row['posted_by']} on {$row['date']}</h5> 
                <h1 class='content_headers'>{$row['title']}</h1> 
                <article>{$row['content']}</article>
                <hr class='artline'>";
            $previous_blog_id = $row['content_id'];
        }
        if (!empty($row['comment_by']) && !empty($row['comments'])) {
             echo "<div class='commented_by'>Posted by: {$row['comment_by']} </div> 
                   <div class='comments'>Comments: {$row['comments']}</div>
                   <hr class='artline2'>";
        }
    }
}

I use the below to insert comments into article_comments table:

function insert_comments($comments, $comment_by, $blog_id) {
    include('core/db/db_connection.php');
    $comment_by = sanitize($comment_by);
    $comments = sanitize($comments);
    $sql = "INSERT INTO article_comments (comments, comment_by, blog_id)
            VALUES ('$comments', '$comment_by', '$blog_id')";
    mysqli_query($dbCon, $sql);
}

This works - it does the insertion, however I have no clue on how I could target the $blog_id variable when the user submits the post... The below is the form I use

<?php echo list_articles(); 
    if (!empty($_POST)) {
        insert_comments($_POST['comments'], $_POST['username'], 11);
        }
?>
<form method='post' action='' class='comments_form'>
    <input type='text' name='username' placeholder='your name... *' id='name'>
    <textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea> 
    <input type='submit' name='submit' id='post' value='post'>
</form>

I bet you noticed that I've manually inserted 11 as a param for the last variable. This links to blog_id 11 (the foreign key) in my article_comments table. It is displaying the comment just fine.

Is there any way to target $blog_id without having to insert a number manually? Something like how I am targeting the $comments variable using $_POST['comments'] ?

Also, even if I can target that, how do I know which post is the user commenting to? Should I give them the option to choose in a drop-down list ? That seems awkward.. but it's the only solution I can think of.

EDIT: My attempt at targeting blog_id in a hidden field:

function list_articles() { 
    include('core/db/db_connection.php');
    $sql = "SELECT blog.content_id, blog.title, blog.content, blog.posted_by, blog.date, article_comments.comments, article_comments.comment_by
            FROM blog LEFT OUTER JOIN article_comments
            ON blog.content_id = article_comments.blog_id
            WHERE blog.content != ''
            ORDER BY blog.content_id DESC";
    $result = mysqli_query($dbCon, $sql);
    
    $previous_blog_id = 0;
    
    while ($row = mysqli_fetch_array($result)) {
        if ($previous_blog_id != $row['content_id']) {
            echo "<h5 class='posted_by'>Posted by {$row['posted_by']} on {$row['date']}</h5> 
                <h1 class='content_headers'>{$row['title']}</h1> 
                <article>{$row['content']}</article>
                <hr class='artline'>";
            $previous_blog_id = $row['content_id'];
        }
        if (!empty($row['comment_by']) && !empty($row['comments'])) {
             echo "<div class='commented_by'>Posted by: {$row['comment_by']} </div> 
                   <div class='comments'>Comment: {$row['comments']}</div>
                   <hr class='artline2'>";
        }
        $sql2 = "SELECT FROM article_comments VALUES blog_id";
        $result2 = mysqli_query($dbCon, $sql2);
        while ($row = mysqli_fetch_assoc($result2)) {
            echo "  <form method='post' action='' class='comments_form'>
                        <input type='text' name='username' placeholder='your name... *' id='name'>
                        <textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea> 
                        <input type='hidden' name=blog_id' value='{$row['blog_id']}'>
                        <input type='submit' name='submit' id='post' value='post'>
                    </form>";
        }                
    }
}

sql2 and result2 part are the statements that are causing the error

EDIT 2:

I don't think the $sql2 is the correct approach. Code works fine now, but I'm back to square 1. For each comment inserted articles get duplicated.

<form method='post' action='' class='comments_form'>
    <input type='text' name='username' placeholder='your name... *' id='name'>
    <textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea> 
    <input type='hidden' name=blog_id' value='{$row['blog_id']}'>
    <input type='submit' name='submit' id='post' value='post'>
</form>";

is there any way to target the blog_id without calling while ($row = mysqli_fetch_array($result)) {} ? or at least, not calling it in the second while loop?

With the first piece of code I posted I get the following results:

Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
-------------------------------------- 
Name: DSK
Comment: Great article!
--------------------------------------
Name: DSK
Comment: Great article! - 2nd comment 

-- BEGIN SECOND ARTICLE ON WEBPAGE 

Article title: LOREM IPSUM 2nd article
Content: LOREM IPSUM DOLOR SIT AMET....
-------------------------------------- 
Name: User0
Comment: Great article!
--------------------------------------
Name: User1
Comment: Great article! - 2nd comment 
--------------------------------------
Name: User2
Comment: Great article! - 3rd comment
-------------------------------------- 

Which is exactly what I'm looking for. However I can only insert comments via the phpmyadmin interface, manually selecting the foreign key(blog_id).

I would like to be able to get the same results through a form:

Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
-------------------------------------- //comments
Name: DSK
Comment: Great article!
--------------------------------------
Name: DSK
Comment: Great article! - 2nd comment 
-------------------------------------- // end comments

|-------------------------------------| // comments form
|Name: New User                       |
|Comment: New comment !               |
|                                     | 
|-------------------------------------|
[Submit]

When the user submits the form, his name and his comment gets submitted to the database into article_comments table. Also the foreign key (blog_id) should link to an existing article (which it does). I just need a way to target it in my function.

Does that make any sense?....

Upvotes: 3

Views: 55

Answers (1)

David Kaplan
David Kaplan

Reputation: 150

Perhaps you could use a hidden form element inside your form:

<input type="hidden" name="blog_id" value="<?PHP echo $id;?>">

Then on submit you could access it with $_POST["blog_id"]

If I understood your question correctly that is.

Upvotes: 1

Related Questions