Google+ PHP TUTORIALS ONLINE HELP FOR MEDICAL AND PROGRAMMING: 2014-03-16

Sunday 16 March 2014

Insert Data From a Form Into a Database

User Registration Form with DataBase

In this example we have discus how to Insert Data From a Form Into a Database. First all we have created HTML From and we display some "text" fields and radio Button. we have also use form validation and we use JavaScript function which is called on checkForm(form).when we click the submit button in the HTML From,the form data is sent to "insert.php".The "insert.php" file connects to a database, and retrieves the values from the form with the PHP $_POST variables.Then, the mysqli_query() function executes the INSERT INTO statement, and a new record will be added to the "Persons" table.

Example

form.html
<html>
<head>
<title>User Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<h1 align="center">User Registration Form with DataBase</h1>
<script type="text/javascript">

  function checkForm(form)
  {
    if(form.firstname.value == "") {
      alert("Error: FirstName cannot be blank!");
      form.firstname.focus();
      return false;
    }
if(form.lastname.value == "") {
      alert("Error: LastName cannot be blank!");
      form.lastname.focus();
      return false;
    }
if(form.address.value == "") {
      alert("Error: Address cannot be blank!");
      form.address.focus();
      return false;
    }
if(form.city.value == "") {
      alert("Error: City cannot be blank!");
      form.city.focus();
      return false;
    }
if(form.pincode.value == "" ||
           isNaN(form.pincode.value) ||
           form.pincode.value.length != 6 )
   {
     alert( "Please provide a pincode in the format ######." );
     form.pincode.focus() ;
     return false;
   }
 if( form.country.value == "-1" )
   {
     alert( "Please provide your Country!" );
     form.country.focus() ;
     return false;
   }

  if(form.mobileno.value == "" ||
           isNaN( form.mobileno.value) ||
           form.mobileno.value.length != 10 )
   {
     alert( "Please provide a Mobile No in the format 123." );
     form.mobileno.focus() ;
     return false;
   }

if ( ( form.sex[0].checked == false ) && ( form.sex[1].checked == false ) )
   {
   alert ( "Please choose your Gender: Male or Female" );
   return false;
   }
 
if(form.age.value == "") {
      alert("Error: Age cannot be blank!");
      form.age.focus();
      return false;
    }
var email = form.emailid.value;
  atpos = email.indexOf("@");
  dotpos = email.lastIndexOf(".");
 if (email == "" || atpos < 1 || ( dotpos - atpos < 2 ))
 {
     alert("Please enter correct email ID")
     form.emailid.focus() ;
     return false;
 }
if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
      if(form.pwd1.value.length < 6) {
        alert("Error: Password must contain at least six characters!");
        form.pwd1.focus();
        return false;
      }
      if(form.pwd1.value == form.username.value) {
        alert("Error: Password must be different from Username!");
        form.pwd1.focus();
        return false;
      }
      re = /[0-9]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one number (0-9)!");
        form.pwd1.focus();
        return false;
      }
      re = /[a-z]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one lowercase letter (a-z)!");
        form.pwd1.focus();
        return false;
      }
      re = /[A-Z]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one uppercase letter (A-Z)!");
        form.pwd1.focus();
        return false;
      }
    } else {
      alert("Error: Please check that you've entered and confirmed your password!");
      form.pwd1.focus();
      return false;
    }

    alert("You entered a valid password: " + form.pwd1.value);
    return true;
  }

</script>

<body bgcolor="#9999FF" text="#000000" leftmargin="30">
<strong>
<table align="center" border="1">
<form action="insert.php" method="post" onsubmit="return checkForm(this);">
<tr>
<td>FirstName</td>
<td><input type="text" name="firstname" size="30"></td>
</tr>
<tr>
<td>LastName</td>
<td>  <input type="text" name="lastname" size="30"></td>
</tr>

<tr>
<td>Address</td>
<td> <input type="text" name="address" size="30"></td>
</tr>

<tr>
<td>City</td>
<td>  <input type="text" name="city" size="30"></td>
</tr>

<tr>
<td>Pincode</td>
<td> <input type="text" name="pincode" size="30"></td>
</tr>

<tr>
<td>Country</td>
<td><select name="country">
           <option value="-1" selected>select..</option>
           <option value="india">India</option>
           <option value="usa">USA</option>
           <option value="canada">Canada</option>
           <option value="pakistan">Pakistan</option>
           </select></td>
</tr>

<tr>
<td>Mobile No</td>
<td> <input type="text" name="mobileno" size="30"></td>
</tr>

<tr>
<td>Gender</td>
<td><input type="radio" name="sex" value="Male"> Male
                   <input type="radio" name="sex" value="Female"> Female</td>
</tr>

<tr>
<td>Age</td>
<td>  <input type="text" name="age" size="30">    </td>
</tr>
<tr>
<td>Email Id</td>
<td> <input type="text" name="emailid" size="30"> </td>
</tr>

<tr>
<td>Password:  </td>
<td>  <input type="password" name="pwd1" size="30"></td>
</tr>
<tr>
<td>Confirm Password:  </td>
<td><input type="password" name="pwd2" size="30"></td>
</tr>

<tr>
<td></td>
<td>  <input type="submit"/></td>
</tr>
</form>
</table>
</strong>
</body>
</html>

insert.php

       <htm>
      <head>
      <title>inserData</title>
      </head>

       <body>
      <?php
          $con=mysqli_connect('localhost','root','','userregistraion');
         // Check connection
         if (mysqli_connect_errno())
         {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }

          $sql="INSERT INTO userinformation (FirstName, LastName, Address, City, 
            PinCode, Country, MobileNo, Sex, Age, Emailid, Pwd1, Pwd2)VALUES
          ('$_POST[firstname]','$_POST[lastname]','$_POST[address]','$_POST[city]','$_POST[pincode]','
 $_POST[country]','$_POST[mobileno]','$_POST[sex]','$_POST[age]','$_POST[emailid]',                  '$_POST[pwd1]','$_POST[pwd2]')";

       if (!mysqli_query($con,$sql))
      {
         die('Error: ' . mysqli_error($con));
          }
          echo "1 record added";

         mysqli_close($con);
                ?>

         </body>
        </html>

Output:


SQL result:-