Fork me on GitHub

Ultimate Guestbook Tutorial: How to build a Guestbook with a honeypot, error checking, IP banning, pagination, e-mail notification and smilies with PHP and mySQL

Ultimate Guestbook Tutorial: How to build a Guestbook with a honeypot, error checking, IP banning, pagination, e-mail notification and smilies with PHP and mySQL

In this section we will show the errors on the screen, if there are any. We will use the include method like we did to include the form.php into the skin.php file. So to do so open up your skin.php file and do:

Before:

<tr>
		<td valign="top"><?php include('form.php');?></td>
	</tr>

Add:

<?php 
		if(isset($error)){
	?>
		<tr>
			<td valign="top">
			<?php
			foreach($error as $value){
				echo $value.'<br />';
			}
			?>
			</td>
		</tr>
		<tr>
			<td height="10"></td>
		</tr>
	<?php
		}
	?>

This will show the errors straight away. But it didn’t look too good. So i added another class to my CSS document:

.errors {
	border: #999999 solid 1px;
	color: #222222;
	background: #ffe958;
	font-size: 14px; 
	padding: 10px;
}

That’s better! Now let me explain what’s going on here.

2: Checks to see if the $error variable is set, in other words, it checks to see if there were any errors or any fields that weren’t submitted. If there are errors then it progresses.

6: This line runs through the $error array (which was from the index.php page, remember all these pages that have been included can be thought of as one page or one file, romantic i know). It grabs the values which are accessible via $value. All we do here is simply print them to screen with a trailing line break.

That’s about it. The rest is just to make it sit right on the page and make it look pürdy.
Next up we need to insert some records into the database!

Posted by voidet

Categorised under PHP
Bookmark the permalink or leave a trackback.

71 Comments

  1. I make this guestbook.
    It go not me. It always write this errors:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /3w/wz.cz/m/medvede/5/includes/actions.php on line 3

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /3w/wz.cz/m/medvede/5/includes/functions.php on line 12

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /3w/wz.cz/m/medvede/5/templates/entries.php on line 15

    i have it on http://medvede.wz.cz/5/index.php

    Help me, please.
    thank you

    June 2, 2008 @ 11:26 pm
  2. VoiDeT

    Hey Vladis,

    Did you make sure you have created the database correctly?

    Please make sure you have done this, otherwise this error would definitely show up.

    June 2, 2008 @ 11:32 pm
  3. I make this TABLE :

    CREATE TABLE `entries` (
    `id` int(8) NOT NULL auto_increment,
    `name` varchar(255) collate latin1_general_ci NOT NULL,
    `email` varchar(255) collate latin1_general_ci NOT NULL,
    `website` varchar(255) collate latin1_general_ci NOT NULL,
    `message` text collate latin1_general_ci NOT NULL,
    `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    and this:

    CREATE TABLE `spam` (
    `id` int(8) NOT NULL auto_increment,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`),
    KEY `ip` (`ip`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    I have in config.php this code:

    And it doesn’t go.

    June 3, 2008 @ 2:46 am
  4. I have in config.php this code:

    $host = ‘mysql.webzdarma.cz’;
    $username = ‘medvede48′;
    $password = ‘xxx’;
    $dbname = ‘guestbook’; – - I try also entries
    $email = ‘[email protected]’;
    $connect = mysql_connect($host, $username, $password);
    $dbselect = mysql_select_db($dbname);
    $items = 10;

    June 3, 2008 @ 2:47 am
  5. VoiDeT

    Can you please provide me with your ftp details?
    It could be my end, or it could be your end. But i thought i tested this script without any rows in the database. Let me know

    June 3, 2008 @ 12:21 pm
  6. I have it on: photoshopsk.wz.cz
    password: 7754705

    I have files from this tutorial.

    June 3, 2008 @ 11:50 pm
  7. VoiDeT

    And your username for me to log in please?

    June 4, 2008 @ 3:55 am
  8. (https://www.webzdarma.cz/)
    My username on FTP is : photoshopsk.wz.cz
    and password: 7754705

    (https://www.webzdarma.cz/mysql/index.php)
    And username on mysql server is: photoshopsk
    password:ragp3s

    June 5, 2008 @ 10:21 pm
  9. VoiDeT

    Those settings do not work.

    I need username, password, and address.

    Otherwise i cannot look for you.

    June 7, 2008 @ 12:20 am
  10. Look you:
    1) http://photoshopsk.wz.cz/1/1.JPG
    2) http://photoshopsk.wz.cz/1/2.JPG
    3) http://photoshopsk.wz.cz/1/3.JPG

    Do you thing this or no?
    If no this, then what you think? What of address?

    June 7, 2008 @ 1:54 am
  11. Linnea

    Hi! I just want to say thank you for a wonderful tutorial. I will probably use this at my website when I have finished it, so I can send the link later. Thank you!

    June 9, 2008 @ 9:46 pm
  12. VoiDeT

    @ Linnea – Thanks alot for your comment. I would love to see your website when you have finished with it!

    @Vladis – Doesn’t work dude. Maybe you have limited the IP range of access?

    June 9, 2008 @ 9:58 pm
  13. My action what I make.

    1.) I am download this tutorial: http://www.jotlab.com/wp-content/uploads/2008/04/guestbook.zip

    2.) I give it on a web all. (http://photoshopsk.wz.cz/)

    3.) I am create table :

    CREATE TABLE `entries` (
    `id` int(8) NOT NULL auto_increment,
    `name` varchar(255) collate latin1_general_ci NOT NULL,
    `email` varchar(255) collate latin1_general_ci NOT NULL,
    `website` varchar(255) collate latin1_general_ci NOT NULL,
    `message` text collate latin1_general_ci NOT NULL,
    `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    and this:

    CREATE TABLE `spam` (
    `id` int(8) NOT NULL auto_increment,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`),
    KEY `ip` (`ip`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    4.) I chanqe in config.php on it :

    AND IT NO GO.
    You know where is mistake???

    June 11, 2008 @ 1:43 am
  14. Hello. I am trying to make a wedding website and want to add a guestbook feature. Everything seemed to be working okay but now I get two big errors.

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/.magdalen/danfrancis/www/attempt/includes/actions.php on line 3

    and

    Fatal error: Call to undefined function: pagination() in /home/.magdalen/danfrancis/www/attempt/templates/skin.php on line 48

    Any clue what is going on? Any help would be great. The site it’s at right now (just testing it out) is: http://www.danfrancisphotography.com/attempt/index.php

    THANKS AGAIN!

    June 11, 2008 @ 8:33 am
  15. Hello there.

    It doesn’t work at myhomepage.. Can you please tell me, what I’ve done wrong?

    - Lily.

    June 11, 2008 @ 11:35 pm
  16. VoiDeT

    Hey people,

    I don’t know why you are having these problems. It sounds like an error in the SQL. I am happy to look on your server if you provide me with the correct FTP details or cpanel details.

    I have installed this script from the zip file and it works fine.

    Thank you

    June 11, 2008 @ 11:44 pm
  17. What do you mean with the correct FTP details or cpanel details?

    June 12, 2008 @ 12:14 am
  18. VoiDeT

    However you upload the files to your server,
    so i can see what the problem is. Because i cant replicate it

    June 12, 2008 @ 12:18 am
  19. The only thing I’ve changed is the MySQL otherwise I haven’t touched anything. The same text as Amanda got I have at my page.

    June 12, 2008 @ 12:27 am
  20. VoiDeT

    Yep,
    what sql did you change?
    the connection settings?

    June 12, 2008 @ 12:40 am
  21. Amazing!

    Everything works fine.
    I am new PHPier and found many useful tips & tricks!

    Keep it up VoiDeT, I will do all PHP tuts here.

    Thanks
    Mohammad
    hattoon.com

    June 22, 2008 @ 10:07 pm
  22. HCF

    Hi, awesome tutorial, shows exactly how to use the basics. 2 questions regarding your techniques:
    1. What about using mySQLi instead of the usual mySQL (only PHP5, but way better), since it is faster and more secure.
    2. I guess this was designed for beginner and advanced user, so it would be useful to show a lil bit of object oriented programming, since it makes the source code more accessible and php more flexible.

    Awesome work, greetings from Germany.

    July 5, 2008 @ 9:09 pm
  23. VoiDeT

    HCF!

    Vielen dank für ihre nette antwort. Du hast auf Englisch geschreiben, so ich werde auf Deutsch antworten. Es freut mich so viel das du die tutorial magst. So danke noch mal. Ich hab noch nicht viele mysqli probiert. Aber ich weiß das es OOP ist. Für dieser tutorial will ich sehr einfach machen, aber vielleicht lern ich MySQLi für meine selber projekte.

    Vielen dank noch mal,

    July 5, 2008 @ 9:18 pm
  24. Hi voidet,

    I like the icons you use to identify the country, OS and browser. How did you do that?

    July 7, 2008 @ 12:08 am
  25. VoiDeT

    Firestats plugin mate ;)

    July 7, 2008 @ 12:11 am
  26. Akira

    Nice guestbook. I like it. :)

    July 30, 2008 @ 4:42 am
  27. Awesome work! I have a guestbook on my existing site but….to put it mildly….it was hacked together to make it work. You have sure a great example here of the things you can do with PHP/MySQL. Keep up the great work, and happy coding!!

    August 9, 2008 @ 10:21 am
  28. I havent read the code in detail but wouldnt it be better to do the error handling client side ?.

    August 11, 2008 @ 9:51 pm
  29. VoiDeT

    Doing just client side opens you up to attacks.
    This is a server side tutorial anyhow. By just doing client side error checking would be insecure and negligent.

    August 12, 2008 @ 12:06 am
  30. mm

    hi – this is really a great tutorial. Im very glad you did this as I was confused about how to add a honeypot.

    listen, do you know how to add an ADMIN page/function to this GB, or is it already there? I was testing mine and managed to ban my own IP! I’m not sure how to manage the database at this point. Any help is appreciated.
    Thanks!

    August 24, 2008 @ 3:55 pm
  31. VoiDeT

    Hey,

    Simply go into phpmyadmin and delete the entry in the banlist that is associated with your IP address :)

    Thanks for the comments!

    August 24, 2008 @ 4:02 pm
  32. This form needs CAPTCHA security image.
    GL.

    September 11, 2008 @ 4:45 pm
  33. Pick Nick

    Just want to see what kind of icon there is for Linux…

    January 30, 2009 @ 7:47 pm
  34. you can delete my posts now, i needed to know if this supported line breaks, which it does.

    thanks

    February 16, 2009 @ 5:15 pm
  35. Hi:
    Where is the tutorial? Do I have to login to see it or I’m just blind. Thank you.

    February 26, 2009 @ 8:17 am
  36. Hi m8 brilliant tutorial. Just wondering any way of putting the guestbook into the layout of your website? I cant quite get it right always goes into the wrong place :S

    March 5, 2009 @ 6:36 am
    • VoiDeT

      Hey Martin!
      Thanks alot for the reply!
      After seeing some bad comments here from some brainless morons, you make me want to write more tutorials!
      I would need to see your page however, then i can give you some feedback on why it isn’t sitting correctly on your page :D
      Thanks,
      VoiDeT

      March 5, 2009 @ 12:45 pm
  37. Norway

    the same problem i got the error

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:AppServwwwgbincludesactions.php on line 3

    March 22, 2009 @ 6:52 am
  38. VoiDeT

    Norway,
    Try putting some content into your database.
    I really should of built in more tests for this tutorial. However it was a tutorial, not a script give away. I will see if i get some free time soon to patch it up a bit more.

    March 28, 2009 @ 12:43 pm
  39. weeman

    Hi
    Thanks for the great tutorial!

    I’m just wondering: How do I make a “new line”-character from the form show up as a new line in entries? Just like all entries in this guestbook we’re writing in now show up nicely formatted with new lines.

    new line
    new line
    new line

    instead of
    new line new line new line

    March 29, 2009 @ 10:49 pm
    • VoiDeT

      Doesn’t the guestbook show newline characters? I mean doesn’t the text drop down a new line? New line characters are written like “n” or a return is “r”. Thanks for the comments Weeman!

      March 30, 2009 @ 11:33 am
  40. Robi Santoso

    Great tutorial, thanks a lot for your contributions. I’m newbie in php subject…
    so it’s very useful for me… :)

    May 12, 2009 @ 5:47 pm
  41. i have the same problem:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a9819450/public_html/includes/actions.php on line 3 :!:

    May 20, 2009 @ 6:46 pm
  42. I am also having a bit of trouble with setting this guestbook up. I think i’ve tracked the issue to ‘$query’, as this is the one thing that all the errors have in common. Please, i’m new to this. thanks.

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/blazesa/public_html/chamvil/guestbook/includes/actions.php on line 3

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/blazesa/public_html/chamvil/guestbook/includes/functions.php on line 12

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/blazesa/public_html/chamvil/guestbook/templates/entries.php on line 10

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/blazesa/public_html/chamvil/guestbook/templates/entries.php on line 15

    the actual code, in order…..

    $spamip = mysql_num_rows($query);

    $rows = mysql_num_rows($query);

    $rows = mysql_num_rows($query);

    while($row = mysql_fetch_array($query)){

    May 25, 2009 @ 9:16 pm
  43. ok cool, it has to do with the tables??? when i go into myphpadmin i see that there are no tables. could this be the problem….

    guestbook.sql

    CREATE TABLE `entries` (
    `id` int(8) NOT NULL auto_increment,
    `name` varchar(255) collate latin1_general_ci NOT NULL,
    `email` varchar(255) collate latin1_general_ci NOT NULL,
    `website` varchar(255) collate latin1_general_ci NOT NULL,
    `message` text collate latin1_general_ci NOT NULL,
    `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    CREATE TABLE `spam` (
    `id` int(8) NOT NULL auto_increment,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`),
    KEY `ip` (`ip`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    May 25, 2009 @ 9:45 pm
  44. well what do you know. i imported the ‘guestbook.sql’ file into the database using phpmyadmin and bingo, alls well that ends well…. thanks for the tutorial. it really helped me a shit load

    May 25, 2009 @ 10:01 pm
  45. Well, at first I have the problem:-
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/blazesa/public_html/chamvil/guestbook/includes/actions.php on line 3

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/blazesa/public_html/chamvil/guestbook/includes/functions.php on line 12

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/blazesa/public_html/chamvil/guestbook/templates/entries.php on line 10

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/blazesa/public_html/chamvil/guestbook/templates/entries.php on line 15

    But that I change my $dbname to username_guestbook and it works fine. Thanks for the great tutorial. I successfully have created mine.. =)

    June 15, 2009 @ 2:13 pm
  46. VoiDeT

    I am getting alot of comments about mysql_num_rows errors!
    People, please check that you have the correct database connection settings in order and that your tables have been set up correctly!

    June 15, 2009 @ 3:44 pm
  47. Hello Friend,….
    Thanx for the nice tutorial its working……
    add more matter like this ….
    we all are very thankful to you..
    Bye

    June 16, 2009 @ 3:37 pm
  48. CGar

    Hello VOIDET

    I spent last night going through this tutorial and it was great and informative.

    One question I have is that I see your guestbook example has had some spambot action. Is this because there are new techniques that your tutorial doesn’t cover? I’d like to keep this kind of crap off my guest book if possible.

    Thanks for your great tutorial and your feedback.

    Best,
    CGar

    August 3, 2009 @ 3:43 am
  49. VoiDeT

    Hey Cgar,

    This is both true and unfortunate.
    I only taught one spam catching technique.
    However more can be applied if need be. Generating a captcha form, or having an ip-ban with 30 day cool off period. Running known ip blocking from black lists.
    The honey pot technique is just one! Surprisingly, it rejects quite alot!

    Let me know if i can help you out further!
    VoiDeT

    August 3, 2009 @ 4:48 pm
  50. Dennis van Duinen

    Hi there,

    A great tut! Im trying to put it on my site.

    But there is one little problem. The honeypot.. when is add this link:

    a new text field appears on my guestbook, while you where saying that it was hidden?

    How is that possible?

    Dennis

    November 12, 2009 @ 10:40 pm
  51. Hi there. Thanks for the great tut. Sorry…forgot to read the last side, as I didn’t used all of the tut for my guestbook at the moment. So I implemented the guestbook in my website without asking you first. And…I’m not completly ready, still working on some things as the honeypot and the pagination.

    December 6, 2009 @ 8:39 am
  52. Hello VOIDET,

    This is the best tutorial on the internet so far! :D

    I’m stuck at stage 7 – 10,
    it looked fine until stage 7 then the succes-message never showed up. :(

    The IP thing didin’t work for me :/ so I jumped that part and now I’m trying to get the entries to work out, but it show me this message:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:xampphtdocsguestbooktemplatesentries.php on line 6

    do you have a idea of what’s wrong?
    I would be happy for any help, just contact my email!

    Regards Zime

    December 20, 2009 @ 11:02 pm
  53. VoiDeT

    @Zime:
    Thanks a lot for the kind words.
    That’s a shame that you can’t get the guestbook working. It looks as though your data isn’t being insert correctly. What you can do however is check your database for any records. If they aren’t in there then check what’s going on with data you’re inserting, and the insert commands.
    If you do see the data in there, then check what’s happening when you try and retrieve the records.

    I’m thinking i might rewrite this tutorial to use OOP with PHP5.
    Or maybe save that for a whole new tutorial.

    December 21, 2009 @ 1:45 pm
  54. Hi
    First great thanks to the author of this tutorial/workshop

    Its working great. But there is only a single problem with the website links in the db entrys they re not working.

    The link includes the hole file path i.e.(http://htdocs/mywebsite/www.pcsh.it) whats wrong?

    thx in advance and best regards
    Oli

    January 29, 2010 @ 4:37 am
  55. Robin

    THX a lot

    love this tut

    March 31, 2010 @ 11:22 pm
  56. ome tony

    hey, where can i find the turtorial?

    greetings

    June 1, 2010 @ 10:55 pm
  57. Dude, many thanks for this nice and clean tutorial. Took me about an hour to read it all up and add own commands.

    July 12, 2010 @ 12:13 am
  58. Hi!

    Great guide! Many Thanks!

    I use it on this page: “(http://www.lindesbergskateboard.se/Gastbook/guestbook/index.php)”

    I have a problem, when I have posted something and then refresh the page it posts again without Im pressing the submit button. Probably just a silly code error from my side but could you please help me?
    :)

    December 12, 2010 @ 7:21 pm
  59. hello … thanks for the guestbook is very nice … but I have a problem.
    basically when someone writes a message, I receive the email the new user who has written, but the message is displayed on the screen …. why?
    thanks in advance ….

    January 31, 2011 @ 9:10 am
  60. Amazing Tutorial. thanks. I have added free captcha, fully working if your interested I can let you have a link :D

    Cheers again

    Dan

    February 14, 2011 @ 9:46 am
  61. great instructions! thank you very much. i want to use this on my website.

    i built it with dreamweaver and linked the guestbook (index.php) to a button in my spry menu bar. when i click on it it opens the guestbook in a new window. but i want it to open it in my template, under my banner and menu bar. how can i achieve this? i tried to use <?php include('guestbook/index.php') kind of think but dreamweaver doesnt seem to recognize it.

    by the way the first time i saw any kind of web coding in my life is when i looked at your tutorial :)

    i am very desperate about this. waiting for your help. thanks

    September 6, 2011 @ 9:24 am
  62. Thanks.. this is very useful… :p

    November 30, 2011 @ 4:45 pm
  63. Wow! Finally I got a web site from where I know how to actually obtain helpful data concerning my study and knowledge.

    February 28, 2012 @ 9:54 pm
  64. Nice code…N design. I m using this code in my site….
    I like. and thank u. so much for this code and design.

    March 3, 2012 @ 5:54 am
  65. Please visit the website I entered above. These are the errors I am getting. I have been through your tutorial many times and can not see where I went wrong. If you have a few minutes and might be able to give me suggestions I would appreciate it. I am willing to also email the files so you can read the direct code.

    I really appreicate your assistance. This guestbook turtorial is awesome, just wish I could figure out where I went wrong.

    Thank you in advance.

    March 17, 2012 @ 7:10 am
  66. I used the guest book tutural. I new to this but made head way.The web site I listed is not hosted on my server. Gut book is working but have two small proublems. One somehow my own ip got baned from guest book..

    Two I dont know how to delete my test entries.
    . Pluse before I was done. I was spamed. How to I delete the entries.
    Thanks in Advance .

    JJ

    December 7, 2012 @ 12:16 pm
  67. JJ

    Got this working. But messed up something.
    The only time I can see the messages is when I don’t fill the form right.
    then the first box shows up with high lighted boxes. Then the entries below that. The a duplicate first box shows up. Any clue?
    Thanks.

    December 13, 2012 @ 5:11 pm
  68. Does your site have a contact page? I’m having problems locating it but, I’d like to shoot you an e-mail.
    I’ve got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it develop over time.

    January 7, 2013 @ 11:12 am

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

or
Links:nike air max pas chernike air max pas chernike tn pas cherray ban pas chernike air max pas chernike tn pas cherray ban pas cherray ban pas cherray ban pas cher