How to insert HTML form date into database using MySQL

In order to store data, we need a MySQL server which is available in XAMPP server. First, we have to download XAMPP from the browser. Just type download XAMPP in your browser and you will get the download link.



After you download you need to install and run the application.


You will see this type of interface and start Apache and MySQL. After that your localhost will be ready. We have already made the register form in previous article, so I am going to use that form. Note that you have to mention the input method and action in the form as shown below:



After that click on MySQL Admin to open PhpMyadmin.


Create new database. I have created and named as "sampleform"(Note: Please do not leave space between the two words that you named for database). After that create new table to storge your data.



We have created table name as "connectregister".

As, I had mentioned above that form "action=connectregister" we will create php file named as "connectregister".

Php Code to connect HTML form to database:

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="sampleform"; // Database name 
$tbl_name="connectregister"; // Table name 

// Connect to server and select database.
$conn = mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
mysqli_select_db($conn, "$db_name")or die("cannot select DB");

// Get values from form 
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$dob=$_POST['dob'];
$gender=$_POST['gender'];
$address=$_POST['address'];
$mnumber=$_POST['mnumber'];
$email=$_POST['email'];
$password=$_POST['password'];

// Insert data into mysql 
$sql="INSERT INTO $tbl_name(fname, lname, dob, gender, address, mnumber, email, password) VALUES('$fname', '$lname', '$dob', '$gender','$address', '$mnumber', '$email', '$password')";
$result=mysqli_query($conn, $sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Thank You ! Your account registered Successfully! ";
echo "<BR>";
}

else {
echo "ERROR";
}
?>

Now save this code and fill data on register form and submit.

After you submit data, you will see "successfully register" notice.

And then open phpMyadmin and check your data;

Successfully we have inserted our data to phpMyadmin.








Post a Comment

0 Comments