Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm making a register page for my website.
While validating the input from the user, I've used the following PHP code.
I'm Encountering an error at line 18(marked in comments)and not able to debug the same. Kindly help.

T he

What I have tried:

<?php
$name=$email=$phone=$address=$zip=$pwd1=$pwd2="";
$nameErr=$emailErr=$phoneErr=$addressErr=$zipErr=$pwd1Err=$pwd2Err="";
/*line 18*/if($_SERVER["REQUEST_METHOD"=="POST"])
{
	if(empty($_POST["fname"]))
	{$nameErr="*First name Required";}
	else
	{$name=test($_POST["fname"]);}

	$email=test($_POST["email"]);
	$result=mysqli_query($con,"SELECT*FROM user_info WHERE email='$email'");
	if(empty($_POST["email"]))
	{$emailErr="Email is required";}
	elseif(!(filter_var($email,FILTER_VALIDATE_EMAIL)))
	{$emailErr="*Invalid Email Format!";}
	elseif(mysqli_num_rows($result)>0)
	{$emailErr="*Username/Email already exists!";}
	
	if(empty($_POST["phone"]))
	{$phoneErr="Phone is Required";}
	else
	{
		$phone=test($_POST["phone"]);
		if(!preg_match("/^[0-9]*$/",$phone)||strlen($phone)<10)
		{$phoneErr="Invalid phone format";}
	}
	
	if(empty($_POST["address"]))
	{$addressErr="*Address Required";}
    else
	{$address=$_POST["address"];}

	if(empty($_POST["zip"]))
	{$zipErr="*Zipcode Required";}
	else
	{
		$zip=test($_POST["zip"]);
		if(!preg_match("/^[0-9]*$/",$zip)||strlen($zip)<6)
		{$zipErr="Invalid zip format";}
	}
	
	if(empty($_POST["pwd1"]))
	{$pwd1Err="*Password Required";}
	else
	{$pwd1=test($_POST["pwd1"]);}

	if(empty($_POST["pwd2"]))
	{$pwd2Err="*Confirm your Password";}
	else
	{$pwd2=test($_POST["pwd2"]);}
}
	
	if(!$name==""&&!$phone==""&&!$address==""&&!$zip==""&&!$pwd1==""&&!$pwd2=="")
	{
		if($pwd1==$pwd2&&isset($_POST["terms"]))
		{
			$query="INSERT INTO user_info(email,fname,lname,phone,address,city,state,zip,password)VALUES('$email','$name','$lname','$phone','$address','$city','$state','$zip','$pwd1')";
			if(mysqli_query($con,$query))
			{
				header('Location:http://localhost/TLG_Web/RegistrationSuccess.php');
			}
		}
	}

	function test($data)
	{
		$data=trim($data);
		$data=striplashes($data);
		$data=htmlspecialchars($data);
		return $data;
	}
?>
Posted
Updated 20-Nov-16 7:18am
v2

this is


if($_SERVER["REQUEST_METHOD"=="POST"])


but should be

if($_SERVER["REQUEST_METHOD"]=="POST")
 
Share this answer
 
Comments
Dishi_Gupta 20-Nov-16 14:23pm    
Thank You Sir..
you have missed the closing ) for if condition on upper line

if(empty($_POST["email"])


it should be

if(empty($_POST["email"]))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900