Wednesday, October 17, 2012

Inserting a data into MySQL Database using PHP

SIMPLE SIGNUP PAGE

Step 1: Creating Database on mysql
  
create database myweb;
create table signup; 
(
fn char(50) not null,
cor char(50) not null,
center char(50) not null,
dob date,
pn char(50));

fn stands for first name,
cor stands for course,
center stands for institution name,
dob stands for date of birth,
pn stands for parents name.

Step 2: HTML Signup Page

 <!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>  
 </head>  
 <body>  
 <form method="post" action="update.php">  
 <table width="424" height="353" >  
  <tr>  
   <th height="49" colspan="3" scope="col">STUDENT SIGN UP</th>  
  </tr>  
  <tr>  
   <td width="31" height="250">&nbsp;</td>  
   <td width="344"><table width="418" >  
    <tr>  
     <td>NAME</td>  
     <th width="146" scope="col"><input type="text" name="name" id="textfield" /></th>  
     <th width="62" scope="col">&nbsp;</th>  
    </tr>  
    <tr>  
     <td>COURSE</td>  
     <td><input type="text" name="cor" id="textfield2" /></td>  
     <td>&nbsp;</td>  
    </tr>  
    <tr>  
     <td>CENTER</td>  
     <td><input type="text" name="center" id="textfield3" /></td>  
     <td>&nbsp;</td>  
    </tr>  
    <tr>  
     <td>DATE OF BIRTH</td>  
     <td><input type="text" name="dob" id="textfield4" /></td>  
     <td>&nbsp;</td>  
    </tr>  
    <tr>  
     <td>PARENTS NAME</td>  
     <td><input type="text" name="pn" id="textfield5" /></td>  
     <td>&nbsp;</td>  
    </tr>  
    <tr>  
     <td>&nbsp;</td>  
     <td><input type="submit" name="button" id="button" value="Sign Up" /></td>  
     <td>&nbsp;</td>  
    </tr>  
   </table></td>  
   <td width="27">&nbsp;</td>  
  </tr>  
  <tr>  
   <td height="44" colspan="3">&nbsp;</td>  
  </tr>  
 </table>  
 </form>  
 </body>  
 </html>  



                                                          
 Step 2: PHP INSERTING Page

 <!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>  
 </head>  
 <?php  
 $name=$_POST[name];  
 $cor=$_POST[cor];  
 $center=$_POST[center];  
 $dob=$_POST[dob];  
 $pn=$_POST[pn];  
 $con = mysql_connect("localhost","root","");  
 if (!$con)  
  {  
  die('Could not connect: ' );  
  }  
 mysql_select_db("hari", $con);  
 $sql="INSERT INTO signup(NAME,cor,center,dob,pn)  
 VALUES  
 ('$_POST[name]','$_POST[cor]','$_POST[center]','$_POST[dob]','$_POST[pn]')";  
 if (!mysql_query($sql,$con))  
  {  
  die('Error: ' . mysql_error());  
  }  
 echo "1 record added";  
 mysql_close($con);  
 ?>   
 <table border="1" style="position:absolute;  
 left:100px;  
 top:150px;">  
  <tr>  
   <td colspan="2"><div align="center">Registered Successfully</div></td>  
  </tr>  
  <tr>  
   <td width="238">Name is</td>  
   <td width="72"><?php echo $name; ?></td>  
  </tr>  
  <tr>  
   <td>Course is;</td>  
   <td><?php echo $cor; ?></td>  
  </tr>  
  <tr>  
   <td>Center is</td>  
   <td><?php echo $center; ?></td>  
  </tr>  
  <tr>  
   <td>Date of Birth is;</td>  
   <td><?php echo $dob; ?></td>  
  </tr>  
  <tr>  
   <td>Parent Name;</td>  
   <td><?php echo $pn; ?></td>  
  </tr>  
 </table>  
 </body>  
 </html>  

NOW TRY YOUR OWN...ALL THE BEST
dd