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

my guestbook will not display the posts i enter

我想提前感谢您帮助我解决相关问题。问题是我的留言簿正在将数据导入数据库,因此它正在连接,但是当我点击提交时,数据进入数据库但不显示我不确定,因为这可能是版本冲突。我正在观看 YouTube 创建此留言簿,但不确定创建留言簿的人是什么版本。除了 post 的显示外,一切正常。我想知道你们是否可以帮助我,我将 post 下面的代码。

<?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. Use mysqli with prepared statements, or PDO with prepared statements 开放的,它们更安全