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="";
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;
}
?>