Как сделать id stick при попытке оставить комментарий - PullRequest
0 голосов
/ 29 апреля 2019

У меня проблемы с наличием моей идентификационной карты, когда я пытаюсь комментировать сообщение на моем сайте.До сих пор, когда я нажимаю на комментарий, у него есть свой идентификатор (например, id = 4), однако, когда я нажимаю «оставить комментарий», идентификатор не привязывается и просто говорит (id =), а не номер, поэтому я могуоставить комментарий, потому что он не знает, где оставить комментарий.

Я попытался определить переменную $ comments = $ _GET ['id'];и положить его в форме действия, но это не сработало.Я пытался поместить "? Id =. $ _GET ['id']" в действие формы, но это также, похоже, не сработало.

Я поставил старты рядом сто, что я думаю, является важной частью моего кода

<?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:

<?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>

Я бы хотел, чтобы идентификатор вставлялся, чтобы я мог добавить комментарийи он появится на моем сайте.

1 Ответ

1 голос
/ 29 апреля 2019

Да, все в порядке, но у вас в коде неверная подсказка:

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