Jump to content

[SOLVED] Trouble with MySQL/php guestbook script


bananababe

Recommended Posts

I'm new to php and I'm trying to complete a class project where I wanted to include a guestbook on the site I created. I am trying to use this script from http://www.phpeasystep.com/phptu/15.html ( apparently the forums there are dead much to my disappointment) which is functioning, EXCEPT that it doesn't seem to be assigning the name, email and comment values to the variables because that info is not saved in the database. Only the id and date/time text areas are populated in the MySQL table and thus not showing up when the view page connects to the database to display each entry. I did not make any changes (I copied and pasted ALL the code) except to put in the correct variable info regarding my host/login/password/database name and table name. Below I xx'd out my username and password for the mysql database so that is not the problem - it is connecting to the database fine, just not assigning the variable information I think. I checked that the id's in the html match the variables. I THINK it is a setting on the table data? anyone got any ideas? I'd appreciate it very much.

 

Here's what I've got:

 

This is the php code I used to create the table in the MySQL database:

 

CREATE TABLE `guestbook` (

`id` int(4) NOT NULL auto_increment,

`name` varchar(65) NOT NULL default '',

`email` varchar(65) NOT NULL default '',

`comment` longtext NOT NULL,

`datetime` varchar(65) NOT NULL default '',

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

 

 

guestbook.php page: www.allenskillicorn.com/beta/phptests/guestbook.php

 

<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> </td>

<td> </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>

 

viewguestbook.php page: www.allenskillicorn.com/beta/phptests/viewguestbook.php

 

<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:3306"; // Host name

$username="XXXX"; // Mysql username

$password="XXXX"; // Mysql password

$db_name="allenski_guestbook"; // Database name

$tbl_name="guestbook"; // 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

?>

 

viewguestbook.php page: www.allenskillicorn.com/beta/phptests/viewguestbook.php

 

<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:3306"; // Host name

$username="XXXX"; // Mysql username

$password="XXXX"; // Mysql password

$db_name="allenski_guestbook"; // Database name

$tbl_name="guestbook"; // 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

?>

 

Link to comment
Share on other sites

The addguestbook.php script was MISSING the variables for the name, email and comment area. the script SHOULD have read as follows:

 

<?php

$host="localhost:3306"; // Host name

$username="XXXX"; // Mysql username

$password="XXXX"; // Mysql password

$db_name="allenski_guestbook"; // Database name

$tbl_name="guestbook"; // 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");

 

 

$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file; this variable initialization was missing

$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file; this variable initialization was missing

$comment=mysql_real_escape_string($_POST['comment']); //This value has to be the same as in the HTML form file; this variable initialization was missing

$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();

?>

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.