Storing signup details in mysql table using PHP

Here we will collect the signup details from the member and pass it through a validation process. The member enters his/her signup details in form in the part I of the tutorial.

Post method of form data

Note that we have assumed that the register_global is Off and all the variables are by default not available for the signupck.php page to use. So we have to use $_post['userid'] to get the values first and then go for validating and inserting. To know more on register global check this tutorial. As we have used POST methodof posting data we can collect them and use in our script.

Data validation

In the validation we will check userid, email address are in correct format or not. We will also see if userid is already there in the signup table so we will ask the member to go for another userid. If all the validation passes then we will insert the record to the table and ask the member to login. You can combine our login script as next step in the signup script. Both the scripts/ tutorials uses common table so you can use it as a single script to learn how such a system works. 

Also we have modified the script and using ctype_alnum function to check the userid for alphanumeric data only. You can use your own validation as per requirement. The basic intention is to explain how a signup script is to be developed. We have checked that userid is minimum 3 char length. 


if(isset($todo) and $todo=="post"){

$status = "OK";

// if userid is less than 3 char then status is not ok
if(!isset($userid) or strlen($userid) <3){
$msg=$msg."User id should be =3 or more than 3 char length<BR>";
$status= "NOTOK";}

$msg=$msg."User id should contain alphanumeric chars only<BR>";
$status= "NOTOK";}

if(mysql_num_rows(mysql_query("SELECT userid FROM plus_signup WHERE userid = '$userid'"))){
$msg=$msg."Userid already exists. Please try another one<BR>";
$status= "NOTOK";}

if ( strlen($password) < 3 ){
$msg=$msg."Password must be more than 3 char legth<BR>";
$status= "NOTOK";}

if ( $password <> $password2 ){
$msg=$msg."Both passwords are not matching<BR>";
$status= "NOTOK";}

if ($agree<>"yes") {
$msg=$msg."You must agree to terms and conditions<BR>";
$status= "NOTOK";}

echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Retry' onClick='history.go(-1)'>"; }else{ // if all validations are passed.
$query=mysql_query("insert into plus_signup(userid,password,email,name,sex) values('$userid','$password','$email','$name','$sex')");
echo "<font face='Verdana' size='2' color=green>Welcome, You have successfully signed up<br><br><a href=login.php>Click here to login</a><br></font>";