在此先感谢您帮助我解决相关问题。事情是我的留言簿正在将数据放入数据库中,因此正在连接,但是当我点击提交时,数据就进入了数据库,但没有显示,我不确定这可能是版本冲突。我在创建此留言簿时正在观看YouTube,但不确定创建该留言簿的那个人的版本。除显示帖子外,其他所有内容均有效。我想知道你们是否可以帮助我,我将在下面发布代码。
<?php error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>
<body>
<?php
// connect to the database
mysql_connect('localhost', 'root', '');
mysql_select_db('tutorials');
/* * ************************************************* */
// form and add stuff area
echo "<h3> Enter posts to GuestBook </h3>";
if ($_POST['postbtn']) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
if ($name && $email && $message) {
$time = date("h:i A");
$date = date("F d, Y");
$ip = $_SERVER['REMOTE_ADDR'];
// add to the database
mysql_query("INSERT INTO guestbook VALUES ( '', '$name', '$email', '$message', '$time', '$date', 'ip'
)");
echo "Your post has been added.";
} else {
echo "You did not enter in all the required information";
}
}
echo "<form action = './guestbook.php' method = 'post'>
<table>
<tr>
<td>Name:</td>
<td><input type = 'text' name = 'name' style = 'width: 300px;' /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type = 'text' name = 'email' style = 'width: 300px;' /></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name = 'message' style = 'width: 300px; height: 300px;'></textarea></td>
</tr>
<tr>
<td></td>
<td><input type = 'submit' name = 'postbtn' value = 'Post' /></td>
</tr>
</table>
</form>";
/* * ************************************************ */
//display stuff area
echo "<h3> Current Posts </h3>";
$query = mysql_query("SELECT * FROM guestbook ORDER BY id DESC");
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
echo "<hr />"; //echoing out the top horizontal line
while ($rows = mysql_fetch_assoc($query)) {
$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
$message = $row['message'];
$time = $row['time'];
$date = $row['date'];
$ip = $row['ip'];
//nl2br new line to break function
$message = nl2br("message");
echo "<div>
By <b>$name</b> - at <b>$time</b> on <b>$date </b><br />
$message
</div> <hr />";
}
} else {
echo "No posts were found.";
}
mysql_close();
?>
</body>
</html>
最佳答案
您已经在rows
循环中将while
复数了:
while ( $rows = mysql_fetch_assoc($query) ){
^ - plural
$id = $row['id'];
^ no "s" - singular
while ( $rows
在其他任何地方都使用$row
,这就是为什么它不显示任何行的原因。以单数形式使用
while ( $row
。我需要指出,您当前的代码对SQL injection开放。使用
mysqli
with prepared statements或PDO with prepared statements,它们更加安全。关于php - 我的留言簿不会显示我输入的帖子,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29107906/