gpt4 book ai didi

php - 我的留言簿不会显示我输入的帖子

转载 作者:行者123 更新时间:2023-11-29 12:20:23 24 4
gpt4 key购买 nike

我想提前感谢您帮助我解决相关问题。问题是我的留言簿正在将数据存入数据库,因此它正在连接,但是当我点击提交时,数据会转到数据库但不显示,我不确定,因为这可能是版本冲突。我在创建此留言簿时观看了 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>

最佳答案

您已在 while 循环中对 rows 进行复数化:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com