PHP

How to insert data into MySql using PHP

In this tutorial i will explain how to insert data into MySql using PHP.
[aio_button align=”none” animation=”none” color=”red” size=”small” icon=”play” text=”View Demo” target=”_blank” relationship=”dofollow” url=”https://phpgurukul.com/demos/mysqlinsert/”] First Create a database with name demo. Now create a table with name data.
Here the strcuture of table

CREATE TABLE IF NOT EXISTS `data` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `contactno` bigint(20) NOT NULL,
  `gender` varchar(255) NOT NULL,
  `education` varchar(255) NOT NULL,
  `address` longtext NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

Code for mysql connection with php

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'demos');
$conn = mysql_connect(DB_SERVER,DB_USER,DB_PASS)
or
die('localhost connection problem'.mysql_error());
mysql_select_db(DB_NAME, $conn);
?>

Create a HTML Form for user inputs

<form name="insert" action="" method="post">
<table width="100%"  border="0">
<tr>
<th height="62" scope="row">Name </th>
<td width="71%"><input type="text" name="name" id="name" value=""  class="form-control" required />
</td>
</tr>
<tr>
<th height="62" scope="row">Email id </th>
<td width="71%"><input type="email" name="email" id="email" value=""  class="form-control" required />
</td>
</tr>
<tr>
<th height="62" scope="row">Contact no</th>
<td width="71%">
<input type="text" name="contactno" id="contactno" value=""  class="form-control" maxlength="10" required /></td>
</tr>
<tr>
<th height="62" scope="row">Gender</th>
<td width="71%"><input type="radio" name="gender" value="Male" checked >Male
<input type="radio" name="gender" value="Female" >female</td>
</tr>
<tr>
<th height="62" scope="row">Education</th>
<td width="71%"><select name="education" id="email" class="form-control" required >
<option value="">Select your Education</option>
<option value="Post Graduate">Post Graduate</option>
<option value="Graduate">Graduate</option>
<option value="Intermediate">Intermediate</option>
<option value="High School">High School</option>
<option value="Other">Other</option>
</select></td>
</tr>
<tr>
<th height="62" scope="row">Address</th>
<td width="71%"><textarea name="addrss" class="form-control"  required></textarea> </td>
</tr>
<tr>
<th height="62" scope="row"></th>
<td width="71%"><input type="submit" name="submit" value="Submit" class="btn-group-sm" /> </td>
</tr>
</table>
</form>

Now get user inputs and store in the PHP variables

$name=$_POST['name'];
$email=$_POST['email'];
$contactno=$_POST['contactno'];
$gender=$_POST['gender'];
$education=$_POST['education'];
$adress=$_POST['addrss'];

Query for insert data into mysql using PHP

$query=mysqli_query($conn,"insert into data(name,email,contactno,gender,education,address)
values('$name','$email','$contactno','$gender','$education','$adress')");

Full PHP Script for insert data into database using mysql

if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$contactno=$_POST['contactno'];
$gender=$_POST['gender'];
$education=$_POST['education'];
$adress=$_POST['addrss'];
$query=mysqli_query($conn,"insert into data(name,email,contactno,gender,education,address)
values('$name','$email','$contactno','$gender','$education','$adress')");
if($query)
{
echo "<script>alert('Data inserted successfully');</script>";
}
else
{
echo "<script>alert('Data not inserted');</script>";
}
}

Here is the full code that we have written during this tutorial:

<?php
include_once("config.php");
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$contactno=$_POST['contactno'];
$gender=$_POST['gender'];
$education=$_POST['education'];
$adress=$_POST['addrss'];
$query=mysql_query("insert into data(name,email,contactno,gender,education,address)
values('$name','$email','$contactno','$gender','$education','$adress')");
if($query)
{
echo "<script>alert('Data inserted successfully');</script>";
}
else
{
echo "<script>alert('Data not inserted');</script>";
}
}
 ?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form name="insert" action="" method="post">
<table width="100%"  border="0">
<tr>
<th height="62" scope="row">Name </th>
<td width="71%"><input type="text" name="name" id="name" value=""  class="form-control" required />
</td>
</tr>
<tr>
<th height="62" scope="row">Email id </th>
<td width="71%"><input type="email" name="email" id="email" value=""  class="form-control" required />
</td>
</tr>
<tr>
<th height="62" scope="row">Contact no</th>
<td width="71%">
<input type="text" name="contactno" id="contactno" value=""  class="form-control" maxlength="10" required /></td>
</tr>
<tr>
<th height="62" scope="row">Gender</th>
<td width="71%"><input type="radio" name="gender" value="Male" checked >Male
<input type="radio" name="gender" value="Female" >female</td>
</tr>
<tr>
<th height="62" scope="row">Education</th>
<td width="71%"><select name="education" id="email" class="form-control" required >
<option value="">Select your Education</option>
<option value="Post Graduate">Post Graduate</option>
<option value="Graduate">Graduate</option>
<option value="Intermediate">Intermediate</option>
<option value="High School">High School</option>
<option value="Other">Other</option>
</select></td>
</tr>
<tr>
<th height="62" scope="row">Address</th>
<td width="71%"><textarea name="addrss" class="form-control"  required></textarea> </td>
</tr>
<tr>
<th height="62" scope="row"></th>
<td width="71%"><input type="submit" name="submit" value="Submit" class="btn-group-sm" /> </td>
</tr>
</table>
</form>
</body>
</html>
View Demo
Download Source Code(How to insert data into MySql using PHP)
Size: 27.8 KB
Version: V 2.0

Recommended Tutorials for you


Related posts

PHP – Variables

Anuj Kumar

How to create pagination in PHP

Anuj Kumar

jQuery Dependent DropDown List – States and Districts

Anuj Kumar

Significance and Advantages of PHP Web Development

Anuj Kumar

String functions

Anuj Kumar

Get City Country By IP Address in PHP

Anuj Kumar

JQuery Ajax Tutorial – Learn About Developing Effective Next Generation Web Application

Anuj Kumar

How to convert number to String in PHP

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.