Javascript 评论时出错
我有一个从数据库获取帖子的页面,并在我命名为news-feed.php的页面中显示帖子。我的页面如下所示:Javascript 评论时出错,javascript,php,html,css,Javascript,Php,Html,Css,我有一个从数据库获取帖子的页面,并在我命名为news-feed.php的页面中显示帖子。我的页面如下所示: <link rel="stylesheet" type="text/css" href="../Style/news_feed.css"> <!-- Styling --> <div class="whole-container"> <?php $connection = mysqli_connect($server_name, $databas
<link rel="stylesheet" type="text/css" href="../Style/news_feed.css"> <!-- Styling -->
<div class="whole-container">
<?php
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name); //The variables are defined in the **backbone.php** page that we have previously included...
$sql = "SELECT * FROM posts";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
showPosts($row["posted_date"], $row["author"], $row["type"], $row["body"], $row["comments"], $row["id"]);
}
function get($property){ //It gets the $_SESSION["id"] and gets other info about the user using the id.
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "SELECT * FROM users WHERE id = '" . $_SESSION["id"] . "'";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
$outcome = $row[$property];
}
return $outcome;
}
function showPosts($date, $author, $type, $body, $comments, $id) {
?>
<div class="post">
<div class="poster"><a href="user.php/<?php echo $author ?>"> <b id="author"><?php echo $author . " posted a new " . $type . ""; ?></a></b> <b id="date"> <?php echo $date ?> </b></div>
<div class="body"><p><?php echo $body ?></p></div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<?php
if($type === "query"){
enableComment();
if(isset($_POST["postComment"]) && !empty($_POST["comment"])){
postComment(get("full_name"), date("h:sa"), $_POST["comment"], $id);
}
}?>
<button id="showComments" name="showComments"><i class="fa fa-sort-desc"></i> Show comments</button>
</form>
<div class="comments">
<?php
if(isset($_POST["showComments"])){
echo $comments;
}
?>
</div>
</div>
<style>
.post{
width: 40%;
background-color: #ffffff;
margin-left: 20%;
margin-top: 2.5%;
border: 0.2px solid #dddfe2;
padding: 2px 2px 2px 2px;
max-height: 80%;
min-height: 20%;
overflow: auto;
}
.poster{
display: inline-block;
width: 100%;
min-height: 20%;
max-height: 20%;
background-color: #ffffff;
overflow-wrap: break-word;
text-overflow: ellipsis;
}
.poster #author{
float: left;
font-family: helvetica;
font-size: 13px;
}
.poster #date{
float: right;
font-family: arial;
color: grey;
font-size: 13px;
}
.post .body{
width: 100%;
background-color: #ffffff;
float: left;
overflow-wrap: break-word;
max-height: 40%;
}
.post .body p{
font-family: helvetica;
}
.post #showComments{
width: 30%;
background-color: #ffffff;
border: none;
cursor: pointer;
font-family: helvetica;
color: #3b5998;
}
.post #showComments i{
color: purple;
}
.post .comments{
width: 60%;
background-color: #ffffff;
height: 39%;
max-height: 40%;
overflow: auto;
margin-top: 1%;
}
.post .comments b{
font-weight: normal;
font-family: helvetica;
}
#comment{
width: 50%;
margin-left: 20%;
background-color: #ffffff;
border-bottom: 0.2px solid #dddfe2;
border-left: none;
border-right: none;
border-top: none;
height: 20px;
padding-left: 10px;
}
#commentButton{
width: 10%;
background-color: #ffffff;
border: 1px solid #dddfe2;
border-radius: 5px;
color: #3b5998;
cursor: pointer;
}
#comment:focus{
outline-width: 0px;
border-bottom: 1px solid #dddfe2;
}
</style>
<?php
} ?>
<?php
function enableComment() {
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input name="comment" type="text" placeholder="Wanna Help?" id="comment">
<button name="postComment" type="submit" id="commentButton"><i class="fa fa-paper-plane"></i></button>
</form>
<?php
}
function postComment($author, $time, $comment, $id){
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "UPDATE posts SET comments = \"'" . $author . "' -> '". $comment . "' @ '".$time . "'\" WHERE id = ". $id. "";
echo $sql;
}
?>
<link rel="stylesheet" type="text/css" href="../Style/news_feed.css"> <!-- Styling -->
<div class="whole-container">
<?php
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name); //The variables are defined in the **backbone.php** page that we have previously included...
$sql = "SELECT * FROM posts";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
showPosts($row["posted_date"], $row["author"], $row["type"], $row["body"], $row["comments"], $row["id"]);
}
function get($property){ //It gets the $_SESSION["id"] and gets other info about the user using the id.
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "SELECT * FROM users WHERE id = '" . $_SESSION["id"] . "'";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
$outcome = $row[$property];
}
return $outcome;
}
function showPosts($date, $author, $type, $body, $comments, $id) {
?>
<div class="post">
<div class="poster"><a href="user.php/<?php echo $author ?>"> <b id="author"><?php echo $author . " posted a new " . $type . ""; ?></a></b> <b id="date"> <?php echo $date ?> </b></div>
<div class="body"><p><?php echo $body ?></p></div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<?php
if($type === "query"){
enableComment();
if(isset($_POST["postComment"]) && !empty($_POST["comment"])){
postComment(get("full_name"), date("h:sa"), $_POST["comment"], $id);
}
}?>
<button id="showComments" name="showComments"><i class="fa fa-sort-desc"></i> Show comments</button>
</form>
<div class="comments">
<?php
if(isset($_POST["showComments"])){
echo $comments;
}
?>
</div>
</div>
<style>
.post{
width: 40%;
background-color: #ffffff;
margin-left: 20%;
margin-top: 2.5%;
border: 0.2px solid #dddfe2;
padding: 2px 2px 2px 2px;
max-height: 80%;
min-height: 20%;
overflow: auto;
}
.poster{
display: inline-block;
width: 100%;
min-height: 20%;
max-height: 20%;
background-color: #ffffff;
overflow-wrap: break-word;
text-overflow: ellipsis;
}
.poster #author{
float: left;
font-family: helvetica;
font-size: 13px;
}
.poster #date{
float: right;
font-family: arial;
color: grey;
font-size: 13px;
}
.post .body{
width: 100%;
background-color: #ffffff;
float: left;
overflow-wrap: break-word;
max-height: 40%;
}
.post .body p{
font-family: helvetica;
}
.post #showComments{
width: 30%;
background-color: #ffffff;
border: none;
cursor: pointer;
font-family: helvetica;
color: #3b5998;
}
.post #showComments i{
color: purple;
}
.post .comments{
width: 60%;
background-color: #ffffff;
height: 39%;
max-height: 40%;
overflow: auto;
margin-top: 1%;
}
.post .comments b{
font-weight: normal;
font-family: helvetica;
}
#comment{
width: 50%;
margin-left: 20%;
background-color: #ffffff;
border-bottom: 0.2px solid #dddfe2;
border-left: none;
border-right: none;
border-top: none;
height: 20px;
padding-left: 10px;
}
#commentButton{
width: 10%;
background-color: #ffffff;
border: 1px solid #dddfe2;
border-radius: 5px;
color: #3b5998;
cursor: pointer;
}
#comment:focus{
outline-width: 0px;
border-bottom: 1px solid #dddfe2;
}
</style>
<?php
} ?>
<?php
function enableComment() {
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input name="comment" type="text" placeholder="Wanna Help?" id="comment">
<button name="postComment" type="submit" id="commentButton"><i class="fa fa-paper-plane"></i></button>
</form>
<?php
}
function postComment($author, $time, $comment, $id){
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "UPDATE posts SET comments = \"'" . $author . "' -> '". $comment . "' @ '".$time . "'\" WHERE id = ". $id. "";
echo $sql;
}
?>
在代码的第一行中,您有以下注释标记:
<link rel="stylesheet" type="text/css" href="../Style/news_feed.css"> <!-- Styling -->
<div class="whole-container">
<?php
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name); //The variables are defined in the **backbone.php** page that we have previously included...
$sql = "SELECT * FROM posts";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
showPosts($row["posted_date"], $row["author"], $row["type"], $row["body"], $row["comments"], $row["id"]);
}
function get($property){ //It gets the $_SESSION["id"] and gets other info about the user using the id.
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "SELECT * FROM users WHERE id = '" . $_SESSION["id"] . "'";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
$outcome = $row[$property];
}
return $outcome;
}
function showPosts($date, $author, $type, $body, $comments, $id) {
?>
<div class="post">
<div class="poster"><a href="user.php/<?php echo $author ?>"> <b id="author"><?php echo $author . " posted a new " . $type . ""; ?></a></b> <b id="date"> <?php echo $date ?> </b></div>
<div class="body"><p><?php echo $body ?></p></div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<?php
if($type === "query"){
enableComment();
if(isset($_POST["postComment"]) && !empty($_POST["comment"])){
postComment(get("full_name"), date("h:sa"), $_POST["comment"], $id);
}
}?>
<button id="showComments" name="showComments"><i class="fa fa-sort-desc"></i> Show comments</button>
</form>
<div class="comments">
<?php
if(isset($_POST["showComments"])){
echo $comments;
}
?>
</div>
</div>
<style>
.post{
width: 40%;
background-color: #ffffff;
margin-left: 20%;
margin-top: 2.5%;
border: 0.2px solid #dddfe2;
padding: 2px 2px 2px 2px;
max-height: 80%;
min-height: 20%;
overflow: auto;
}
.poster{
display: inline-block;
width: 100%;
min-height: 20%;
max-height: 20%;
background-color: #ffffff;
overflow-wrap: break-word;
text-overflow: ellipsis;
}
.poster #author{
float: left;
font-family: helvetica;
font-size: 13px;
}
.poster #date{
float: right;
font-family: arial;
color: grey;
font-size: 13px;
}
.post .body{
width: 100%;
background-color: #ffffff;
float: left;
overflow-wrap: break-word;
max-height: 40%;
}
.post .body p{
font-family: helvetica;
}
.post #showComments{
width: 30%;
background-color: #ffffff;
border: none;
cursor: pointer;
font-family: helvetica;
color: #3b5998;
}
.post #showComments i{
color: purple;
}
.post .comments{
width: 60%;
background-color: #ffffff;
height: 39%;
max-height: 40%;
overflow: auto;
margin-top: 1%;
}
.post .comments b{
font-weight: normal;
font-family: helvetica;
}
#comment{
width: 50%;
margin-left: 20%;
background-color: #ffffff;
border-bottom: 0.2px solid #dddfe2;
border-left: none;
border-right: none;
border-top: none;
height: 20px;
padding-left: 10px;
}
#commentButton{
width: 10%;
background-color: #ffffff;
border: 1px solid #dddfe2;
border-radius: 5px;
color: #3b5998;
cursor: pointer;
}
#comment:focus{
outline-width: 0px;
border-bottom: 1px solid #dddfe2;
}
</style>
<?php
} ?>
<?php
function enableComment() {
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input name="comment" type="text" placeholder="Wanna Help?" id="comment">
<button name="postComment" type="submit" id="commentButton"><i class="fa fa-paper-plane"></i></button>
</form>
<?php
}
function postComment($author, $time, $comment, $id){
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "UPDATE posts SET comments = \"'" . $author . "' -> '". $comment . "' @ '".$time . "'\" WHERE id = ". $id. "";
echo $sql;
}
?>
在HTML中注释代码行的正确语法,请参阅
<link rel="stylesheet" type="text/css" href="../Style/news_feed.css"> <!-- Styling -->
<div class="whole-container">
<?php
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name); //The variables are defined in the **backbone.php** page that we have previously included...
$sql = "SELECT * FROM posts";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
showPosts($row["posted_date"], $row["author"], $row["type"], $row["body"], $row["comments"], $row["id"]);
}
function get($property){ //It gets the $_SESSION["id"] and gets other info about the user using the id.
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "SELECT * FROM users WHERE id = '" . $_SESSION["id"] . "'";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
$outcome = $row[$property];
}
return $outcome;
}
function showPosts($date, $author, $type, $body, $comments, $id) {
?>
<div class="post">
<div class="poster"><a href="user.php/<?php echo $author ?>"> <b id="author"><?php echo $author . " posted a new " . $type . ""; ?></a></b> <b id="date"> <?php echo $date ?> </b></div>
<div class="body"><p><?php echo $body ?></p></div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<?php
if($type === "query"){
enableComment();
if(isset($_POST["postComment"]) && !empty($_POST["comment"])){
postComment(get("full_name"), date("h:sa"), $_POST["comment"], $id);
}
}?>
<button id="showComments" name="showComments"><i class="fa fa-sort-desc"></i> Show comments</button>
</form>
<div class="comments">
<?php
if(isset($_POST["showComments"])){
echo $comments;
}
?>
</div>
</div>
<style>
.post{
width: 40%;
background-color: #ffffff;
margin-left: 20%;
margin-top: 2.5%;
border: 0.2px solid #dddfe2;
padding: 2px 2px 2px 2px;
max-height: 80%;
min-height: 20%;
overflow: auto;
}
.poster{
display: inline-block;
width: 100%;
min-height: 20%;
max-height: 20%;
background-color: #ffffff;
overflow-wrap: break-word;
text-overflow: ellipsis;
}
.poster #author{
float: left;
font-family: helvetica;
font-size: 13px;
}
.poster #date{
float: right;
font-family: arial;
color: grey;
font-size: 13px;
}
.post .body{
width: 100%;
background-color: #ffffff;
float: left;
overflow-wrap: break-word;
max-height: 40%;
}
.post .body p{
font-family: helvetica;
}
.post #showComments{
width: 30%;
background-color: #ffffff;
border: none;
cursor: pointer;
font-family: helvetica;
color: #3b5998;
}
.post #showComments i{
color: purple;
}
.post .comments{
width: 60%;
background-color: #ffffff;
height: 39%;
max-height: 40%;
overflow: auto;
margin-top: 1%;
}
.post .comments b{
font-weight: normal;
font-family: helvetica;
}
#comment{
width: 50%;
margin-left: 20%;
background-color: #ffffff;
border-bottom: 0.2px solid #dddfe2;
border-left: none;
border-right: none;
border-top: none;
height: 20px;
padding-left: 10px;
}
#commentButton{
width: 10%;
background-color: #ffffff;
border: 1px solid #dddfe2;
border-radius: 5px;
color: #3b5998;
cursor: pointer;
}
#comment:focus{
outline-width: 0px;
border-bottom: 1px solid #dddfe2;
}
</style>
<?php
} ?>
<?php
function enableComment() {
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input name="comment" type="text" placeholder="Wanna Help?" id="comment">
<button name="postComment" type="submit" id="commentButton"><i class="fa fa-paper-plane"></i></button>
</form>
<?php
}
function postComment($author, $time, $comment, $id){
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "UPDATE posts SET comments = \"'" . $author . "' -> '". $comment . "' @ '".$time . "'\" WHERE id = ". $id. "";
echo $sql;
}
?>
第二个问题是,您的页面保存为.css
,它应该保存为.php
,因此您保存了一个包含html
,css
的页面,php
as.css
页面您在新闻提要.css
中显示您的帖子?在页面底部的更新帖子
查询中有“
这些帖子,看起来好像有很多,而且没有得到很好的照顾。您应该重新检查这些”“
这些只是为了将数据澄清为字符串@MukeshAryal您不应该在事情解决后编辑代码,这可能会在将来与新用户讨论这个问题时产生误解
<link rel="stylesheet" type="text/css" href="../Style/news_feed.css"> <!-- Styling -->
<div class="whole-container">
<?php
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name); //The variables are defined in the **backbone.php** page that we have previously included...
$sql = "SELECT * FROM posts";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
showPosts($row["posted_date"], $row["author"], $row["type"], $row["body"], $row["comments"], $row["id"]);
}
function get($property){ //It gets the $_SESSION["id"] and gets other info about the user using the id.
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "SELECT * FROM users WHERE id = '" . $_SESSION["id"] . "'";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
$outcome = $row[$property];
}
return $outcome;
}
function showPosts($date, $author, $type, $body, $comments, $id) {
?>
<div class="post">
<div class="poster"><a href="user.php/<?php echo $author ?>"> <b id="author"><?php echo $author . " posted a new " . $type . ""; ?></a></b> <b id="date"> <?php echo $date ?> </b></div>
<div class="body"><p><?php echo $body ?></p></div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<?php
if($type === "query"){
enableComment();
if(isset($_POST["postComment"]) && !empty($_POST["comment"])){
postComment(get("full_name"), date("h:sa"), $_POST["comment"], $id);
}
}?>
<button id="showComments" name="showComments"><i class="fa fa-sort-desc"></i> Show comments</button>
</form>
<div class="comments">
<?php
if(isset($_POST["showComments"])){
echo $comments;
}
?>
</div>
</div>
<style>
.post{
width: 40%;
background-color: #ffffff;
margin-left: 20%;
margin-top: 2.5%;
border: 0.2px solid #dddfe2;
padding: 2px 2px 2px 2px;
max-height: 80%;
min-height: 20%;
overflow: auto;
}
.poster{
display: inline-block;
width: 100%;
min-height: 20%;
max-height: 20%;
background-color: #ffffff;
overflow-wrap: break-word;
text-overflow: ellipsis;
}
.poster #author{
float: left;
font-family: helvetica;
font-size: 13px;
}
.poster #date{
float: right;
font-family: arial;
color: grey;
font-size: 13px;
}
.post .body{
width: 100%;
background-color: #ffffff;
float: left;
overflow-wrap: break-word;
max-height: 40%;
}
.post .body p{
font-family: helvetica;
}
.post #showComments{
width: 30%;
background-color: #ffffff;
border: none;
cursor: pointer;
font-family: helvetica;
color: #3b5998;
}
.post #showComments i{
color: purple;
}
.post .comments{
width: 60%;
background-color: #ffffff;
height: 39%;
max-height: 40%;
overflow: auto;
margin-top: 1%;
}
.post .comments b{
font-weight: normal;
font-family: helvetica;
}
#comment{
width: 50%;
margin-left: 20%;
background-color: #ffffff;
border-bottom: 0.2px solid #dddfe2;
border-left: none;
border-right: none;
border-top: none;
height: 20px;
padding-left: 10px;
}
#commentButton{
width: 10%;
background-color: #ffffff;
border: 1px solid #dddfe2;
border-radius: 5px;
color: #3b5998;
cursor: pointer;
}
#comment:focus{
outline-width: 0px;
border-bottom: 1px solid #dddfe2;
}
</style>
<?php
} ?>
<?php
function enableComment() {
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input name="comment" type="text" placeholder="Wanna Help?" id="comment">
<button name="postComment" type="submit" id="commentButton"><i class="fa fa-paper-plane"></i></button>
</form>
<?php
}
function postComment($author, $time, $comment, $id){
require '../includes/backbone.php';
$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
$sql = "UPDATE posts SET comments = \"'" . $author . "' -> '". $comment . "' @ '".$time . "'\" WHERE id = ". $id. "";
echo $sql;
}
?>