Let’s create a script which would insert the data to our database.
Imagine that you are an online magazine and want to add a form, where people could post comments.
You would need:
- An HTML form which has multiple input fields (maybe name, email, and text)
- A database table
- PHP script which would check if the form is submitted
- If submitted, insert the data into the database
In our tutorials so far, we have covered, how to:
- How to create an HTML form
- How to create a database table
- How to check if anything has been submitted
- How to insert data into the database
- How to show database records on a page
Independent exercise
- Create a new file – comments.php
- Create a database table for comments
- Write an HTML form (to give it some style, use the style code you find below)
- Open the php tags, and write an if-statement, to check if form has been submitted
- Insert the results when form has been posted
- Test it
- Show the previously added comments on the same page
Use these CSS rules in your page, to give the form some styling. Change as you like!
<style>
* {
font-family: 'Arial';
}
input[type='text'], input[type=email], textarea {
outline: none;
border: 1px solid #dadada;
background-color: #f0f0f0;
color: #000000;
font-family: 'Arial';
border-radius: 20px;
padding: 10px 20px;
font-size: 16px;
display: block;
margin-bottom: 15px;
resize: none;
width: 300px;
}
button {
padding: 10px 20px;
background-color: #000000;
color: #FFFFFF;
outline: none;
border: 0;
border-radius: 20px;
font-size: 16px;
}
textarea {
height: 60px;
}
</style>