Jump to content

PHP & MySQL - Guestbook


dont_be_hasty

Recommended Posts

Hi  :)

 

Im creating a guestbook using the following tutorial: http://www.phpeasystep.com/workshopview.php?id=15

 

Ive basically just copied that code and only changed things like username, database name etc...

 

However when the user enters their details, it does not seem to actually add the details to the database. A new entry is added to the table but the info taken from the html does not seem to get added to the database correctly.

 

Here is the code from the pages.

 

Page with form:

<table width="400" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC">
					<tr>
						<form id="form1" name="form1" method="post" action="addguestbook.php">
						<td>
							<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
								<tr>
									<td width="117">Name</td>
									<td width="14">:</td>
									<td width="357"><input name="name" type="text" id="name" size="40" /></td>
								</tr>
								<tr>
									<td>Email</td>
									<td>:</td>
									<td><input name="email" type="text" id="email" size="40" /></td>
								</tr>
								<tr>
									<td valign="top">Comment</td>
									<td valign="top">:</td>
									<td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
								</tr>
								<tr>
									<td> </td>
									<td> </td>
									<td> </td>
								</tr>
								<tr>
									<td> </td>
									<td> </td>
									<td><input type="submit" value="Submit"><input type="reset" value="Reset"></td>
								</tr>
							</table>
						</td>
					</form>
					</tr>
				</table>

 

Code to add info to database:

<?php

					$username="root"; // Mysql username
					$password=""; // Mysql password
					$database="assocdb"; // Database name
					$tbl_name="guestbook"; // Table name

					// Connect to server and select database.
					mysql_connect(localhost,$username,$password);
					@mysql_select_db($database) or die("Unable to select database");

					$datetime=date("y-m-d h:i:s"); //date time

					$query="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
					$result=mysql_query($query);

					//check if query successful
					if($result){
						echo "<br>";
						echo "<br>";
						echo "Your comment was successfully added to the guestbook";
						echo "<br>";
						echo "<br>";
						echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
					}

					else {
						echo "ERROR";
					}

					mysql_close();
				?>
			</div>

On this page the date/time is added correctly to the database, its just the info that is taken from the form that isnt added.

 

 

There is a 3rd page that displays the info, but it shouldnt be that page that is causing the trouble.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/150779-php-mysql-guestbook/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.