Jump to content

[SOLVED] Validation


Schlo_50

Recommended Posts

I have a commerce project going, the user has to register before they can purchase so that there details can be used to login should they wish to make another purchase The following script below is working however, has no validation apart from an attempt at making sure the passwords match when submitting the form.

 

Can someone revise the code to include some validation such as making sure the username is unique please? It would be very helpful as my php book doesn't teach this, thank-you in advance.

 

<?php

$self=$_SERVER['PHP_SELF'];

$firstname=$_POST['first_name'];

$surname=$_POST['sur_name'];

$username=$_POST['user_name'];

$password=$_POST['password'];

$rpassword=$_POST['rpassword'];

 

?>

              A customer can register their details here: </p>

            <center>

              <form action="secret" method="post">

                <div align="right">First Name:

                  <input type="text" name="first_name" />

                    <br />

                  Last Name:

                  <input type="text" name="sur_name" />

                  <br />

                  Username:

                  <input type="text" name="user_name" />

                  <br />

                  Password:

                  <input type="password" name="password" />

                  <br />

                  Re-enter Password:

                  <input type="password" name="rpassword" />

                  <br />

                  <input name="submit" type="submit" />

                </div>

              </form>

            </center>

            <?php

 

    if ($password == $rpassword){

         

$conn = mysql_connect( "secret","secret","secret" )

or die( "Err:Conn" );

$rs = mysql_select_db( "dombar0_work", $conn )

or die( "Err:Db" );

$sql = "INSERT INTO users ( first_name,sur_name,user_name,password ) VALUES

( \"$firstname\",\"$surname\",\"$username\",

                             password(\"$password\") )";

 

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

if ($rs){ echo( "Registration Complete $username!" ); }

}

?>

 

Thanks!  ;)

Link to comment
Share on other sites

Just do a select for the username to check if it exists in the database before doing your insert like:

 

<?php
$self=$_SERVER['PHP_SELF'];
$firstname=$_POST['first_name'];
$surname=$_POST['sur_name'];
$username=$_POST['user_name'];
$password=$_POST['password'];
$rpassword=$_POST['rpassword'];

?>
              A customer can register their details here: </p>
            <center>
              <form action="secret" method="post">
                <div align="right">First Name:
                  <input type="text" name="first_name" />
                    

                  Last Name:
                  <input type="text" name="sur_name" />
                  

                  Username:
                  <input type="text" name="user_name" />
                  

                  Password:
                  <input type="password" name="password" />
                  

                  Re-enter Password:
                  <input type="password" name="rpassword" />
                  

                  <input name="submit" type="submit" />
                </div>
              </form>
            </center>
            <?php

    if ($password == $rpassword){
         
   $conn = mysql_connect( "secret","secret","secret" )
                                             or die( "Err:Conn" );
   $rs = mysql_select_db( "dombar0_work", $conn ) 
            or die( "Err:Db" );   

   $sql = "SELECT username FROM users WHERE username='$username";
   $rs = mysql_query( $sql, $conn );
   if (mysql_num_rows ($rs)) {
    echo ("Please choose a different user name");
  }else{
   $sql = "INSERT INTO users ( first_name,sur_name,user_name,password ) VALUES
         ( \"$firstname\",\"$surname\",\"$username\",
                             password(\"$password\") )";
                  
   $rs = mysql_query( $sql, $conn );
   if ($rs){ echo( "Registration Complete $username!" ); }
  }

}                                                                                   
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.