Schlo_50 Posted February 16, 2007 Share Posted February 16, 2007 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! Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted February 16, 2007 Share Posted February 16, 2007 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!" ); } } } ?> Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 16, 2007 Author Share Posted February 16, 2007 Thanks. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 16, 2007 Author Share Posted February 16, 2007 What about prescence checks? Is there anything i can do for that? Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted February 16, 2007 Share Posted February 16, 2007 What do you mean? Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 16, 2007 Author Share Posted February 16, 2007 Well, making sure that the fields have some kind of text entry before they are finally submitted to my database. I don't want a database full of blank fields. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 16, 2007 Share Posted February 16, 2007 just check if the variable is blank. if($password == ""){ echo "You didnt enter password!"; exit; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.