Skip to content
Advertisement

display unique comment page for each url id

I am trying to create a php comment page which shows a unique comment page for each unique url id that I have. But currently, my comment page is shared among all the url. I understand I need to add the url id(which is the bug id) to the mySql table for comments but I cant figure out a way to do so. Can anyone show me what sql insert query should I use to achieve this? thanks

How I get my dynamic url for each bug report:

<?php
$db = mysqli_connect ("localhost", "root", "", "bugreport");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
if(isset($_GET['ID'])) {
    $ID= mysqli_real_escape_string($db, $_GET['ID']);
    
    $sql= "SELECT * FROM bugreport WHERE BugID='$ID' ";
    $results=mysqli_query($db, $sql) or die("Bad Query: $sql");
    $row= mysqli_fetch_array($results);
}

if(mysqli_num_rows($result) > 0)
  {
   while($row = mysqli_fetch_object($result))
   {
    $output .= '
    <tr>
     <td><a href="commentInfo.php?ID='.$row->BugID.'">'.$row->BugID.'</td>
    </tr>';
   }
  }
?>

The code for adding the info to the comment mySql table:

$connect = new PDO('mysql:host=localhost;dbname=comment', 'root', '');
if($error == '')
{
 $query = "
 INSERT INTO comment 
 (parent_comment_id, comment, comment_sender_name, BugID) 
 VALUES (:parent_comment_id, :comment, :comment_sender_name, ihavenoideawtfbbqtoputhere )
 ";
 
 $statement = $connect->prepare($query);
 $statement->execute(
  array(
   ':parent_comment_id' => $_POST["comment_id"],
   ':comment'    => $comment_content,
   ':comment_sender_name' => $comment_name
  )
 );

I have zero experience with php as it’s totally new to me and I am just learning as I go along from all the youtube vids/tutorials as I need it for a project of mine. But from what I understand I need help on how to insert the BugID from the bugreport table into the comments table using the INSERT query shown above. Once done I should be able to display the unique comments for each page by doing something like this:

 function load_comment()
 {
     $.ajax({
         url:"fetch_comment.php",
         data: {article_id: 0},
         method:"POST",
         success:function(data)
         {
             $('#display_comment').html(data);
         }
     })
 }

I saw the above suggestion from another question similar to mine on SO.

Advertisement

Answer

Inserting the bug id (so essentially replacing “ihavenoideawtfbbqtoputhere” works like the following).

Note that you need to get the $bug_id from somewhere. From where depends on your page setup and can’t be answered from the details you give from your question. To use the code I posted, the “BugID” has to be set in the POST request.

I assumed, that you have a Bug-page. This page shows one bug. And this bug has an id. So I assume that your bug page looks something like this:

<?php
    $db = mysqli_connect ("localhost", "root", "", "bugreport");

    $ID = intval($_GET["ID"]);
    $sql = "SELECT * FROM bugreport WHERE BugID='".$ID."';";
    $results = mysqli_query($db, $sql) or die("Bad Query: ".$sql);
    $row = mysqli_fetch_array($results);
?>
<html>
    <body>
        <h1>Bug <?php print($row->BugID); ?></h1>
        <p><?php print($row->content); ?></p>
        <form method="POST" action="comment.php">
            <h2>Comment</h2>
            <input type="hidden" name="BugID" value="<?php print($row->BugID); ?>" />
            <textarea name="comment_content"></textarea>
            ...
            <input type="submit" />
        </form>
    </body>
</html>

Then the following file (comment.php) is executed, whenever the comment is submitted. Now you know the BugID from the hidden input field.

$connect = new PDO('mysql:host=localhost;dbname=comment', 'root', '');

// don't know if this works for you
$bug_id = intval($_POST["BugID"]);

if($error == ''){
    $query = "INSERT INTO comment 
        (parent_comment_id, comment, comment_sender_name, BugID) 
        VALUES (:parent_comment_id, :comment, :comment_sender_name, :bug_id)";
 
    $statement = $connect->prepare($query);
    $statement->execute(array(
        ":parent_comment_id" => $_POST["comment_id"],
        ":comment"  => $comment_content,
        ":comment_sender_name" => $comment_name,
        ":bug_id" => $bug_id
    ));
}
else{
    print("Error");
}

Note that the user can change the hidden input field in the html. So you may want to have a server-side check, if the user can add a comment to this bug. Also note, that my code will redirect the user to a new page, the comment.php. That is very inconvenient. So you may want to look on submitting forms via ajax.

In addition you are using two different libraries to connect to your database. You use MySQLI and PDO. Both are classes/libraries to connect to databases but they are different. While (I guess) there is no error produced by this, it just is weird (in my eyes). You have to use two different code styles. Also you need to connect two database objects instead of one (which doesn’t matter at the moment because they are on separate pages anyway). I just suggest to use either PDO or MySQLI.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement