Search code examples
phphtmlsticky

How to make an id stick when trying to leave a post comment


I'm having trouble on having my id stick when I'm trying to make a comment on a post on my site. So far when I click on the comment it has its id (ex. id=4), however, when I click on 'leave comment' the id doesn't stick and just says (id=) and not the number so I can't leave a comment because it doesn't know where to leave the comment.

I've tried defining a variable $comments = $_GET['id']; and putting it in the form action but that didn't seem to work. I've tried putting "?id= . $_GET['id']; in the form action as well, but that also didn't seem to work.

I have put starts next to what I think is the important part of my code

<?php
session_start();
?>
<html>
<!-- this includes the header and mysqli pages-->

<?php
include('includes/header.html');
include('includes/mysqli_connect.php');
?>

<head>

</head>
<body>
<!-- this styles the css for my form to make it look nice -->
<style type="text/css">
body{
    font-family: Arial, Gadget, sans-serif;
    background-color: #FFD9D9;
}
strong{
    color: red;
    font-weight: bold;
}
center{
    margin: auto;
}

</style>



<!-- have access to posting special characters and will trim unnecessary spaces-->
<?php
$userid = mysqli_real_escape_string($dbc, trim($_SESSION['user_id']));
$blogid = mysqli_real_escape_string($dbc, trim($_GET['id']));
$comment = mysqli_real_escape_string($dbc, trim($_POST['comment']));
***$comments = $_GET['id'];***

#This adds in the comment based on their ID, email,fname,lname and date they made the comment
if (($title != NULL) && ($post != NULL)){
$query = "INSERT INTO comments (comid, userid, blogid, comment, comdate) VALUES (NULL, '$userid', '$blogid', '$comment', NOW());";
$result = mysqli_query($dbc, $query);

#This displays an error if there was an error in making a comment
#EDIT: The "There was an error" never will display since my form validates if criteria was filled out or not, if it's not it makes the guest fill out that area
if ($result) {
    echo "<center><strong>Thank you, the comment has been added.</strong></center><br /><br />"; 
    $title = NULL;
    $title = NULL;

    }
else {
    echo "<strong>There was an error! </strong>" . mysqli_error($dbc);
    }
}



?>


<form action="<?php echo basename(__FILE__) . ***"?id=" . $comments;*** ?>" method="post">

<?php
if(($_POST['comment'] == NULL) && ($_SERVER['REQUEST_METHOD'] == 'POST')){
    echo "<strong><center>Please fill in a comment!</center></strong><br />";
}
?>

<p><center><label><b>Leave comments here: </p></center></label></b>
<p align="center"><textarea name="comment" cols="40" rows="5">
<?php echo $_POST['comment'];
?>
</textarea>
</p>



<br />
<p align="center"><input type="submit" name="submit" value="Submit" /></p>
</form>

<!--this includes the footer page-->
<?php
include('includes/footer.html');
?>
</body>
</html>

comments.php code:

<?php
session_start();
?>
<html>

<head>

</head>
<body>
<!-- this styles the css for my form to make it look nice -->
<style type="text/css">
body{
    font-family: Arial, Gadget, sans-serif;
    background-color: #FFD9D9;

}
strong{
    color: red;
    font-weight: bold;
}
center{
    border-left: .17em dashed;
    border-top: .17em solid;
    border-right: .17em dashed;
    border-bottom: .17em solid;
    padding-left:25px;
    padding-bottom: 20px;
    width: 1000px;
    background-color: #E1A0A0;
    border-color: black;
    margin: auto;
    text-align: left;

}
h3 {
text-align: center;
color: red; 
}

</style>
<!-- this includes the header and mysqli pages-->
<?php
include('includes/header.html');
include('includes/mysqli_connect.php');
?>


<?php
$blogid = $_GET['id'];
?>

<?php
$query = "SELECT blogid, title, post, DATE_FORMAT (postdate, '%M, %d, %Y') AS date, post FROM blogposts WHERE blogid=$blogid";

$result = mysqli_query($dbc, $query);
?>

<!-- this will display the ID, fname, lname, email, comment, and date posted gathered from the MYSQL database-->
<?php
while ($rowb = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo "<center><h2>" . $rowb['title'] . "</h2>" . "<b>Posted On: </b>" . $rowb['date'] . "<br/>" . '<br/><a href="leavecomments.php?id=' . $row['blogid'] . '">Leave Post Comment</a>' . "\n";
      if($_SESSION['user_id'] == 3) {
            echo '| <a href="update.php?id=' . $row['blogid'] . '" >Update Blog Post</a> | <a href="' . basename(__FILE__) . '?id=' . $row['blogid'] . '" >Delete Blog Post</a>';
        }
        echo "</center>";
}
?>

<div align="center">
<h2>
Comments
</h2>
</div>

<?php
//define query
$q = "SELECT * FROM comments JOIN users USING (userid) WHERE $blogid";
$r = mysqli_query ($dbc, $q); //run query
?>

<!-- this will display the ID, fname, lname, email, comment, and date posted gathered from the MYSQL database-->
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    echo "<center><br/>" . "<b>Comment: </b>" . $row['comment'] . "<br /><br/>" . "<b>ID: </b>" . $row['comid'] . "<br />" . "<b>First Name: </b>" . $row['fname'] . "<br />" . "<b>Last Name: </b>" . $row['lname'] . "<br />" . "<b>Email: </b>" . $row['email'] . "<br />" . "<b>Posted At: </b>" . $row['comdate'] . "<br/></center>\n";

}
?>



<!--this includes the footer page-->
<?php
include('includes/footer.html');
?>

</body>
</html>

I would like the id to stick so I can put in a comment and it will show up on my site.


Solution

  • Yep, its all ok, but you have bad prompt in your code:

    <a href="leavecomments.php?id=' . $row['blogid'] . '" not use $row but $rowb