当我尝试对自己网站上的帖子发表评论时,我很难贴上身份证。到目前为止,当我单击评论时,它具有其ID(例如ID = 4),但是,当我单击“离开评论”时,该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,以便我可以发表评论,它会显示在我的网站上。
答案 0 :(得分:1)
是的,没关系,但是您的代码中提示错误:
<a href="leavecomments.php?id=' . $row['blogid'] . '" not use $row but $rowb