Username/password Creation in PHP and MySQL. a Simple form is designed to get the username and two passwords the objectives are
1. Both the username and password should contain atleast 6 characters
2. both the passwords should be same
3. upon submitting to the database, the existence of username is checked with the database, if the username already exists, then the user is allowed to select some other, else user is created.
//html file is given below, file name is
<h2>User Registration Form</h2>
<pre>
<form action=”userregis.php” method=”post”>
Enter the username: <input name=”username” type=”text” /> Enter the password: <input name=”password1″ type=”password” />
Confirm the password: <input name=”password2″ type=”password” />
<input type=”submit” value=”create” />
</form>
</pre>
//userregis.php
<?php
include(”dbconnect.php”);
$u=$_POST['username'];
$p1=$_POST['password1'];
$p2=$_POST['password2'];
$query1=”Select * from usertable where user=’$u’”;
$query2=”Insert into usertable values (’$u’,password(’$p1′))”;
if(strlen($u)<6 or strlen($p1)<6)
{
echo “The username and password should contain atleast 6 characters”;
}
else if($p1!=$p2)
{
echo “The Passwords should be same”;
}
else
{
$r1=mysql_query($query1) or die(”Database Selection Failed”);
$num=mysql_num_rows($r1);
if($num==1)
{
echo “The username <h3> $u </h3> is already exists”;
}
else
{
$r2=mysql_query($query2) or die(”Database Insertion Failed”);
if($r2)
echo “Username <h3> $u </h3> is successfully created”;
else
echo “Database Insertion Failed”;
}
}
?>
<?php
include(”dbconnect.php”);
$u=$_POST['username'];
$p1=$_POST['password1'];
$p2=$_POST['password2'];
$query1=”Select * from usertable where user=’$u’”;
$query2=”Insert into usertable values (’$u’,password(’$p1′))”;
if(strlen($u)<6 or strlen($p1)<6)
{
echo “The username and password should contain atleast 6 characters”;
}
else if($p1!=$p2)
{
echo “The Passwords should be same”;
}
else
{
$r1=mysql_query($query1) or die(”Database Selection Failed”);
$num=mysql_num_rows($r1);
if($num==1)
{
echo “The username <h3> $u </h3> is already exists”;
}
else
{
$r2=mysql_query($query2) or die(”Database Insertion Failed”);
if($r2)
echo “Username <h3> $u </h3> is successfully created”;
else
echo “Database Insertion Failed”;
}
}
?>
//dbconnect.php
<?php
$con=mysql_connect(”localhost”,”root”,”abc123″) or die(mysql_error());
$db=mysql_select_db(”dummy”,$con) or die (mysql_error());
?>
Comments
Post a Comment