PDA

View Full Version : PHP MySQL Guestbook


nicky
07-16-2008, 05:47 PM
I want to create an updating system like CuteNews or FanUpdate. First I thought I'd start by trying to put together a simple guestbook. I logged onto www.phpeasystep.com and I followed all the steps. It worked! But then I started modifying stuff and screwed it all up, so I restarted. After I restarted though, it wouldn't work! I tried finding another tutorial but I haven't found a decent one. Please help!

Fumigator
07-16-2008, 06:24 PM
How do you expect we can help with no access to your code or machine?

nicky
07-17-2008, 03:59 AM
'Cause I'm not really sure what I'm doing and I was just wondering if someone can direct me towards a great tutorial.

bazz
07-17-2008, 04:29 AM
You could repeat your initial steps?

I logged onto www.phpeasystep.com and I followed all the steps.


bazz

nicky
07-17-2008, 10:09 PM
It said to create three pages. Here's the coding from my first page that contains the form.


<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>Test Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" 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>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
</tr>
</table>


Then when you submit the form it goes to a page which contains this coding. I deleted the database information for security purposes obviously.


<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

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

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

//check if query successful
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
}

else {
echo "ERROR";
}

mysql_close();
?>


Here is the coding for the 3rd page which would display the results if I had any.

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
</tr>
</table>
<br>

<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?
}
mysql_close(); //close database
?>


If you'd like to test it out, go to www.candi-coded.com/nicky/comment

Fumigator
07-17-2008, 10:44 PM
I think you are misunderstanding the purpose of this forum. We are here to help fellow coders with specific issues they encounter as they achieve their coding dreams. We are not here to provide free coding services. So if you can isolate and focus your problem into a specific issue, we will be happy to help resolve that issue.

"This doesn't work can you fix it plz heres the code" doesn't fly.

nicky
07-18-2008, 07:26 AM
This is my issue. I don't understand what you're talking about focussing my problem into a specific issue. I just stated it. I had this guestbook working and I'm so confused as to why it's not now all of a sudden. If you don't want to help me, then fine, just say so.

_Aerospace_Eng_
07-18-2008, 08:03 AM
Well what part of it isn't working? Is your database populated? Are you doing any error checking meaning do you have or die(mysql_error()) on any of your mysql_query calls?

nicky
07-18-2008, 05:28 PM
When I submit the form and it goes to the second page, it says ERROR. I'm not all too familiar with MySQL so...

_Aerospace_Eng_
07-18-2008, 09:49 PM
On the second page change this
echo "ERROR";
to this
echo mysql_error();
Copy and paste the error you get here.