Bipsmedium

Apr 11, 2020

3 min read

How to insert HTML form data into MySQL database using PHP

Before getting started with HTML form submission we need to know the basic overview of some superglobal variables in PHP. There are some variables, we can access from anywhere in the PHP script are called superglobal variables. Some of them are defined as follows — —

1. $_SERVER: It is a global variable which holds information about headers, paths, host address, and script locations, etc. such as
. $_SERVER[‘PHP_SELF’] = Returns the filename of the currently executing script.
. $_SERVER[‘SERVER_ADDR’] = Returns the IP address of the host server.
. $_SERVER[‘SERVER_NAME’] = Returns the name of the host server such as www.websitename.com

. $_SERVER[‘REQUEST_METHOD’] = Returns the request method used to access the page.

2. $_REQUEST: It is a superglobal variable which collects data from a form after clicking the submit button.

3. $_POST: It is a global variable that is used to send form data to the server. Data sent from a form with $_POST method is invisible to others. POST method is used to send sensitive information like username and password etc.

4. $_GET: It is a global variable that is used to send form data to the server. Data sent from a form with $_GET method is visible to others via URL. It is used for sending non-sensitive data.

Example
— — — — — — -
Also read, Introduction to PHP and how it works

How to echo form data after submit in PHP

In the example above a form is given with an input field and a submit button. When a user clicks the submit button, the form data is sent to the file specified in the action attribute of the <form> tag to process the data.

Here we have used the current file for the action of the data with $_SERVER[‘PHP_SELF’] or we can leave the action attribute blank. If we want then we can use a separate PHP file for the action attribute. Then, we can use the super global variable $_REQUEST to collect the value of the input field or we can use $_POST to retrieve the form data from an input field.

Inserting form data into a database using PHP and displaying in a table

1. Create the database in PHPmyadmin with collation “utfmb4-unicode-ci”.

2. Create a table in the database. Here I am using a table named ‘users’ which contains five fields such as “id”,”username”,”phone_no”,”email”,”password”.

DDL Information of the table:
— — — — — — — — — — — — — — — — — — —
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`phone_no` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Now, we will do the submission with simple form validation in PHP

Also read, Basic overview of PHP variables and data Types

I hope this article will help you to understand the basic overview of the form submission.