我想提前感谢你帮助解决我的相关问题。问题是我的留言簿是将数据输入数据库所以它正在连接但是当我点击提交数据进入数据库但不显示我不确定,因为这可能是版本冲突。我正在观看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>
答案 0 :(得分:1)
您在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,他们更安全。