在在线学习了一些教程之后,我正在尝试编写一个简单的 PHP 留言簿。我可以毫无问题地写入数据库;但是,当我尝试解析现有评论并将它们“回显”回页面时,我没有看到任何显示的内容。
这有明显的原因吗?
(有问题的代码从“现有评论:”开始)
<?php
require_once('includes/config.inc.php');
define('DB_GUEST','guestbook_db');
// Connect
$mysqli = @new mysqli(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_GUEST);
// Check connection
if (mysqli_connect_errno()) {
printf("Unable to connect to database: %s", mysqli_connect_error());
exit();
}
// POST
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$now = time();
$name=($_POST['name']);
$message =($_POST['message']);
$sql = "insert into comments (name,message,timestamp) values ('$name','$message','$now')";
$result = $mysqli->query($sql) or die($mysqli->error.__LINE__);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Guest Book</title>
</head>
<body>
<h3>Post A Comment:</h3>
<form action="index.php" method="post">
<strong>Name:</strong><br/> <input type="text" name="name" /><br/>
<strong>Comment:</strong><br/> <textarea name="message" rows="5" cols="25">
<textarea><br/>
<input type="submit" value="Go">
</form>
<h3>Exisiting Comments:</h3>
<?php
$sql1 = "select * from guestbook_db.comments";
$allPostsQuery = $mysqli->query($sql1) or die($mysqli->error.__LINE__);
if(!mysql_fetch_rows($allPostsQuery)) {
echo "No comments were found!";
} else {
while($row = mysql_fetch_assoc($allPostsQuery)) {
echo "<b>Name:</b> {$comment[1]} <br/>";
echo "<b>Message:</b> {$comment[2]} <br/>";
echo "<b>Posted at:</b> " .date("Y-d-m H:i:s",$comment[3]). " <br/><hr/>";
}
}
?>
</body>
</html>