-2

Creating a guestbook for a website, I am using a while loop to get rows from my table, When I use echo it returns the Row's name not the actual content stored in it. I don't know what I am doing wrong. here is the PHP:

    <?php

if(mysql_connect('localhost','root','')&& mysql_select_db('m_database')){
$time= time();
$errors =array();

if (isset($_POST['guestbook_name'],$_POST['guestbook_email'],$_POST['guestbook_message'])){

    $guestbook_name = mysql_real_escape_string(htmlentities($_POST['guestbook_name']));
    $guestbook_email = mysql_real_escape_string(htmlentities($_POST['guestbook_email']));
    $guestbook_message = mysql_real_escape_string(htmlentities($_POST['guestbook_message']));



    if ( empty($guestbook_name) || empty($guestbook_email) || empty ($guestbook_message)) {
        $errors[]='all fields must be completed.';
    }

if (strlen($guestbook_name)>25 || strlen($guestbook_email)>255 || strlen($guestbook_message)>255) {
    $errors= 'some of your fields exceeded the character limit, please double check.';
}
if ( empty($errors)){
    $insert = "INSERT INTO guestbook VALUES('','$time','$guestbook_name','$guestbook_email','$guestbook_message') ";

        if ( mysql_query ($insert)) {
            header('location: '.$_SERVER['PHP_SELF']);//redirect the user back to the same page, for spam purposes
        }else {
            $errors[] ='something is not working please try again';
        }

    }else{
    foreach ($errors as $error){
        echo'<p><strong>'.$error.'</strong></p>';
        }
    }

}
//set values form the form
//displaying the data
//using a while loop to display data on page
$entries =mysql_query("SELECT 'timestamp','name','email','message' FROM 'guestbook' ORDER BY 'timestamp'DESC");
if (mysql_num_rows($entries)==0){
    echo 'no entries found';

}else {
    while($entries_row = mysql_fetch_assoc($entries)){
        $entries_timestamp =$entries_row['timestamp'];
        $entries_name =$entries_row['name'];
        $entries_email =$entries_row['email'];
        $entries_message =$entries_row['message'];


            echo entries_name.'<br>';
                  echo $entries_message. '<br>';
                echo $entries_email. '<br>';
        }
    }


    } else {
    echo 'could not connect to database';
    }

    ?>
BMC
  • 5
  • 5

1 Answers1

2

I'm guessing based off of variable names since you didn't include information about the table nor the query used. Is the content for which you seek stored in the "message" column? If so, you're echoing the wrong variable. Try inspecting the $entries_message variable instead.

Update:

I see the problem now that you've posted the full query. The problem is that you've enclosed the columns you want in quotes. That tells the MySQL query engine to return those strings and doesn't actually return the column data. Remove the quotes from the SELECT clause and it should work as you expect.

The line with the query should read:

$entries = mysql_query( "SELECT timestamp, name, email, message FROM guestbook ORDER BY timestamp DESC" );
Thomas
  • 1,402
  • 1
  • 11
  • 14
  • the table has 3 rows that i wish to display name , message and email. when i echo them it returns the name of the Rows instead of the data stored in them. it literally returns name message and email instead the data within the table. that is my problem – BMC Jan 01 '13 at 12:10
  • Thanks for providing more information. I see the problem and have updated the answer to reflect the new data. – Thomas Jan 02 '13 at 14:55