How to Create a Form in PHP MySQL
In this tutorial, we are going to see how to create a form in PHP MySQL. An HTML form is an essential component in a web application. It allows us to collect data from different users. In this tutorial, we will create a simple HTML form that will collect user data, such as username and email address. Using PHP, we will save the collected data in our MySql database table. This is a common scenario in web development, where we often need to store various information about the user.
Step 1: Create the HTML form
<html> <head> <title>Form in PHP / MySQL</title> </head> <body> <form method="post" action="controller.php"> Name : <input type="text" name="name" placeholder="Enter your name" /><br /> Email : <input type="email" name="email" placeholder="Enter your Email" /><br /> <input type="submit" value="Submit" /> </form> </body> </html>
As you can see in the HTML Form above, we have the <form> </form> tag, which contains two inputs, such as username and email, and you can always add more inputs if you want to collect more data from the user. These inputs can have different attributes, such as name, type, maximum length, etc.
Step 2: Build the processing page with PHP
Our HTML form is ready to receive user input. Now we need to create a PHP page to collect data from this form. PHP is a server-side web programming language, it performs all the tasks on the server and the end-user doesn’t see anything unless there are errors or results. Have you noticed the “action” attribute in the HTML form tag above? This points to “controller.php” and means that all input field values will be sent to “controller.php”. All we need to do now is to create “controller.php”. To simply get the values captured via the HTML form, we can write something like this:
<?php // Check if request comes from a form if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_POST["name"]; $email = $_POST["email"]; if (!isset($name)){ die("Please enter your name"); } if (!isset($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)){ die("Please enter your email address"); } print "Hello " . $name . "!, your e-mail address is ". $email; } ?>
We have used filter_vars to validate the user’s email address, so always remember to check for invalid data whenever you need to.
Step 3: Storing data in MySql
Using the PhpMyAdmin interface, you can easily create a new table to store the user’s information. You can also copy/paste the code below, it will create a new table called “users” for you in MySql>.
CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL, `name` varchar(120) NOT NULL, `email` varchar(120) NOT NULL, )AUTO_INCREMENT=1 ; ALTER TABLE `users` ADD PRIMARY KEY (`id`); ALTER TABLE `users` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
Once we have the database table, we can continue working on “controller.php”. As you can see in the example below, we have added some additional PHP code. In addition to the above code, we have added the MySql connection information that will be used to connect to MySql database. Once the connection is opened, we store the user’s entries in the database table by using MySql INSERT query.
<?php // Check if request comes from a form if ($_SERVER["REQUEST_METHOD"] == "POST") { // Mysql credentials $host = "localhost"; $username = "root"; $password = ""; $database = "users"; $name = $_POST["name"]; $email = $_POST["email"]; if (!isset($name)){ die("Please enter your name"); } if (!isset($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)){ die("Please enter your email address"); } // Open a new connection to MySQL server $mysqli = new mysqli($host, $username, $password, $database); // Show any connection errors if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); } // Prepare the statement $statement = $mysqli->prepare("INSERT INTO users (name, email) VALUES(?, ?)"); // Bind the parameteres and execute the statement $statement->bind_param('ss', $name, $email); if($statement->execute()){ print "Hello " . $name . "!, your e-mail address is ". $email; }else{ print $mysqli->error; } } ?>
The controller.php page is now ready to collect data from the HTML form.