This article show php code of simple guestbook with adding post, remove latest post and form reset functionality. All content of article is saved in database.
Guestbook is a simple php application with ability:
Post user commit into guestbook – data are stored in mariadb/ mysql database
Remove latest user post – latest message in form is used for matching database row in DELETE sql query
Reset button reinitialize all displayed messages in space of submit form (upper part of page)
Next picture show final state of our aplication
Basic prerequisities
Before creating our application, we must consider all requirements for data stored in database.
Our database table Guestbook will store:
id (uniqe self incrementing number)
name_of_writer – text up to 30 chars,
write_date – date/ time type generated by script along current time
email – text up to 70 chars,
message_text – large text with minimal 65 535 chars.
For firstime database and table creation was used phpMyAdmin in XAMPP environment.
Setup data for database access are:
server: localhost or 127.0.0.1
database: test
name: admin
password: test*555
Database and user account is created in phpMyAdmin and first result is shown on next picture.
For quick database table creation we prepared php script with name createdatabase.php with content:
<?php // script for accessing database and first table structure establishement
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user ‚root‘ with no password) */
$postmessage = trim($postmessage); // trim possible leading whitespaces
// create DELETE query
$sql = „DELETE FROM guestbook WHERE message_text = „.“‚$postmessage'“ ;
if(mysqli_query($dbc, $sql)){
$msg = ‚Last message sucessfully removed from database.‘;
$msgClass = ‚alert-success‘;
// clear entry fileds after sucessfull deleting from database
$name =“;
$email =“;
$postmessage = “;
} else {
$msg = „ERROR: Could not able to execute $sql. “ . mysqli_error($dbc);
$msgClass = ‚alert-danger‘;
}
// end connection
mysqli_close($dbc);
};
PHP code for form reset
In some case is good way to reset all error messages displayed in form area. Following code is handy
// if reset button clicked
if(filter_has_var(INPUT_POST, ‚reset‘)){
$msg = “;
$msgClass = “; // bootstrap format for allert message with red color
$name = “;
$email = “;
$postmessage = “;
};
Outputting article stored in the database in to a Guestbook
Solution for displaying all post messages stored in a database is this. Use SELECT query SELECT * FROM guestbook ORDER BY id DESC. Last part order data in descending manner for showing latest article as first.
Then store result in output variable and fetch them row by row with while loop as it show next code:
<?php // script for accessing database for all records and then output them in page
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user ‚root‘ with no password) */
We inform you that we use cookies and other technologies on this site to function and improve the operation of the site, ensure it, provide social networking features, personalize content and ads to users, and analyze traffic and user behavior. For more information, please read our Terms of Use and Cookies. You can prevent cookies from being processed by changing the settings in your Internet browser.OkPrivacy policy