Insert Form Data into Database Table in Php

 

Insert or Add and Show data  into Database Table Using PHP: 

Code (insert.php) :-

 <?php  
 $con=mysql_connect("localhost","root","");  
 mysql_select_db("exam");  
 $fname=$_POST['fname'];  
 $lname=$_POST['lname'];  
 $dob=$_POST['dob'];  
 $org=$_POST['org'];  
 $dst=$_POST['dst'];  
 $country=$_POST['country'];  
 $state=$_POST['state'];  
 $city=$_POST['city'];  
 $query="insert into tblpersons(firstname,lastname,doj,organization,designation,countryid,state,city)values('$fname','$lname','$dob','$org','$dst','$country','$state','$city')";  
 $q=mysql_query($query,$con);  
 mysql_close($con);  
 echo "insert successfully..";  
 header("location:show.php");  
 ?>  



Code(Display Your data):-

 <html>  
 <head>  
 </head>  
 <body>  
 <p align="right" >  
 <a href="form.php" style="right:20px">click here to add person</a></p>  
 </br>  
 </br>  
 <table border="1">  
 <tr>  
 <th>Id</th>  
 <th>firstname</th>  
 <th>lastname</th>  
 <th>Date of birth</th>  
 <th>organization</th>  
 <th>designation</th>  
 <th>countryId</th>  
 <th>state</th>  
 <th>city</th>  
 <th>Action</th>  
 </tr>  
 <?php  
 $con=mysql_connect("localhost","root","");  
 mysql_select_db("exam");  
 $sql="select * from tblpersons";  
 $q=mysql_query($sql,$con);  
 while($r=mysql_fetch_array($q))  
 {  
      $id=$r['id'];  
      $fname=$r['firstname'];  
      $lname=$r['lastname'];  
      $dob=$r['doj'];  
      $org=$r['organization'];  
      $dst=$r['designation'];  
      $country=$r['countryid'];  
      $state=$r['state'];  
      $city=$r['city'];  
 ?>  
 <tr>  
      <td><?php echo $id; ?></td>  
   <td><?php echo $fname; ?></td>  
   <td><?php echo $lname; ?></td>  
   <td><?php echo $dob; ?></td>  
   <td><?php echo $org; ?></td>  
   <td><?php echo $dst; ?></td>  
   <td><?php echo $country; ?></td>  
   <td><?php echo $state; ?></td>  
   <td><?php echo $city; ?></td>  
      <td> <a href="delete.php?id=<?php echo $id; ?>">Delete</a>  
        <a href="updaterecord.php?id=<?php echo $id; ?>" >Select</a>  
 </td>  
      </tr>  
   <?php  
 }   
 mysql_close($con);  
 ?>  
 </table>  
 </body>  
 </html>  


Database Table:(backend) :-

Tablename in database:tblpersons

Output After Insert :-


Comments