Jump to content

Need help with a PHP script for a guestbook


Big_R

Recommended Posts

Okay I am extremely new to PHP and am trying to make a 2 page guestbook, where one page has the form to submit a message to the guestbook and the other page displays the messages. However I am having some errors, one being that when I submit a test message it still shows the "please enter your name and comments" thing I have set when people don't fill the required fields. The other error I'm having is if I put two test messages into the guestbook some of the info from the last message fills in the fields of the other one instead of what was actually filled into the form.

 

For example, if I insert the name "Pat" and the comment "testing 1 2 3" into one message and than insert the name "Bob" and the comment "hi" for another message when I submit the second message it may instead of putting "hi" it'll put "testing 1 2 3"

 

so I get:

 

Posted by: Pat

testing 1 2 3

________________

 

Posted by: Bob

testing 1 2 3

 

instead of:

 

Posted by: Pat

testing 1 2 3

________________

 

Posted by: Bob

hi

 

Like it should.... What am I doing wrong????  :sweat::facewall:

 

My PHP code for submition is:

 

<?php

//connecting to the Database

$connect = mysql_connect("127.0.0.1","patben_guestbook","") or die("Error");

//selecting the table

mysql_select_db("patben_guestbookdatabase") or die("Error");

//selecting ALL data

$queryget = mysql_query("SELECT * FROM guestbook") or die("Error");

//sort the data

while ($row = mysql_fetch_assoc($queryget))
{
$id = $row['id'];
$date = $row['date'];
$name = $row['name'];
$email = $row['email'];
$song = $row['song'];
$part = $row['part'];
$comments = $row['comments'];

//processing data

echo "
<table>
<tr>
<td>
<b>Posted by:</b> $name ($email)<br />
<b>$date</b><br />
<b>Favorite Pat Song:</b> $song<br />
<b>Favorite Part of the Site:</b> $part
</td>
</tr>
<tr>
<td>
".nl2br(strip_tags($comments))."
<hr />
</td>
</tr>
</table>
";

}

if ($_POST['submit']); 
{

$date = date("Y-m-d");
$name = $_POST['name'];
$comments = $_POST['comments'];


if ($name&&$comments)
{

$queryget = mysql_query("INSERT INTO guestbook (id,date, name, email, song, part, comments) VALUES ('','$date','$name','$email','$song','$part','$comments')"); 
echo "Please wait... <meta http-equiv='refresh' content='2'>"; 

echo mysql_error();  

}
else 

echo "Please enter your name and comments";
}

?>

 

And my HTML form is:

 

<form action="process.php" method="post"> 
<table width="500" border="0" cellspacing="2" cellpadding="0"> 
<tr valign="top"> 
<td width="500px" class="name"><span style="color:#ff0000"><b>*</b></span>Name:</td> 
<td width="500px"><input name="name" type="text" id="name" size="32" /></td> 
</tr>
<tr valign="top"> 
<td width="500px" class="bodytext"> Email Address:</td> 
<td width="500px"><input name="email" type="text" id="email" size="32" /></td> 
</tr>
<tr valign="top"> 
<td width="500px" class="pat"> Your Favorite Pat Song?:</td> 
<td width="500px"><input name="song" type="text" id="song" size="32" /></td> 
</tr> 
<tr valign="top"> 
<td width="500px" class="part"> Your Favorite Part of my Site?:</td> 
<td width="500px"><input name="part" type="text" id="part" size="32" /></td> 
</tr> 
<tr valign="top"> 
<td width="500px" class="comments"><span style="color:#ff0000"><b>*</b></span> Comment:</td>
<td width="500px"><textarea name="comments" id="comments" cols="28" rows="6" class="comments"></textarea></td> 
</tr> 
<tr valign="top"> 
<td class="bodytext"> </td> 
<td align="left" valign="top"><input type="submit" name="Submit" value="Post" class="btn" /></td> 
</tr> 
</table> 
</form>

 

 

 

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.