如何在试图留下 post 评论时让 id 保持不变
How to make an id stick when trying to leave a post comment
当我试图对我网站上的 post 发表评论时,我的 id 无法显示。到目前为止,当我点击评论时它有它的 id(例如 id=4),但是,当我点击 'leave comment' 时,id 不固定,只是说(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>
我希望 id 保留下来,这样我就可以发表评论,它会显示在我的网站上。
是的,没问题,但是你的代码提示错误:
<a href="leavecomments.php?id=' . $row['blogid'] . '" not use $row but $rowb
当我试图对我网站上的 post 发表评论时,我的 id 无法显示。到目前为止,当我点击评论时它有它的 id(例如 id=4),但是,当我点击 'leave comment' 时,id 不固定,只是说(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>
我希望 id 保留下来,这样我就可以发表评论,它会显示在我的网站上。
是的,没问题,但是你的代码提示错误:
<a href="leavecomments.php?id=' . $row['blogid'] . '" not use $row but $rowb