Easy PHP Guestbook Part 2 – The Database
In this part of the tutorial I will cover briefly how to setup a new database and create the php file that will have the database connection strings.
First things first, create a new database and name it guestbook, assign a new user to this database name the new user guestbook, assign a password for this user 12345 will work for now.
Creating databases and assigning users will not be covered in this tutorial, this should be covered in a help file provided by your hosting company.
Once your database has been created and a user has been added you will need to setup the tables that will store each guestbook entry.
For starters lets create a table and call it guestbookentries
You will need 5 columns in this table:
- guestbookentryid – Set this column as the primary key and set it to auto increment, the type will be BIGINT
- name – The type will be TEXT
- date – The type will be DATETIME
- website – The type will be TEXT
- entry – The type will be TEXT
I suggest using phpmyadmin to setup this table in the database, if you don’t feel up to creating this table you can download this file guestbookentries.sql
You can import that file using phpmyadmin and it will create the table for you.
Once the table has been created you need to create a new file and name it sqlcon.php and place the following code into this file.
//This line of code connects to the mysql server
//This line of code selects the correct database
@mysql_select_db($database) or die (“Unable to select database”);
Upload this file to a new folder on your server, create a folder called guestbook on your servers root and upload the sqlcon.php file to there.
You can point your browser to this file http://yourservername.com/guestbook/sqlcon.php
If the page comes up blank then everything is working as it should, if it comes up with an error then you’ve made a mistake somewhere. I’d be happy to help troubleshoot any problems you are having up to this point.
This concludes Part 2 of the tutorial
- Easy PHP Guestbook Part 5 – Retrieving from the Database
- Easy PHP Guestbook Part 4 – Submitting to the Database
- Easy PHP Guestbook Part 3 – The Form
- Easy PHP Guestbook Part 1 – Does my server meet the minimum requirements ?
- Latest WordPress Hack