Post by bfr on Nov 13, 2005 14:10:07 GMT -5
I just finished making a simple guestbook with PHP + MYSQL
Here is the SQL Query code:
Here is a minimal version:
Here a version with CSS stylings and such:
You all are free to use it, and I will explain it to anybody who doesn''t understand it. The only parts you need to edit are the three mySQL connection paramaters.
I posted this mostly to explain, because none of you are probably going to use it anyways...
I'll be frequently updating it.
Here is the SQL Query code:
CREATE TABLE `guestbook` (
`email` VARCHAR(20),
`message` VARCHAR(200), NULL,
`ipaddress` VARCHAR(10), NULL,
`id` INT AUTO_INCREMENT,
`thedate` VARCHAR(30) NULL
PRIMARY KEY(`guestbook`)
);
Here is a minimal version:
<html>
<body>
<!--Simple PHP Guestbook-->
<form action="guestbookg.php" method="post">
Email: <input type="text" name="email" /><br />
Message:
<textarea rows="5" cols="40" name="message">
</textarea>
<br />
<input type="submit" value="Submit" name="postp" onclick="function () {this.disabled = 'disabled' }" />
<input type="reset" />
</form>
<?php
//Connect to database
$dbConn = mysql_connect ('host', 'root', 'password');
$dbSelect = mysql_select_db ('testdb', $dbConn);
$dbQuerya = mysql_query ("SELECT * FROM guestbook ORDER BY id DESC") or die(mysql_error());
//Set up database variables
$ips = Explode (".",$_SERVER['REMOTE_ADDR']);
$theip = $ips[0];
$email = $_POST['email'];
$message = $_POST['message'];
$tdate = date("F j, Y, g:i a");
//Put post in database
if(isset($_POST['postp']))
{
if($message!="")
{
if($email=="")$email="NULL";
$dbQuery = mysql_query("INSERT INTO guestbook (email,message,ipaddress,thedate) VALUES('$email','$message','$theip','$tdate')");
}
}
//Display posts
while ($myRow = mysql_fetch_array ($dbQuerya))
{
echo '<br />';
echo '<a href="mailto:';
echo $myRow ['email'];
echo '">From IP: ';
echo $myRow ['ipaddress'];
echo '...</a>';
echo ' At: ';
echo $myRow ['thedate'];
echo '<br />';
echo $myRow ['message'];
}
//Close connection
mysql_close ();
?>
</body>
</html>
Here a version with CSS stylings and such:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>MY Guestbook</title>
<style type="text/css">
.myguestbook {
width: 400px;
border: outset #333333;
border-width: 3px 0;
background: #0000CC;
font-family: "Trebuchet MS", Arial, Verdana, sans-serif;
color: #0066FF;
}
.inputtext {
background: #0066FF;
}
.theposts {
width: 400px;
background: ##0000CC;
border: outset #333333;
border-width: 3px 0;
font-family: "Trebuchet MS", Arial, Verdana, sans-serif;
color: #0066FF;
}
.theposts a:link {
text-decoration: none;
margin-left: 10px;
}
.theposts a:hover {
font-weight: bold;
}
.date {
font-weight: bold;
}
.thetext {
margin-left: 10px;
}
</style>
<script type="text/javascript">
function validate()
{
// the variables below are assigned to each
// form input
var gemail, gmessage;
with(window.document.myguestbook)
{
gemail = email;
gmessage = inputtext;
}
// alert the visitor if email is empty or
// if the format is not correct
else if(trim(gemail.value) != '' && !isEmail(trim(gemail.value)))
{
alert('Please enter a valid email address or leave it blank');
gemail.focus();
return false;
}
// alert the visitor if message is empty
else if(trim(gmessage.value) == '')
{
alert('Please enter your message');
gmessage.focus();
return false;
}
else
{
// when all input are correct
// return true so the form will submit
return true;
}
}
/*
Strip whitespace from the beginning and end of a string
*/
function trim(str)
{
return str.replace(/^\s+|\s+$/g,'');
}
/*
Check if a string is in valid email format.
*/
function isEmail(str)
{
var regex = /^[-_.a-z0-9]+@(([-a-z0-9]+\.)+(ad|ae|aero|af|ag|
ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|
bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|
ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|
ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|
gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|
il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|
kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|
ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|
ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|
pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|
si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|
tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|
vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2]
[0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2]
[0-4][0-9]|[2][5][0-5]))$/i;
return regex.test(str);
}
</script>
</head>
<body>
<!--Simple PHP Guestbook-->
<form action="guestbookcss.php" method="post" class="myguestbook" align="center" id="myguestbook">
Email: <input type="text" name="email" id="email" /><br />
Message:
<textarea rows="5" cols="40" name="message" class="inputtext" id="inputtext">
</textarea>
<br /><br />
<input type="submit" value="Submit" name="postp" onclick="function () {this.disabled = 'disabled' } return validate();" />
<input type="reset" />
</form>
<?php
//Connect to database
$dbConn = mysql_connect ('host', 'root', 'password');
$dbSelect = mysql_select_db ('testdb', $dbConn);
$dbQuerya = mysql_query ("SELECT * FROM guestbook ORDER BY id DESC") or die(mysql_error());
//Set up database variables
$ips = Explode (".",$_SERVER['REMOTE_ADDR']);
$theip = $ips[0];
$email = $_POST['email'];
$message = $_POST['message'];
$tdate = date("F j, Y, g:i a");
//Put post in database
if(isset($_POST['postp']))
{
if($message!="")
{
if($email=="")$email="NULL";
$dbQuery = mysql_query("INSERT INTO guestbook (email,message,ipaddress,thedate) VALUES('$email','$message','$theip','$tdate')");
}
}
//Display posts
while ($myRow = mysql_fetch_array ($dbQuerya))
{
echo '<div class="theposts';
echo '" align='.'"center'.'">';
echo '<br />';
echo '<a href="mailto:';
echo $myRow ['email'];
echo '">From IP: ';
echo $myRow ['ipaddress'];
echo '...</a>';
echo ' <b class="date">At: ';
echo $myRow ['thedate'];
echo '</b><br /><div class="thetext">';
echo $myRow ['message'];
echo '</div></div>';
}
//Close connection
mysql_close ();
?>
</body>
</html>
You all are free to use it, and I will explain it to anybody who doesn''t understand it. The only parts you need to edit are the three mySQL connection paramaters.
I posted this mostly to explain, because none of you are probably going to use it anyways...
I'll be frequently updating it.