Insert data in mysql using php

Step 1 :

If you Not have table to insert data Then create table in database visit post Create Table Using PHP

Step 2 :

Create HTML Form to Insert Data

InsertForm.html preview


<!DOCTYPE html>
        <h1>Insert Form []</h1>
        <form action="SaveFormData.php" method="post">
            User Name : <input type="text" name="userName" required><br>
            User Email : <input type="email" name="userEmail" required><br>
            User Phone : <input type="text" name="userPh" required><br>
            <input type="submit" value="Save Record">

Step 3 :

Create PHP Script


// SaveFormData.php []

$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'mydb';

$con = mysqli_connect($host, $user, $pass, $db);

    die('Connection Error!');
}else if(isset ($_POST['userName'],$_POST['userEmail'],$_POST['userPh'])){
    $name = $_POST['userName'];
    $email = $_POST['userEmail'];
    $ph = $_POST['userPh'];
    $sql = "insert into user(name,email,ph) values ('$name','$email','$ph')";
    $result = mysqli_query($con, $sql);
        echo 'Insert Error!';
        echo 'Data Insert Completed';

SaveFormData.php Preview

No comments:

Post a Comment