Jump to content

Newbie help with guestbook


mrmodest

Recommended Posts

Alright, I am trying to learn some PHP so I have been following a tutorial on how to make a simple guestbook page for my website. The problem is, the post button I coded into the page is not appearing and the area that I want displaying the current posts is appearing in the message text area where you actually are supposed to write posts. Everything is working properly with the database though which is a feat on my part. I must have a problem with a quotation somewhere but I have been looking for hours for my error. It is not a lot of code but I am not well experienced with this language. Everything looks correct until the message area underneath the other 2 text areas.

 

<?php
error_reporting (E_ALL ^ E_NOTICE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xht
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guestbook - Index</title>
</head>
<body>
<?php
		  
mysql_connect("xxx","xxx", "xxx") OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db("xxx");

		  
		  echo "<h2>Post to the Guestbook</h2>";
		  
		  if ($_POST['postbtn']){
		  $name = striptags($_POST['name']);
		  $email = striptags($_POST['email']);
		  $message = striptags($_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 "Post submitted!";

			}
			else
				echo "All information is required for posting.";
		  }
		  
		  echo "<form action='./guestbook.php' method='post'>
		  <table>
		  <tr>
		  <td>Name:</td>
		  <td><input type='text' name='name' style='width: 200px;' /></td>
		  </tr>
		  <tr>
		  <td>Email:</td>
		  <td><input type='text' name='email' style='width: 200px;' /></td>
		  </tr>
		  <tr>
		  <td>Message:</td>
		  <td><textarea name='message' style='width: 200px; height: 100px;' /></td>
		  </tr>
		  <tr>
		  <td></td>
		  <td><input type='submit' name='postbtn' value='Post' /></td>
		  </tr>
		  </table>
		  </form>";
		  
		  // DISPLAY
		  echo "<h2>Current Posts</h2>";
		  $query = mysql_query("SELECT * FROM guestbook ORDER BY id DESC"); // get most recent post
		  $numrows = mysql_num_rows($query); // how much data has been found
		  
		  if ($numrows > 0) { // if there are posts in the database
		  
		    echo "</ hr>"; // top horizontal line

			while ( $row = mysql_fetch_assoc($query) ) {

				$id = $row['id']; // get id out of the row variable for particular query
				$name = $row['name'];
				$email = $row['email'];
				$message = $row['message'];
				$time = $row['time'];
				$date = $row['date'];
				$ip = $row['ip'];

				$message = nl2br($message); // for breaking if new line

				echo "<div>
					By <b>$name</b> at <b>$time</b> on <b>$date</b><hr /> 
					$message
				</div> <hr />";
			}
		  }
		  else
		  echo "No posts were found.";
		  
		  mysql_close();
?>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.