Monday, 22 April 2013

How to insert records in table using PHP + MySQL ???

HTML and PHP code to insert new record 

HTML page having radio button list and checkbox list

Copy and paste this code into the html body part


<form id="form1" name="form1" method="POST" action="">
<table>
<tr><td colspan="2"><h1>REGISTRATION FORM</h1></td></tr>
<tr><td>Username</td><td><input type="text" name="username" value="" /></td></tr>
<tr><td>password</td><td><input type="text" name="password" value="" /></td></tr>
<tr><td>confirm password</td><td><input type="text" name="repassword" value="" /></td></tr>
<tr><td>Hobbies</td><td><input type="checkbox" name="hobbies[]" value="cricket" />Cricket<input type="checkbox" name="hobbies[]" value="football" />FootBall<input type="checkbox" name="hobbies[]" value="carrom" />Carrom</td></tr>
<tr><td>Gender</td><td><input type="radio" name="gender[]" value="male" />MALE<input type="radio" name="gender[]" value="FEMALE" />FEMALE</td></tr>
<tr><td>Emailid</td><td><input type="text" name="emailid" value="" /></td></tr>
<tr><td>Mobileno</td><td><input type="text" name="mobileno" value="" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="submit"><input type="reset" name="reset" value="reset">
</td>
</tr>
</table>
</form>


php code 


<?php
if(isset($_POST['submit']))
{$s="";
 $g="";
$user=trim($_POST['username']); 
$pass=trim($_POST['password']);
$email=trim($_POST['emailid']);
$mobile=trim($_POST['mobileno']);

if(!isset($_POST['hobbies'])) 
{
echo("<p>please select your hobbies</p>\n");

else 

$ho = $_POST['hobbies'];
for($i=0; $i<sizeof($ho); $i++)
{
$s .= $ho[$i];
if (($i+1) < sizeof($ho))
$s .= ",";
}
echo "your hobbies are : "$s;echo("</p>");


if(!isset($_POST['gender'])) 
 {
echo("<p>please select your gender</p>\n");
  } 
else 
 { 
$ho = $_POST['gender'];
for($i=0; $i<sizeof($ho); $i++)
{
$g .= $ho[$i];
}
         echo "You are ".$g;echo("</p>");



$con=mysqli_connect(@"localhost","root","","phpdemo") or die("Unable to connect to the database of Mysql name :  phpdemo");


if(mysqli_query($con,"create table tblreg(id int primary key auto_increment not null,username varchar(20),password varchar(20),hobbies varchar(40),gender varchar(6),emailid varchar(30),mobileno decimal(10,0))"))
{
echo "<br>Table created successfully.<br>";
}

$sql="insert into tblreg(`id`,`username`,`password`,`hobbies`,`gender`,`emailid`,`mobileno`) values(NULL,'$user','$pass','$s','$g','$email','$mobile');";
if(mysqli_query($con,$sql))
{
echo "successfully inserted into  the registration table ";
}
else
{
   echo "error".mysqli_error();
}


No comments:

Post a Comment