How to insert data into Database using PHP
1. Create Form and give name form.php, the file look like :
<form method="post" action="insert.php">
<label>Name</label>
<input type="text" name="name" />
<label>Surname</label>
<input type="text" name="surname" />
<input type="submit" name="submit" />
</form>
2. create database connection and give name db.php and create table give name tbl1, the file look like :
<?php
$con=mysqli_connect("localhost","root","root","demo") or die(mysqli_error());
3. create insert.php file , the file look like :
<?php
include "db.php";
if(isset($_POST['submit']))
{
extract($_POST);
$query=mysqli_query($con,"insert into tbl1(name,surname) value('$name','$surname')") or die(mysqli_error($con));
if($query)
{
echo "record inserted";
}
else
{
echo "error";
}
}
<form method="post" action="insert.php">
<label>Name</label>
<input type="text" name="name" />
<label>Surname</label>
<input type="text" name="surname" />
<input type="submit" name="submit" />
</form>
2. create database connection and give name db.php and create table give name tbl1, the file look like :
<?php
$con=mysqli_connect("localhost","root","root","demo") or die(mysqli_error());
3. create insert.php file , the file look like :
<?php
include "db.php";
if(isset($_POST['submit']))
{
extract($_POST);
$query=mysqli_query($con,"insert into tbl1(name,surname) value('$name','$surname')") or die(mysqli_error($con));
if($query)
{
echo "record inserted";
}
else
{
echo "error";
}
}
Comments
Post a Comment