...

View Full Version : Guestbook problem with data output



tkm76
09-17-2011, 04:03 PM
Hi guys,

I'm having a problem with my guestbook script. It's not displaying data entered into the table, it just outputs names of specific columns.

In this manner:

Entries found.

Posted by name (email) on timestamp:
message

Posted by name (email) on timestamp:
message

Posted by name (email) on timestamp:
message

I'm not getting any error messages and that makes it difficult to understand what is actually wrong with this code. Any ideas what needs to be done? Thanks in advance!



<?php

if (mysql_connect('localhost','root','password') && mysql_select_db('phpdemo')) {

$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 are required.';
}

if (strlen($guestbook_name)>25 || strlen($guestbook_email)>50 || strlen($guestbook_message)>250){
$errors[] = 'One or more fields exceeded the character limit!';
}

if (empty($erros)) {
$insert = "INSERT INTO guestbook VALUES ('','$time','$guestbook_name','$guestbook_email','$guestbook_message')";
if (mysql_query($insert)){
header ('Location: '.$_SERVER['PHP_SELF']);
} else {
$errors[] = 'Something went wrong. Try again.';
}

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

}

}
// display entries
$entries = mysql_query("SELECT 'timestamp', 'name', 'email', 'message' FROM guestbook ORDER BY 'timestamp' DESC");

if (mysql_num_rows($entries)==0){
echo 'No entries, yet.';
} else {
echo 'Entries found.';

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 '<p><strong>Posted by '.$entries_name.' ('.$entries_email.') on '.$entries_timestamp.'</strong>:<br>'.$entries_message.'</p>';

}
}

} else {
echo 'Could not connect at this time.';
}

?>

<hr>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<strong>Post something...</strong><br>
Name:<br><input type="text" name="guestbook_name" maxlength="25"><br>
Email: <br><input type="text" name="guestbook_email" maxlength="250"><br>
Comment body: <br><textarea name="guestbook_message" rows="6" cols="30" maxlength="250"></textarea><br>
<input type="submit" value="Submit comment">
</form>

tangoforce
09-17-2011, 04:48 PM
Try doing away with the apostrophes:
mysql_query("SELECT 'timestamp', 'name', 'email', 'message'

Either use nothing:
mysql_query("SELECT timestamp, name, email, message

or use backticks:
mysql_query("SELECT `timestamp`, `name`, `email`, `message`

See if that makes any difference.

Also enable error reporting and check that you have data in the table to display.

tkm76
09-17-2011, 05:19 PM
Removing single quotes worked.
The only problem remaining is the timestamp column.
I am getting "1315765293" instead readable date.

Also, how would you suggest i replace the "isset" line to suit IE?
I'm using IE8 at the moment and everything seems to be working pretty much the same as in FF.

Many thanks!

tkm

tangoforce
09-17-2011, 05:55 PM
I am getting "1315765293" instead readable date.

Also, how would you suggest i replace the "isset" line to suit IE?

1) Lookup the date() (http://www.php.net/date) function. Input your desired effect and pass the timestamp into it as the second parameter.

2) isset() is nothing to do with IE. It's a php function which runs on the server. If you're having a problem with your page layout thats better suited to the html/css forums. isset() is used to test that a variable is set.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum