If you have a MySQL database, then this is quite simple.
First, run this script via PhpMyAdmin:
- Code: Select all
CREATE TABLE `comments` (
`id` INT( 255 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NOT NULL ,
`comment` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM
connect.php: (fill in the data, make sure you name it connect.php)
- Code: Select all
<?php
$host = "(enter host name)";
$username = "(enter username)";
$password = "(enter password)";
$database = "(enter database)";
mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
?>
Add this to the part where you want users to view/add comments:
- Code: Select all
<html>
<?php
include 'connect.php';
$sql = mysql_query("SELECT * FROM comments ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
$name = $row['name'];
$comment = $row['comment']; ?>
<table width="200" border="0">
<tr>
<td width="61">Name:</td>
<td width="123"><?php echo $name; ?></td>
</tr>
<tr>
<td>Comment:</td>
<td><?php echo $name; ?></td>
</tr>
</table>
</br>
<?php } ?>
<p> </p>
<p>
<?php
if (isset($_GET['name']) and isset($_GET['comment']) and $_GET['submit'] == "Submit") {
$name = addslashes($_GET['name']);
$comment = addslashes($_GET['comment']);
mysql_query("INSERT INTO comments (name, comment) VALUES ('$name', '$comment')") or die(mysql_error());
} else { ?>
</p>
<form name="form1" method="get" action="">
<p> </p>
<table width="257" border="0">
<tr>
<td width="79">Name:</td>
<td width="105"><input type="text" name="name"></td>
</tr>
<tr>
<td>Comment:</td>
<td><label>
<textarea name="comment" rows="6"></textarea>
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="Submit" value="Submit">
</label></td>
</tr>
</table>
</form>
<?php } ?>
</html>