Core PHP,Mysql registeration form with array validation

php_register_form
Today we will discuss regarding registration form in core php . Before starting coding ,we need to create a database table .
Create below table in your phpmyadmin .If you don’t have database ,do create database first .

php mysql video tutorial in Hindi language by UnitedWebSoft




<form action="" method="post">
<table width="36%" height="290" border="0">
  <tr>
    <td width="35%">Name</td>
    <td width="65%"><input type="text" name="name" 
	value="<?php if(isset($_POST['name'])){ echo $_POST['name']; } ?>" /></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><input type="text" name="email"
	value="<?php if(isset($_POST['email'])){ echo $_POST['email']; } ?>"
	 /></td>
  </tr>
  <tr>
    <td>Password</td>
    <td><input type="password" name="password" /></td>
  </tr>
  
  
  
  <tr>
    <td>Contact</td>
    <td><input type="text" name="contact" /></td>
  </tr>
 
  <tr>
    <td>Address</td>
    <td><input type="text" name="address"  />  </td>
  </tr>
  <tr> 
  
  <tr>
    <td>gender</td>
    <td><input checked="checked" type="radio" name="gender" value="male" /> Male
	<input type="radio" name="gender" value="female" /> Female	</td>
  </tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Submit" /></td>
  </tr>
</table>

</form>
		 

After we need to have below php code to handle this form submition .

Lets start code step by step .

if(isset($_POST['submit'])){
	$error=array();

Here we checked whether submit buttonhas clicked .Then we created a null array .

	$name=$_POST['name'];
	$email=$_POST['email'];
	$password=$_POST['password'];
 	$contact=$_POST['contact'];
	$address=$_POST['address'];
    $gender=$_POST['gender'];

Above we read all form fields value entered by user.

if($name==''){
		$error[]="Name field is required";
	}
	
	if($email==''){
		$error[]="Email field is required";
	}	
	
	if($password==''){
		$error[]="Password field is required";
	}
	
	if($contact==''){
		$error[]="Contact field is required";
	}
	

Here above we are checking if user has left any form field ,we store a message to the array .

$count=count($error);
	
	if($count>0){
		//error
		<a href="http://php.net/manual/en/control-structures.foreach.php" target="_blank">foreach</a>($error as $value){
			echo $value.'<br/>';
		}
		
		
	}else{
		//sucess
			$mysqli->query("INSERT INTO members (name,email,password,contact,address,gender)
		 VALUES('$name','$email','$password','$contact','$address','$gender')");
		
		if($mysqli->error){
			echo $mysqli->error;
		}else{
			echo "Successfully Registered";
			
			
		}

Now we count array element . If array count greater than 0 ,we are showing error message using foreach loop else inserting values to our database table .

check below complete codes in one place .


		 <?php
 
if(isset($_POST['submit'])){
	$error=array();
	
	$name=$_POST['name'];
	$email=$_POST['email'];
	$password=$_POST['password'];
 	$contact=$_POST['contact'];
	$address=$_POST['address'];
    $gender=$_POST['gender'];

	if($name==''){
		$error[]="Name field is required";
	}
	
	if($email==''){
		$error[]="Email field is required";
	}	
	
	if($password==''){
		$error[]="Password field is required";
	}
	
	if($contact==''){
		$error[]="Contact field is required";
	}
	
	$count=count($error);
	
	if($count>0){
		//error
		foreach($error as $value){
			echo $value.'<br/>';
		}
		
		
	}else{
		//sucess
			$mysqli->query("INSERT INTO members (name,email,password,contact,address,gender)
		 VALUES('$name','$email','$password','$contact','$address','$gender')");
		
		if($mysqli->error){
			echo $mysqli->error;
		}else{
			echo "Successfully Registered";
			
			
		}
		
	}
	
	
	
	
	
}


?>


There we also considered php array validation ,this validation considered best server side validation because if we use client side validation like jquery/javascript validation , any user can skip by disabling JavaScript from browser . Visit our e-commerce project based PHP training syllabus
http://unitedwebsoft.in/php-training-in-delhi.php