Search code examples
phpmysqlxampp

my guestbook will not display the posts i enter


I want to thank you in advance for helping me with my related issue. The thing is my guestbook is getting the data into the database so it is connecting but when i hit submit the data goes to the database but does not display i am not sure as this maybe be a version conflict. I was watching YouTube in creating this guestbook but not sure what version the guy that created the guestbook. Everything works except the display of the post. I was wondering if you guys could help me with this and i will post code below.

<?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>

Solution

  • You've pluralized rows in your while loop:

    while ( $rows = mysql_fetch_assoc($query) ){
                ^ - plural
    
        $id = $row['id'];
                  ^ no "s" - singular
    

    while ( $rows where you're using $row for everything else, which is why it's not displaying any of your rows.

    Use while ( $row in singular form.


    I need to point out that your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.