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

InsertForm.html

<!DOCTYPE html>
<html>
    <head>
        <title>Ali-Mirza.blogspot.com</title>
    </head>
    <body>
        <h1>Insert Form [Ali-Mirza.blogspot.com]</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">
        </form>
    </body>
</html>

Step 3 :

Create PHP Script

SaveFormData.php

<?php
// SaveFormData.php [ali-mirza.blogspot.com]

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

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

if(!$con){
    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);
    if(!$result){
        echo 'Insert Error!';
    }else{
        echo 'Data Insert Completed';
    }
}

SaveFormData.php Preview

No comments:

Post a Comment