Jump to content

[SOLVED] signup form -- check both username and email fields for duplicity in DB


johnwayne77

Recommended Posts

Hi!

 

I'm trying to make a signup system for a project of mine.

 

All good till now, I can register users but I want to check the input fields : USERNAME & EMAIL for duplicity in the database, so people can't register withe same username or email twice.

 

This is my code related to this:

 

$_utilizator = str_replace("<", "", $_POST['utilizator']);
$_parola = str_replace("<", "", $_POST['parola']);
$_email = str_replace("<", "", $_POST['email']);

mysql_connect($host, $user_name, $password)
   or die('Could not connect: ' . mysql_error());

mysql_select_db($database_name) or die('Could not select database');
$result = mysql_query("SELECT * FROM users WHERE email='$_email'") or die(mysql_error());
$row = mysql_fetch_array( $result );

if(($row['email'] == '') || ($row['utilizator'] == ''))

{


mysql_query ("INSERT INTO users (nume, an, grupa, localitate, cnp, utilizator, parola, email, admin) VALUES ('$_nume','$_an','$_grupa','$_localitate','$_cnp','$_utilizator','$_parola', '$_email', '0')");
echo "<br><b>cont creat</b><br>";

}

 

So, now it checks only the duplicity of email.

If i change this line:

 

$result = mysql_query("SELECT * FROM users WHERE email='$_email'") or die(mysql_error());

 

from email='$_email' to utilizator='$_utilizator' , it will check for the duplicity of the username but will ignore the email field.

 

I've tried puttin in the IF statement the following:

 

if(($row['email'] == '') || ($row['utilizator'] == ''))

if($row['email'] == '' || $row['utilizator'] == '')

if($row['email'] == '' && $row['utilizator'] == '')

 

..but with no luck.

 

Any ideas ?

Link to comment
Share on other sites

yeah, change the query to:

 

$result=("SELECT * FROM users WHERE email='$_email' OR utilizator='$_utilizator'") or die(mysql_error());

 

That's assuming the column in mysql is called "utilizator" aswell. Leave the rest as it was and it should work. It wasn't working before because you were only asking if email addresses were the same, but not usernames, and then you had your if statement querying both even though you hadn't looked in the database for both.

 

Personally, a i think word like utilizator is really unneccessary; you're just gonna make extra problems for yourself with spelling errors leading to mysql errors etc., keep it simple!

Link to comment
Share on other sites

i've changed the line with:

 

$result = mysql_query("SELECT * FROM users WHERE email='$_email' OR utilizator='$_utilizator'") or die(mysql_error());

 

and the if is:

 

if(($row['email'] == '') || ($row['utilizator'] == ''))

 

it checks only the email duplicity... as for username, i can register 10 usernames 'michael' for example

 

if i put utilizator='$_utilizator' before email='$_email' in the select command above, it checks only the utilizator (that is the first condition)...

 

can't figure out what's the matter with it

Link to comment
Share on other sites

separate checking

# for duplicate name
$result = mysql_query("SELECT nume FROM users WHERE nume = '$_nume' AND email='$_email'") or die(mysql_error());
$row = mysql_num_rows($result);

if($row > 0):
header('location: previewspage.php');
endif;

# for duplicate email
$result = mysql_query("SELECT email FROM users WHERE nume = '$_nume' AND email='$_email'") or die(mysql_error());
$row = mysql_num_rows($result);

if($row > 0):
header('location: previewspage.php');
endif;

 

 

single checking

 

 

# for duplicate name & email
$result = mysql_query("SELECT nume, email FROM users WHERE nume = '$_nume' AND email='$_email'") or die(mysql_error());
$row = mysql_num_rows($result);

if($row > 0):
header('location: previewspage.php');
endif;

 

Link to comment
Share on other sites

Ah! I was being stupid, that's a really bad way of doing it. As all you want to know is that there aren't any duplicates, you just need to count the number of rows with matching emails or usernames, so you should change it all to...

 

$result=mysql_numrows(mysql_query("SELECT * FROM users WHERE email='$_email' OR utilizator='$_utilizator'")) or die(mysql_error());

if ($result > 0) {
echo("Someone is already registered with that username or email address!");
} else {
mysql_query ("INSERT INTO users (nume, an, grupa, localitate, cnp, utilizator, parola, email, admin) VALUES ('$_nume','$_an','$_grupa','$_localitate','$_cnp','$_utilizator','$_parola', '$_email', '0')");
echo "<br><b>cont creat</b><br>";
}

 

That should do the trick!

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.