View Full Version : register script

03-05-2007, 10:59 PM
$submit = $_GET['submit'];
$name = $_POST['name'];
$sname = $_POST['sname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
if (isset($submit))
if (!isset($username))
echo "Please enter a username.";
if (!isset($password))
echo "Please enter a password.";
else {
$count = count($password);
if (!isset($email))
echo "Please enter your VALID email address.";
if (!isset($name))
echo "Please enter your name.";
if (!isset($sname))
echo "Please enter your last name.";
if ($count < 6)
echo "Password too short.";
$datahost = localhost;
$dataname = databasename;
$datapase = databasepass;
$datauser = databaseuser;
mysql_connect($datahost, $datauser, $datapass) or
mysql_select_db($dataname) or die(mysql_error());
$check = mysql_query("SELECT username FROM users WHERE
$user_exist = mysql_num_rows($check);
if($user_exist > 0){
echo "I'm sorry but the username you specified has already
been taken. Please pick another one.";
$query = "INSERT INTO users (name, sname, email, username,
password, )
VALUES('$name', '$sname', '$email', '$username',
mysql_query($query) or die(mysql_error());
echo "You have successfully registered please click <a href
=\"/login.php\" >here</a> to log in";
if (!isset(submit))
include 'register.htm';

would this script work i have no host to test
p.s if someone has a little space for me to test i would appreciate
for some reason lycos wont work, and cant find anouther one with instant sign up

03-05-2007, 11:54 PM
instead of:

$submit = $_GET['submit'];

$submit = $_POST['submit']

03-06-2007, 12:07 AM
It'll work, but if the form fields are empty, you're not stopping the script from querying the db. It'll do the query if they're empty or filled and will result in an error. You need to set up a flag (I think that's the right term), so if an error is shown, it increases a variables value. Then before the query, check the variable is what it started off as and not been incremented by the errors

Also, just by scanning it quickly, at the bottom you have missed off a $

if (!isset($submit))

03-06-2007, 12:15 AM
Use empty() (http://www.php.net/empty) instead of isset(). isset() will always be true since you specifically the variables earlier in the script. The manual has some examples and comparisons between the two.

03-06-2007, 01:21 PM
so somehting like
if (empty($email))
die(Please Enter Email Address.);

03-06-2007, 01:47 PM
If you use die() then your script won't continue loading and will invalidate your html page as it'll not finish loading.

I'd do something a bit like this

$errormessages = array("No username entered","No password entered","etc");
$error = '';
$error .= $errormessages[0];
$error .= $errormessages[1];

// done all checking
if($error == ''){
// do db query etc here
echo $error;

Untested and will need tweaking. I've only just woke up so just wanted to knock something quick up as an example

03-06-2007, 01:53 PM
nice night, thanks
could i use 2 error checks on same line for the password field

$errormessages = array("No username entered","No password entered or password to short","etc");
if (empty($password) || $count < 6){
$error .= $errormessages[1]

03-06-2007, 05:13 PM
Yes, but you need a colon after your $error line.