Categories
PHP

How To Insert Data Into MySql In PHP

How To Insert Data Into MySql In PHP

In this tutorial i will teach you How To Insert Data Into MySql In PHP.This tutorial will be very helpful when you are thinking of insert some data into a database table through your PHP code.Here i am using a form with two fields (Employee_firstname,Employee_lastname).Method to insert the data into table i am using is PHP_SELF: (PHP_SELF is a variable that returns the current script being executed).

CODE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>How To Insert Data Into MySql In PHP</title>
<style type="text/css">
body
{
margin:0px auto;
width:980px;
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;	
}
h2
{
font-size:30px;
text-align:center;	
}
.text
{
color:#090;
padding-top:40px;
text-align:center;
font-size:14px;	
}
.hr
{
clear:both;	
margin-top:60px;
border-bottom:solid 2px rgba(0,0,0,0.4);
}
.footer
{
width: 100%;
text-align: center;
padding-top: 0px;
font-size: 16px;
}
</style>
</head>
<body>

<h2>How To Insert Data Into MySql In PHP</h2>
<div class="text">
<form name="myform" method="post" action="<?php $_PHP_SELF ?>">
<label>Employee Firstname:</label><input type="text" name="fname" />
<label>Employee Lastname:</label><input type="text" name="lname" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php
if(isset($_POST['submit']))
{
$connection= mysqli_connect("localhost","root","root");
if($connection){
echo "<br><br><br><br>Connection Established";
}
else
{
echo "coonection fail";	
}
//Select Database
mysqli_select_db($connection,"demo");

//Insert Into Database
$empfname=($_POST['fname']);
$emplname=($_POST['lname']);

$sql="INSERT INTO employee (Employee_FirstName, Employee_LastName) VALUES ('$empfname', '$emplname')";
$insert=mysqli_query($connection,$sql);
if ($insert)
{
echo "<br><br>Record Inserted successfully";
}
else
{
echo "<br><br>Error Inserting Record: " . mysqli_error($connection);
}	
}
?>
</div>

<!--hr-->
<br /><br /><br /><br />
<hr />

<!--footer-->
<div class="footer">
<p>&copy; 2013 All rights reserved by HighTechnology.in
<a href="http://hightechnology.in" target="_blank">HighTechnology.in</a>
| Hosting Partner <a href="http://www.grootstech.com" target="_blank">Grootstech Solutions</a></p>
</div>
</body>
</html>

2 replies on “How To Insert Data Into MySql In PHP”

I’ve been following your site for some time now and finally got the bravery to go ahead and give you a shout out from Dallas Texas! Just wanted to tell you keep up the good work!

Comments are closed.