Insert Data into database using PHP and Mysql Database

Insert Data into database
Here is the Tutorial for how to insert data into database using php. for insert the data first create the Table in the Mysql database and then create following page.

DB.php


1
2
3
4
5
6
7
8
9
<?php
$hostname="your hostname";
$username="your database username";
$password="your database password";
$database="your database name";

mysql_connect("$hostname","$username","$password");
mysql_select_db("$database");
?>

In the DB.php mysql_connect is used for connection.then create the view file like below

Index.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<form method="post" action="Insert.php">
<table>
 <tr>
   <td>First Name:</td>
   <td><input type="text" name="fname"></td>
 </tr>
 <tr>
   <td>User Name:</td>
   <td><input type="text" name="uname"></td>
 </tr>
 <tr>
  <td>Password:</td>
  <td><input type="text" name="pwd"></td>
 </tr>
 <tr>
  <td><input type="Submit" name="btn_submit" Value="Submit"></td>
 </tr>
</table>
</form>
After Creating Index..php Create Insert.php In the Insert.php Contains the Code of PHP. When the Index.php page is Run the php code is also run because Page Action is insert.php

Insert.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
include("DB.php");
if(isset($_POST['btn_submit']))
{
 $name=$_POST['fname'];
 $uname=$_POST['uname'];
 $pass=$_POST['pwd'];
 
 $query=mysql_query("INSERT INTO demo(name,uname,password) VALUES ('$name','$uname','$pass')");
 if($query)
 {
  echo 'success';
 }
}
?>
Here is the Demo for Insert the Data into Database.

Latest
Previous
Next Post »