How to insert data in MySQL database in php ?

                         How to insert data in mysqli database ?

suppose you have create database in MySQL name :-"signup" . First you create database connection file "dbcon.php".

<?php

$host = "localhost";
$username = "root";
$password = "";
$database = "signup";

// connection to database

$con = mysqli_connect("$host","$username","$password","$database");


?>

by default your hostname($host) is 'localhost'. your username , password and database name is what you write in database when you create. If you are use in Xampp server all data set default as write above.
Make table in database name:- "register" . Then make columns in table like this. 

Full texts
idnameemailpassword
Then write insert query. Give file name is "register.php".

<?php
include 'dbcon.php';
if(isset($_POST['submit']))
{
    $name =  mysqli_real_escape_string($con ,$_POST['name']);
    $email =  mysqli_real_escape_string($con ,$_POST['email']);
    $password =  mysqli_real_escape_string($con ,$_POST['password']);
 $user_query = "INSERT INTO register (name,email,password) VALUES
('$name','$email','$pass')";
           if(mysqli_query($con, $user_query)){

echo "
                  <script>
                  alert('Data insert in database');
                  </script>
                  ";
           }else{
               echo "
               <script>
               alert('Data is not insert');
               </script>";
               }
?>


HTML look like :-

<?php
include 'register.php';
?>
<form method="POST" action="#" >
  <label for="inputUserName" class="form-label">Name</label>
      <input name="name" type="text" class="form-control" id="inputName"
            placeholder="Name">
  <label for="inputemail" class="form-label">Email</label>
      <input name="email" type="email" class="form-control" id="inputemail" >
<label for="inputPassword" class="form-label">password</label>
      <input name="password" type="password" class="form-control" id="inputPassword" >
<button name="submit" type="submit" class="btn btn-primary
  sub border-light">Log in</button>
</form>



Post a Comment

Previous Post Next Post