...

View Full Version : register script



rafiki
03-05-2007, 10:59 PM
<?php
$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
die(mysql_error());
mysql_select_db($dataname) or die(mysql_error());
$check = mysql_query("SELECT username FROM users WHERE
username='$username'");
$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.";
unset($user);
exit;
}
$query = "INSERT INTO users (name, sname, email, username,
password, )
VALUES('$name', '$sname', '$email', '$username',
'$password')";
mysql_query($query) or die(mysql_error());
mysql_close();
echo "You have successfully registered please click <a href
=\"/login.php\" >here</a> to log in";
?>
<?php
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

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

$submit = $_GET['submit'];
try

$submit = $_POST['submit']

Nightfire
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))

Inigoesdr
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.

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

Nightfire
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 = '';
if(empty($username)){
$error .= $errormessages[0];
}
if(empty($password)){
$error .= $errormessages[1];
}

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

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

rafiki
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]
}

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum