I second kokorico in that if you have no PHP experience then do a few other things with it first to get yourself acquainted with it. I'm always unsure when answering questions like this where the line is between essentially just giving you the answer and giving you enough that you're able to work out the bulk of it yourself. I'll give an overview on the storing a guestbook and how to access it since that's what's you asked and it isn't something you can just
know how to do with knowledge of the basics of PHP.
Right so if you have a mySQL database, first you'll want to create a table with those three pieces of data I mentioned. It should be pretty straight forward to do if the hosting service you're using offers a user interface - if not then I can explain how to do that using SQL if you want.
Do you also have mySQL installed locally to work with XAMPP? It's free to download so you should be able to get it, though it might need a bit of setting up.
Your table should have (at least) three columns, feel free to name them whatever you like:
- Name - a VARCHAR column, you'll probably want to limit it to something like 60 characters
- Message - another VARCHAR, with a much higher limit in the hundreds or thousands depending on how long you want the guestbook messages to be
- Time - a DATETIME column, to store the exact time the message was posted.
For the PHP stuff,
To access the database using PHP I use
PDO (although other interfaces are available - it might also be dependent on the version of PHP you're using). Basically what you use this for is to send SQL queries to your database, and it returns the data you asked for.
The very basic structure you'll be using when accessing your database using PDO is this:
PHP:
//Connect to the database
try {
//There will be credentials needed to access the database
//For example, on my local machine there is no password so I would have
////$servername = 'localhost'
////$username = 'root'
////$password = ''
////$dbname = 'guestbook'
//You'll have to set these/work out what they're supposed to be yourself
//When you put them on your web server they'll likely be different, so keep that in mind when creating the page there
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$SQLQuery = '' //SQL query to send to the database goes here
$stmt = $conn->prepare($SQLQuery); //This line makes sure the query you're about to send is something the database likes
//It helps stop SQL injection - basically a way to hack your database
$stmt->execute(); //sends the query to the database
//$stmt now contains the data you want
Personally, I'd build the displaying the guestbook messages half first, before the submitting new messages - but manually add some testing messages to the database first so that there's actually stuff for it to display.
I don't know how familiar you are with SQL but it's a pretty easy language to read what it's supposed to do.
To get the guestbook messages, the query you'll want to send is
SELECT name, message, time FROM guestbook ORDER BY time DESC
. This gets the three pieces of data from the guestbook and also returns them most-recent-first. (If you want oldest-first, then replace 'DESC' with 'ASC')
So if you run the code above with that as the $SQLQuery, you can then do something like this:
PHP:
foreach ($stmt as $entry) {
echo $entry["name"] . '<br />';
echo $entry["message"] . '<br />';
echo $entry["time"] . '<br />';
echo '<br />'
}
And this simply prints out each of the messages in a boring way on your page. In reality you'll want to organise it in whatever nice HTML/CSS you have for it to look nice. Whether it be in a table or a div or whatever. It's best to start with the minimum like this to confirm that it's working though.
As for adding new messages to the guestbook - I'll leave that until you've built something with PHP and mySQL first just to confirm you're comfortable with it. Of course, the more you learn there more you'll be able to know what to google to find answers yourself.
Remember you can always practice skills you've learned with other stuff, even if you're not going to make a whole project out of it. You could build a simple pokedex from the above, or a list of games you own and whether you have played them yet.
A few notes on some limitations/things I didn't go into:
- It would be best practice to have a unique column in your database called something like MessageID with an INT datatype. To be an incrementing number that is unique to every entry in the guestbook. Though this isn't strictly necessary.
- If you display the time your message was posted on your page straight from the database, it'll most likely display the time from your server's perspective - in other words it will appear wrong to someone in a different timezone. Timezones can be tricky, so I wouldn't worry about this yet.
- If there's lots and lots of messages in the guestbook, displaying all of the messages on the page is going to make it very long and affect loading times, so there should be a limit to the number it displays and have multiple pages of messages.