Tuesday, 28 November 2017

How to insert data in database using php-mysql.

In this example I have created a database having Student Table with RollNo, StudentName, Marks field. Using php we can easily insert data into this table as I shown in following steps.


I have created Following files:
Index.html : This file contain Main Menu
<html >
<head>
<title>Untitled Document</title>
</head>
<body bgcolor="#FFFF00">
<h1>Main Menu</h1>
<hr>
<a href="insert.html">Registration of student</a><br>
<a href="remove.php">Cancel Registration</a><br>
<a href="display.php">View student List</a><br>
</body>
</html>

Insert.html

<!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>Untitled Document</title>
<style type="text/css">
.td_left {
     background-color: #CC9;
}
</style>
</head>

<body bgcolor="#CCCC00">

<form action="insert.php" method="post">
<table width="59%" height="203" border="1">
  <tr>
    <th colspan="2" scope="row">STUDENT ENTRY FORM</th>
  </tr>
  <tr>
    <th class="td_left" width="34%" scope="row">Roll No</th>
    <td width="66%">  &nbsp;
      <input name="rno" type="text" size="8" /></td>
  </tr>
  <tr>
    <th class="td_left" scope="row">Student Name</th>
    <td><input name="sname" type="text" size="20" /></td>
  </tr>
  <tr>
    <th class="td_left" scope="row">Marks</th>
    <td>&nbsp;&nbsp;<input name="marks" type="text" size="10" /></td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td>&nbsp;&nbsp;<input name="" type="submit" />&nbsp;</td>
  </tr>
</table>
</form>
<p>&nbsp;</p>
</body>
</html>

Insert.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
 <?php
$rno=$_POST['rno'];
$sname=$_POST['sname'];
$marks=$_POST['marks'];

mysql_connect("localhost","root","");
mysql_select_db("collegedb");
mysql_query("insert into student values(".$rno.",'".$sname."',".$marks.")") or die(mysql_error());
echo"<h1>Record Inserted Successfully</h1>";
echo"<a href=\"index.html\">goto main Menu </a></h1>";

?>
</body>
</html>

Remove.php
<html>


<body  bgcolor="#999933">

<form action="<?php $_SERVER['PHP_SELF']?>" method="GET">
<font bold size=12> Deletion form  </font><hr>

Enter Roll Number:
<input type="text" name="rno"><br><br>

<input type="Submit"  name="Submit" value="Remove"><br>
<A HREF = "index.html">Main menu</a><br>
</form>
<?php

$id=$_GET['rno'];
mysql_connect("localhost","root","");
mysql_select_db("collegedb");
$result=mysql_query("delete  from student where rno=".$id) or die(mysql_error());
if($result)

   echo "Record deleted successfully";
echo"<a href=\"index.html\">goto main Menu </a></h1>";

?>
</body>
</html>


Delete.php

<html>

<?php

$id=$_GET['rno'];
mysql_connect("localhost","root","");
mysql_select_db("collegedb");
$result=mysql_query("Delete * from student where rno=".$id) or die(mysql_error());
if($result)
   echo "Record deleted successfully";
echo"<a href=\"index.html\">goto main Menu </a></h1>";
?>
<body>

<form action="<?php $_SERVER['PHP_SELF']?>" method="GET">
<font bold size=12> Deletion form  </font>
Enter Roll Number:
<input type="text" name="rno"><br><br>

<input type="Submit" name="Submit" value="Remove"><br>
<A HREF = "index.html">Main menu</a><br>
</form>
</html>


Display.php











<html>
<body  bgcolor="#999933"><?phpmysql_connect("localhost","root","");
mysql_select_db("collegedb");
$result=mysql_query("select * from student ") or die(mysql_error()); 
echo "<table width=300 bgcolor=yellow align=center border=1>";
echo "<tr align=center><th>Roll No</th>";  echo "<th>Name</th>";
  echo "<th>Marks </th></tr>";
while($row = mysql_fetch_array($result))
{  
echo "<tr align=center><td>".$row['rno']."</td>";  echo "<td>".$row['sname']."</td>";
  echo "<td>".$row['marks']."</td></tr>";}echo"</table>";
?>
<a href="index.html">
<--goto main Menu </a> 
</body>
</html>

click here to download above all files and extract them to your server folder.

No comments:

Post a Comment