Ask Question

Menu

Insert Data Into MySQL Using PHP


For insert data in MySQL first we have to create a table in data base.

Here we using 3 file for insert data in MySQL:

CREATE TABLE `employee` (
`userid` int(8) NOT NULL,
`first_name` varchar(55) NOT NULL,
`last_name` varchar(55) NOT NULL,
`city_name` varchar(55) NOT NULL,
`email` varchar(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

database.php

<?php
$url='127.0.0.1:3306';
$username='root';
$password='';
$conn=mysqli_connect($url,$username,$password,"crud");
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
?>

insert.php

<!DOCTYPE html>
<html>
<body>
<form method="post" action="process.php">
First name:<br>
<input type="text" name="first_name">
<br>
Last name:<br>
<input type="text" name="last_name">
<br>
City name:<br>
<input type="text" name="city_name">
<br>
Email Id:<br>
<input type="email" name="email">
<br><br>
<input type="submit" name="save" value="submit">
</form>
</body>
</html>

process.php

<?php
include_once 'database.php';
if(isset($_POST['save']))
{
// variables for input data

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$city_name = $_POST['city_name'];
$email = $_POST['email'];
// sql query for inserting data into database

mysqli_query($conn,"insert into employee(first_name,last_name,city_name,email) values ('$first_name','$last_name','$city_name','$email')") or die(mysqli_error());
echo "<p align=center>Data Added Successfully.</p>";
}
?>






Subscribe with us to get latest topic update






Choose a Language



Subscribe