Chapter practice task
Task: Save every contact form message to a MySQL table and list them on a private page
- Create a database and a
messagestable in phpMyAdmin. - Connect with PDO from a
db.phpfile, with credentials in your config. - Insert each valid form submission with a prepared statement.
- Build a password-protected
messages.phppage that lists the latest 50 messages, escaped.
By the end of this chapter
- You can create a database and table in phpMyAdmin
- You can connect to MySQL with PDO and handle connection errors properly
- You can insert and read data with prepared statements, safe from SQL injection
- You have a finished contact form that validates, emails and saves every message
Quick answers for this chapter
PDO or MySQLi?
PDO. It has a clean API, named placeholders and works with other databases too.
What is SQL injection?
An attack where user input changes your SQL query. Prepared statements prevent it completely when you use them for every value.
Where do I find my database details on Hostinger?
In hPanel under Databases → Management. Create a database and user there, and use the host it shows (often localhost).