登录 | 注册 | FAQ
Anonymous

Field Validation Problem

+ 发表回复

2 篇帖子 分页: 1 / 1


Field Validation Problem

Thimmaraju » 周四 5月 15, 2014 10:37 am

Hi am new in PHP, am created registration page it has to validate empty username and email, its store the value perfectly but its not validating, if i enter empty values also its store in data base, my code is this please get me out off this problem, Advance Thanks for your feedback.

<html>
<head>
<title>New User Registrion</title>
<link rel="stylesheet" type="text/css" href="style.css">
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
$UnameErr = $PasswordErr = $FnameErr = $EmailErr = " ";
$Uname=$Password = $Fname = $Email = " ";
if(isset($_POST['add']))
{


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


if (empty($_POST['Uname']))
{
$UnameErr = "UserName is required";
}
else
{
$Uname = $_POST["Uname"];
}

if (empty($_POST["Password"]))
{
$PasswordErr = "Password is required";
}
else
{
$Password =$_POST["Password"];
}

if (empty($_POST["Fname"]))
{
$FnameErr = "Name is required";
}
else
{
$Fname = $_POST["Fname"];
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$Fname))
{
$FnameErr = "Only letters and white space allowed";
}
}

if (empty($_POST["EmailID"]))
{
$EmailErr = "Email is required";
}
else
{
$Email = $_POST["EmailID"];
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$Email))
{
$EmailErr = "Invalid email format";
}
}

// }



$Lname = $_POST['Lname'];
$ContactNum = $_POST['ContactNum'];
$Address = $_POST['Address'];


$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}


$sql = "INSERT INTO userregister (Username, Password, FirstName, LAstName, EmailID, ContactNum, Address)
VALUES ('$Uname','$Password','$Fname','$Lname','$Email','$ContactNum','$Address')";

mysql_select_db('UserInformation');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Registration successfully finished\n";
echo "<a href='Login.html'>LOGIN HERE</a> ";
mysql_close($conn);
}

?>


<div id="Sign-In">

<p><span class="error">* required field.</span></p>

<form method="post" action="<?php $_SERVER["PHP_SELF"]?>">

Username:<input id="b1" type="text" name="Uname">
<span class="error">* <?php echo $UnameErr;?></span> <br><br>
Password:<input id="b1" type="password" name="Password">
<span class="error">* <?php echo $PasswordErr;?></span><br><br>
FirstName:<input id="b1" type="text" name="Fname">
<span class="error">* <?php echo $FnameErr;?></span><br><br>
LastName:<input id="b1" type="text" name="Lname"><br><br>
Email ID:<input id="b1" type="text" name="EmailID">
<span class="error">* <?php echo $EmailErr;?></span><br><br>
Contact Number:<input id="b1" type="text" name="ContactNum"><br><br>
Address:<textarea id="b2" name="Address" rows="5" cols="40"></textarea><br><br>

<input type="submit" name="add" value="Submit">

</form>
</div>

</body>
</html>
头像

Thimmaraju

  • 帖子: 1
  • 注册: 周四 5月 15, 2014 10:31 am

Re: Field Validation Problem

XainPro » 周六 5月 17, 2014 9:29 am

Thimmaraju 写道:Hi am new in PHP, am created registration page it has to validate empty username and email, its store the value perfectly but its not validating, if i enter empty values also its store in data base, my code is this please get me out off this problem, Advance Thanks for your feedback.

<html>
<head>
<title>New User Registrion</title>
<link rel="stylesheet" type="text/css" href="style.css">
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
$UnameErr = $PasswordErr = $FnameErr = $EmailErr = " ";
$Uname=$Password = $Fname = $Email = " ";
if(isset($_POST['add']))
{


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


if (empty($_POST['Uname']))
{
$UnameErr = "UserName is required";
}
else
{
$Uname = $_POST["Uname"];
}

if (empty($_POST["Password"]))
{
$PasswordErr = "Password is required";
}
else
{
$Password =$_POST["Password"];
}

if (empty($_POST["Fname"]))
{
$FnameErr = "Name is required";
}
else
{
$Fname = $_POST["Fname"];
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$Fname))
{
$FnameErr = "Only letters and white space allowed";
}
}

if (empty($_POST["EmailID"]))
{
$EmailErr = "Email is required";
}
else
{
$Email = $_POST["EmailID"];
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$Email))
{
$EmailErr = "Invalid email format";
}
}

// }



$Lname = $_POST['Lname'];
$ContactNum = $_POST['ContactNum'];
$Address = $_POST['Address'];


$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}


$sql = "INSERT INTO userregister (Username, Password, FirstName, LAstName, EmailID, ContactNum, Address)
VALUES ('$Uname','$Password','$Fname','$Lname','$Email','$ContactNum','$Address')";

mysql_select_db('UserInformation');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Registration successfully finished\n";
echo "<a href='Login.html'>LOGIN HERE</a> ";
mysql_close($conn);
}

?>


<div id="Sign-In">

<p><span class="error">* required field.</span></p>

<form method="post" action="<?php $_SERVER["PHP_SELF"]?>">

Username:<input id="b1" type="text" name="Uname">
<span class="error">* <?php echo $UnameErr;?></span> <br><br>
Password:<input id="b1" type="password" name="Password">
<span class="error">* <?php echo $PasswordErr;?></span><br><br>
FirstName:<input id="b1" type="text" name="Fname">
<span class="error">* <?php echo $FnameErr;?></span><br><br>
LastName:<input id="b1" type="text" name="Lname"><br><br>
Email ID:<input id="b1" type="text" name="EmailID">
<span class="error">* <?php echo $EmailErr;?></span><br><br>
Contact Number:<input id="b1" type="text" name="ContactNum"><br><br>
Address:<textarea id="b2" name="Address" rows="5" cols="40"></textarea><br><br>

<input type="submit" name="add" value="Submit">

</form>
</div>

</body>
</html>



Modify your code like this

mysql_select_db('UserInformation');


if(empty($UnameErr) && empty($PasswordErr) && empty($FnameErr) && empty($EmailErr)) {

$retval = mysql_query( $sql, $conn );

}

if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Registration successfully finished\n";
echo "<a href='Login.html'>LOGIN HERE</a> ";
mysql_close($conn);
}
头像

XainPro

  • 帖子: 3933
  • 注册: 周五 2月 17, 2012 8:10 pm


+ 发表回复

分页: 1 / 1