I created a really simple guestbook script, and it seems that I'm having troubles getting PHP to display the right thing. What I want displayed is this:
On (date), (name) said:
(comments)
but instead, this is what I get
On, (name of database I'm using) said:
(comments).
Here's the code for the script that inserts the rows into the database.
<?php
//make all the globals local
$name = $_POST['name'];
$email = $_POST['email'];
$url = $_POST['url'];
$comments = $_POST['comments'];
//check to make sure the required variables are set
if(empty($name) || empty($email) || empty($comments)) {
$message = "You did not fill in the required fields! Please go back and check your entry.";
} else {
//check if the url is filled in
if(empty($url)) {
$link = FALSE;
} else {
$link = "$url";
}
//connect to the database
require("config.php");
//insert the record into the database
$date = date('l, F j, Y \a\t g:i A');
$new_entry = "INSERT INTO entries VALUES('', '$name', '$email', '$link', '$date', '$comments')";
$entry_res = mysql_query($new_entry) or die("Was unable to perform the query.");
if($entry_res) {//redirect
$message = "Everything worked out. <a href='guestbook.php'>View the guestbook</a>";
} else {
$message = "Sorry, but the entry could not be added.";
}
}
?>
<html>
<head>
<title>Guestbook</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div align="center">
<div id="container">
<div id="header"><img src="utadavserik.jpg" /></div>
<div id="main">
<?php echo "$message"; ?>
</div>
</div>
</div>
</body>
</html>
and the script that displays the rows:
<html>
<head>
<title>Guestbook</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div align="center">
<div id="container">
<div id="header"><img src="utadavserik.jpg" /></div>
<div id="main">
<?php
//connect to the database
require("config.php");
//select all the entries
$get_entries = "SELECT * FROM entries ORDER BY id DESC";
$get_entries_res = mysql_query($get_entries) or die(mysql_error());
//any results?
if(mysql_num_rows($get_entries_res) < 1) {
echo "Nada.";
} else {
//begin the while loop
while($entries_info = mysql_fetch_array($get_entries_res)) {
$id = $entries_info['id'];
$name = $entries_info['name'];
$email = $entries_info['email'];
$link = $entries_info['url'];
$date = $endtires_info['entrytime'];
$comments = $entries_info['comments'];
if(isset($link)) {
$display_block = "
<table>
<tr>
<td>On $date, <a href='$link'>$name</a> said:</td>
</tr>
<tr>
<td>$comments</td>
</tr>
</table>
";
} else {
$display_block = "
<table>
<tr>
<td>On $date, $name said:</td>
</tr>
<tr>
<td>$comments</td>
</tr>
</table>
";
}
}
}
?>
<p>
<?php echo "$display_block"; ?>
</p>
<div align="left" style="padding: 10px;">
<p>
Submit a comment:<br />
<form method="post" action="guestbook_action.php">
Your name:<br />
<input type="text" name="name" /><br />
Your email (will not be made public):<br />
<input type="text" name="email" /><br />
Your website URL (optional): <br />
<input type="text" name="url" /><br />
Your comments:<br />
<textarea rows="8" cols="40" name="comments"></textarea><br />
<input type="submit" value="Submit">
<input type="reset" value="Clear">
</form>
</div>
</div>
</div>
</div>
</body>
</html>