• Welcome to The Cave of Dragonflies forums, where the smallest bugs live alongside the strongest dragons.

    Guests are not able to post messages or even read certain areas of the forums. Now, that's boring, don't you think? Registration, on the other hand, is simple, completely free of charge, and does not require you to give out any personal information at all. As soon as you register, you can take part in some of the happy fun things at the forums such as posting messages, voting in polls, sending private messages to people and being told that this is where we drink tea and eat cod.

    Of course I'm not forcing you to do anything if you don't want to, but seriously, what have you got to lose? Five seconds of your life?

Website Questions II

IndigoClaudia

The Fool
Pronoun
She/Fae
Hey y'all, i'm back at a new website. My HTML + CSS, and general webmaster skills have gone up a level. (Sadly though, i'm not working on a pokemon website. If i have time and enough people want it, i may make a subsite which will just be Indigo's Corner 2 with better CSS, although i'll ask TCoD what they want :P its actually mostly so i have something presentable for butterfree to affiliate link to cause indigo's corner embarrases me )

That said, i'm in way too deep this time, and need some help from y'all. This is gonna be my thread for it.

(Remember last thread? I know it wasn't even a year ago and yet this gives me nostalgia- https://forums.dragonflycave.com/threads/website-questions.18576/)

Anyhow, my resources have been increased significantly, i now have access to PHP, SQL, and a few more funky stuff if i need it, and i'm using XAMPP to host it locally on my computer.

(lol i'm making this into a big deal since i know this thread will be my most visited page on the internet in a matter of weeks)

So, here we have...

Question 1

How do you make a Guestbook using PHP? I've used a few tutorials and they aren't really working.
 
Last edited:
In other news, i've dug myself a hole i cannot escape planning Indigo's Corner II. Now i'm gonna have to make it >:| (It'll be way easier though, so all it does is push back normal website development by like a month adding the side-website)
 
Last edited:
This isn't something I've done before so someone who knows more about what they're talking about, feel free to tell me I'm talking nonsense.

But the way I'd approach it would be splitting it into three things to think about:

  • How to store the guestbook. I'd use a database which I assume you have if you say you have access to SQL. There's quite a few ways to access databases with PHP, but it's probably dependant on what kind of database you have. (mySQL?) If you have a database the absolute simplest it could be is probably just to have a single table with columns for
    • The name of the person who wrote the message,
    • The message itself
    • The date it was entered.
      If you can't use a database, perhaps you could read and write to a file manually and format it in such a way that you can easily determine where each of these three pieces of information is for each entry (I'd use an XML file).
  • Reading the guestbook database/file and displaying it on your site. Basically, when you query your guestbook it should return a list of objects representing a guestbook message, each of which contains the three bits of data above. You'll want to loop through this list and turn each into HTML to display on your page
  • Adding new messages to the guestbook. This bit should be somewhat simple, at least compared to the other parts. You just have a form to enter a name and a message, and it adds it to your database/file when you submit. You either have the page reload after you submit so that it shows all messages (including the one just entered) on the page once you submit, or you could have it add the new message onto the page using javascript so then it doesn't have to reload but still shows that it was added.
I know I haven't gone into much detail on the how you do each of these things - that's mainly because a lot of it is dependent on how the guestbook is going to be stored server-side, that kind of thing. I should be able to help more whichever way you go about it - so let me know if you need anything.
 
This isn't something I've done before so someone who knows more about what they're talking about, feel free to tell me I'm talking nonsense.

But the way I'd approach it would be splitting it into three things to think about:

  • How to store the guestbook. I'd use a database which I assume you have if you say you have access to SQL. There's quite a few ways to access databases with PHP, but it's probably dependant on what kind of database you have. (mySQL?) If you have a database the absolute simplest it could be is probably just to have a single table with columns for
    • The name of the person who wrote the message,
    • The message itself
    • The date it was entered.
      If you can't use a database, perhaps you could read and write to a file manually and format it in such a way that you can easily determine where each of these three pieces of information is for each entry (I'd use an XML file).
  • Reading the guestbook database/file and displaying it on your site. Basically, when you query your guestbook it should return a list of objects representing a guestbook message, each of which contains the three bits of data above. You'll want to loop through this list and turn each into HTML to display on your page
  • Adding new messages to the guestbook. This bit should be somewhat simple, at least compared to the other parts. You just have a form to enter a name and a message, and it adds it to your database/file when you submit. You either have the page reload after you submit so that it shows all messages (including the one just entered) on the page once you submit, or you could have it add the new message onto the page using javascript so then it doesn't have to reload but still shows that it was added.
I know I haven't gone into much detail on the how you do each of these things - that's mainly because a lot of it is dependent on how the guestbook is going to be stored server-side, that kind of thing. I should be able to help more whichever way you go about it - so let me know if you need anything.

Thanks for your answer :D
I indeed have mySQL, no need to use an xml file (😅 what even is an xml file amirite hahh)
So i'm actually not sure how to do any of these things except for the form, which i could probably do through html. As far as javascript and php and all that fancy stuff though, i'm a bit out of the loop. How would i go about doing this exactly?
 
If you don't know anything at all yet about PHP and server-side programming, and you're serious about learning, I wonder if it might be worth following some tutorials or something before you start trying to make your own website from scratch.

One resource I've heard good things about is freeCodeCamp's PHP course on YouTube - it's about 5 hours long, although obviously you don't have to watch it all at once. Or if you prefer to learn from books there's this, which is a newer edition of the book I learned PHP from!
 
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.
 
Last edited:
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.

Alright, so i've been trying to set up the tables in SQL but i realised that i'm not currently sure how to restrict a certain amount of characters (such as not letting a name go above 100 or so characters, or a message go above 1,500 characters). Thankfully the SQL thingy i'm using has an interface, so it's easier for me to understand.
 
It should be working slightly. I don't know why but i tend to avoid messing around with CSS if i can help it for now, so that should be great.
 
Back
Top Bottom