HOW TO INSERT DATA INTO MYSQL USING HTML FORM IN PHP - CULT CODE



In this tutorial, we will learn how to insert data into MYSQL using HTML form in PHP.

1. Create a table name user.

 Use the code below to create a table:
 
CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `name` varchar(251) NOT NULL,
  `email` varchar(251) NOT NULL,
  `password` varchar(251) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
 

 

2. Now create a file connect.php:

This file will help us to connect PHP to Mysql.
<?php   
 $connect = mysqli_connect("localhost", "root", "password", "demo");
 ?>

3. Now create a file index.php:

In this file, we will create an HTML form.
<?php include('dbconfig.php'); ?>

<form method="post" action="" >
<h3>HTML FORM</h3>
<br>
<input type="text"  name="name" required placeholder="Name" />
<br>    
<br>    
<input type="email" name="email" required placeholder="Email" />
<br>   
<br>   
<input type="password" name="password" required placeholder="password" />
<br>
<br>
<button type="submit" name="register_user" value="Register">Register</button>
</form>
 

4. Now create a file dbconfig.php:

This file will help us to insert data into MySQL using PHP.

 <?php 
include('connect.php');
if (isset($_POST['register_user'])) {
 $name = mysqli_real_escape_string($connect, $_POST["name"]);
 $email = mysqli_real_escape_string($connect, $_POST["email"]);
 $password = mysqli_real_escape_string($connect, $_POST["password"]);
$sql = "INSERT INTO user (name, email, password) VALUES ('$name', '$email','$password')";
 $sucess = mysqli_query($connect, $sql);
 if($sucess){
 echo '<script>alert("Data Inserted!");</script>';
 }else{
 echo '<script>alert("Something Went Wrong!");</script>';
 }
}

?>
 

5. Run your project.

Step - 1:


 

 

 

 

 

 

Step - 2: 

Fill the Form and Click on Register.

 

 

 

 

 

 

Step - 3: 


 

 

 

Now Check the Database:

 

 

Comments

Popular posts from this blog

HOW TO UPDATE+FETCH DATA IN PHP - CULT CODE

LOGIN/SIGNUP FORM WITH SESSION IN PHP MYSQL - CULT CODE

HOW TO FETCH DATA FROM MYSQL WITH WHERE CONDITION USING PHP - CULT CODE