💾 Part 4: Insert data with PHP / HTML form

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

  1. Create a new file – comments.php
  2. Create a database table for comments
  3. Write an HTML form (to give it some style, use the style code you find below)
  4. Open the php tags, and write an if-statement, to check if form has been submitted
  5. Insert the results when form has been posted
  6. Test it
  7. 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>
Scroll to Top