PHP Examples - Form - Insert Data To Database

PHP Examples - Form 
 Insert Data To Database

Program:
// php9.php
<html >
<head>
<title>PHP EXAMPLE 9 : Database with Form - Insertion </title>
</head>

<body>

<?php
 
 echo "<br/>PHP Example 9";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"Insert Form Data To DATABASE";
 echo"</h1>";
 echo"<br/>";
?>
<center>
<h2><b><u> STUDENT DETAILS </u></b></h2>
<form method="post" action="insertdata.php"><br/><br/><br/>
Roll No: <input type="text" value="" name="rollno"/><br/><br/><br/>
Name: <input type="text" value="" name="name1"/><br/><br/><br/>
Mark: <input type="text" value="" name="mark"/><br/><br/><br/>
<input type="submit" value="Submit" />

</form>
</center>
</body>
</html>




// insertdata.php

<html >
<head>

<title>PHP EXAMPLE 9 : Database with Form - Insertion</title>
</head>

<body>
<?php
 
 echo "<br/>PHP Example 9";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"Insert Form Data To DATABASE";
 echo"</h1>";
 echo"<br/>";
 
 echo"<br>CONNECTING...";
 $con = mysql_connect("localhost","root","");  // Connecting to the database
 if(!$con)
   die('Connection Failed...'.mysql_error());
 echo"<br>CONNECTED...";
 if(!mysql_select_db("DB1",$con)) 
 {
  if(mysql_query("CREATE DATABASE DB1",$con)) // Creating database
   echo"<br><br>DB1 Created...";
  else
   echo 'Error Occured...'.mysql_error();
  mysql_select_db("DB1",$con);  // Creating tables
  mysql_query(" CREATE TABLE Students(
  Rollnum int NOT NULL,
  Name varchar(20),
  Mark int,
  PRIMARY KEY(Rollnum) )",$con);
  echo"<br><br>Table  Students Created...";  
 }

 
  
 echo"<br><br>Table  Students Created...";  
   // Inserting data
 $insertst = "INSERT INTO Students VALUES ('$_POST[rollno]','$_POST[name1]','$_POST[mark]')";
 if(!mysql_query($insertst,$con))
   die( "<br/> Error...".mysql_errno());  
 echo"<br><br>Records added...<br/> Updated Table...";    
 $data= mysql_query(" SELECT * FROM Students ",$con); // Retreiving data

 echo "<table border='2'>
   <tr> 
   <th> Roll No</th>
   <th> Name</th>
   <th> Mark </th>
   </tr>";

 while($rec=mysql_fetch_array($data))
 {
  echo "<tr><td>".$rec['Rollnum']."</td><td>".$rec['Name']."</td><td>".$rec['Mark']."</td></tr>";  
 }
 echo"</table>";
 
 // mysql_query(" DROP DB1 ",$con);
 mysql_close($con);  // Closing connection

 echo "<br/><br/><br/><br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>

2 comments:

Related Posts Plugin for WordPress, Blogger...