This is pretty much a basic “How to” for making a very basic guestbook that uses a database instead of text. I go over everything from making the database to the table, the html form and actual posting. If you watch this video you will pretty much see the basic idea of what a guestbook is, how they work, and how to make a basic one yourself.
This video has two parts, so to understand fully, please watch both.
Part 2 and the source after the jump
<?php
$link = mysql_connect('localhost', 'phpuser', 'phppass');
if (!$link) {
die('Not connected : ' . mysql_error());
}else{
//echo "I am connected. ";
}
// make foo the current db
$db_selected = mysql_select_db('guestbook', $link);
if (!$db_selected) {
die ('Can\'t use guestbook DB : ' . mysql_error());
}
$name = trim($_REQUEST['name']);
$comment = trim($_REQUEST['comment']);
if(strlen($name)> 0){
if(strlen($comment)> 0){
$name = mysql_real_escape_string($name);
$comment = mysql_real_escape_string($comment);
$sql = "INSERT INTO `guestbook`.`entries` (
`ID` ,
`name` ,
`date` ,
`comment`
)
VALUES (
NULL , '$name', UNIX_TIMESTAMP( ) , '$comment'
);";
mysql_query($sql);
}else{
echo "You did not put in a comment.";
}
}else{
if(strlen($comment)> 0){
echo "you got a comment, but not a name;";
}
}
$sql = "SELECT *
FROM `entries` ORDER BY `ID` DESC
LIMIT 3";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
echo $row['name']." Said: <blockquote>".$row['comment']."</blockquote><br />";
}
?>
<form action="?" method="post">
Name:
<input type="text" name="name" /><br />
Comment: <br />
<textarea name="comment"></textarea><br />
<input type="submit" value="Submit" />
</form>
June 11th, 2010 at 12:11 pm
nice one i check it too, btw when are u going to insert another tutorials Kloplop321? I am more flash dev….
June 13th, 2010 at 8:44 am
Yes, I have not quit, I just have been very busy. In fact I plan to release some more very soon. I also ventured into being a flash dev, but I found some things limiting. However, I have made a flash-based login system(even with a captcha) that interacts with PHP a long long time ago.
June 22nd, 2010 at 6:36 pm
what am i doing wrong? i copy and change only the connection but when i tried it like your way, it doesn’t show the message and name when i hit submit button.
June 22nd, 2010 at 7:48 pm
are you sure you have guestbook database fully working? That should only happen if you don’t have the MySQL working properly. Can you give my a mysql dump of your guestbook database?