Form controller PHP page and save user data into MySQLi

0
html form controller server page and mysqli database connection

Whenever you create an HTML form and the most necessary part of the form is the form action, form method, and name of the input type. Today we will get to know how to create a controller server page for the HTML form using the PHP server page and we will save the HTML form data into MySQLi. By using this method you will get to know how to create HTML from the controller saver page.

This post is going to more helpful if you are a beginner in the PHP language. Let's create an HTML form

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>HTML Form</title>
</head>
<body>
	
	<form action="form-controller.php" method="POST">		
			<p>
				<label>Name</label>
				<input type="text" name="name">
			</p>
			<p>
				<label>Phone</label>
				<input type="tel" name="phone">
			</p>
			<p>
				<label>Email</label>
				<input type="email" name="email">
			</p>
			<p>
				<label>Message</label>
				<textarea name="message" rows="3"></textarea>
			</p>

		<button type="submit">Submit</button>
	</form>

</body>
</html>

I have input form action as form-controller.php and input the form method POST.

Let's understand the difference between GET and POST methods.

html form get and post method explain

GET method is used to submit form data from the URL and the information filled into the form can be read by the hackers.

POST method is used to submit form data that are safer than the GET method. It not possible to read by any other person because the data are encrypted when we use the POST method.

Let's create a form-controller.php file that will help us to handle all the server-side codes.

<?php 
	
	$name = $_POST["name"];
	$phone = $_POST["phone"];
	$email = $_POST["email"];
	$message = $_POST["message"];

	echo "<h1>Name of the user is {$name}</h1>";
	echo "<h1>User phone number is {$phone}</h1>";
	echo "<h1>User Email address is {$email}</h1>";
	echo "<h1>Message is :- {$message}</h1>";

 ?>

The above code is used to print the data which was submitted by the user in the HTML form. Now we will save the form data into the MySQLi database. To save form data into the MySQLi, you need to create a table into PHPMyAdmin if you want to save user data into a different database, you can create a new database in PHPMyAdmin.

First of all, open PHPMyAdmin type http://localhost/phpmyadmin/ in address make sure you have installed XAMPP local server in your computer and also started Apache server and MySQL. After that select database and click on the structure menu in the form, you need to type the name of the table which you want and select the column numbers. I hope you will create a table if I will tell you all the things briefly this article will get more longer.

Let's code again in the form-controller.php file and type few lines of code for inserting data into the database.

<?php 
	
	$name = $_POST["name"];
	$phone = $_POST["phone"];
	$email = $_POST["email"];
	$message = $_POST["message"];

	$conn = new MySQLi("localhost", "root", "", "shiv");
     if($conn->connect_error)
    	{die("Error in Mysqli connection");}

    $query = "INSERT INTO contact_table(name,phone,email,message) VALUES('{$name}','{$phone}','{$email}','{$message}')";

    $result = $conn->query($query);
    if($result) {
             echo "<h3>Values Inserted Successfully</h3>";
          } 
          else{die("Failed to Insert data into database");}

    $conn->close();    

 ?>

$conn->conect_error this function will let us know whether we have connected with the Mysqli database or not, and at the end of the server file, we need to stop the MySQL database connection using exit(); function.

In the above code localhost means name of the host, root means name of the user, "" is used for password my database has not any password so I left it blank and last one Shiv it's name of my database on which I heave create table. After executing this PHP code you will get to see the user data in the database. I hope this was easy to understand.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !