Php Insert

PHP Insert new data to MYSQL database

PHP Inserting data to database

PHP Insert new data to MYSQL database

Hello guys today I will show you the basic on how to store data from PHP form to MYSQL database

To continue this tutorial you must have this following skills below if not please read previous tutorials

  • You must know how to run PHP and phpMyadmin
  • Understand the basic of PHP
  • You must know how to create database and tables
  • You must know how to connect PHP to MYSQL database using mysqli
  • You must understand how to use $_POST and $_GET
  • You must know how to write SQL query

If you think you passed the above requirements then lets proceed.

We are going to use the database that the previous tutorial used.

  • Database name: db_test
  • Table name: tbl_test
  • Table fields: id, name, age, gender

Ok! now lets start just follow the code below

Easy right? Now lets try to create simple HTML form.

If you got it right then great job! But we are still not finish yet. If you try to reload your page and check your database table it will duplicate the previous record. The reason is because the $_POST contains a value.

Their are many ways to solve this issue but for this tutorial we will use other file to execute PHP and redirect back to the form and bringing parameter to confirm the result.

We will also add some input filters to avoid stored xss attack

To start

  • Create a new PHP file name it as “add.php”
  • Point your HTML form to add.php by using action=”add.php”
  • Transfer your PHP code to action.php

So when the submit button click the action will perform to add.php



As you can see I just transfer the PHP code to add.php. So when you click the submit button it will trigger the add.php

But this is not done yet. We need to redirect it back to the form and show some message and also add some input filter



Check also