Tuesday, 4 June 2013

how to store data into database easily and reset the form after successful submission

           

Code below will guide you how to store data into database easily. Save this as "save.php" and open it in your host e.g. localhost.
<?php
 session_start();
 // Assing value about your server to these variables for database connection
 $hostname_connect= "localhost";
 $database_connect= "olympic"; //"olympic" this is database name you may create your own database.
 $username_connect= "root";
 $password_connect= "";
 $connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
 @mysql_select_db($database_connect) or die (mysql_error());

//if you submit form data submited will be store in database
 if($_POST)
 {
 $key = mt_rand(); // this mt_rand() function genrates randum key for you
 $username = $_POST['username'];
 $email = $_POST['email'];
 $password = $_POST['password'];
 $passconf = $_POST['passconf'];
 $postal_code = $_POST['postal_code'];

 if($_POST['username']=='' || $_POST['email'] =='' || $_POST['password']=='' || $_POST['passconf']=='' || $_POST['postal_code']=='')
 {
  echo "Please fill all fields";
 }
 else
 {
 // save data info database where i have named table as "user" and this is query syntax INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
  $query = "INSERT INTO `user`(`username`,`email`,`password`,`postal_code`,`key`,`status`)VALUES('$username','$email','$password','$postal_code','$key','InActive')";
  $result=mysql_query($query);
   if(!$result)
   {
   echo "Not Saved.";
   }
   else
   {
    //store message in session this will display after redirection.
   $_SESSION["msg"] = "Data has been saved.";
   // line below will redirect you to "save.php" and your form will be reset/refreshed.
   header( "Location: save.php" );
   exit;
   }
 }
 }
 ?>
 <html>
 <body>
<?php
// if session has been set it will show message and unset session.
if(isset($_SESSION["msg"]))
{
 echo '<p style="color:#0C9; font-weight:bold; text-align:center;">'.$_SESSION["msg"].'</p>';
 unset($_SESSION["msg"]);
}
   
 ?>
 <form id='registeration' action='' method='post' >
 <fieldset >
 <legend>Register</legend>
 <label for='username' >Username*: </label><br/>
 <input type='text' name='username' id='username' value="<?php if(!empty($username)) echo $username ?>" maxlength="50" /><br/>
 <label for='email' >Email Address*:</label><br/>
 <input type='text' name='email' id='email' value="<?php if(!empty($email)) echo $email ?>" maxlength="50" /><br/>
 <label for='password' >Password*:</label><br/>
 <input type='password' name='password' id='password' maxlength="50" /><br/>
 <label for='passconf' >Confirm Password*:</label><br/>
 <input type='password' name='passconf' id='password' maxlength="50" /><br/>
 <label for='postal_code' >Postal Code*:</label><br/>
 <input type='text' name='postal_code' id='postal_code' value="<?php if(!empty($postal_code)) echo $postal_code ?>" maxlength="50" /><br/>
 <input type='submit' name='Submit' value='Submit' />
 </fieldset>
 </form>
 </body>
 </html>

 

    

0 comments:

Post a Comment