Friday 30 September 2011

Insert Form Data Using Php Mysql

Step 1 : Create Html Form
First Name
Last Name
Date Of Birth
Gender checked="checked" /> Male checked="checked" /> Female
Email Id "/>
Password
Confirm Password
In the above code all the field have a unique "name" and "id" is same in one field but that differ one field one to an other. First step in PHP code to connect with mysql data base.
<?php
 
//Database Information

$dbhost = "localhost";
$dbname = "priyanka";
$dbuser = "root";
$dbpass = "";

//Connect to database

mysql_connect ($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
?>
Above code connect the databse priyanka . Now to check submit button. If submit button is click its return the value . Php code to check "is button is click ".
 <?php if(isset($_POST['submit'])){     /*submit is the name of submit button " For example : if submit button name is xyz then we do $_POST[' xyz']    "      */
  
Action code is goes here 

} ?>

form method is post therefore : how we store values in php variables.
$fname = $_POST['fname'];  
  $lname  = $_POST['lname'];
 $date= $_POST['date'];
  $month= $_POST['month'];
 $year= $_POST['year']; 
  $dob = $date ."-" . $month ."-" . $year ;
  $gender = $_POST['gender'];
  $email_id= $_POST['email_id'];
   $pass= $_POST['pass'];
	 $c_pass= $_POST['c_pass'];
	 $datetime = date("m/d/y G.i:s", time());
How to use PHP insert Query :

$query = "INSERT INTO `priyanka`.`users` (`id`, `fname`, `lname`, `dbirth`, `gender`, `email_id`, `pass`, `r_date`) VALUES ('', '$fname', '$lname','$dob','$gender','$email_id','$pass','$datetime')";
	 
	$done = mysql_query($query) or die(mysql_error());
	 if($done) {
	
	 $id1= mysql_insert_id();
		$url = "thankyou.php?id=" . $id1 . ""; 
		 header("Location: " . $url); 
File contain : index.php style.css thankyou.php user.sql import this file in database download

No comments:

Post a Comment