Jump to content

[SOLVED] Preventing identical email and username registrations?


christofurr

Recommended Posts

Here's my first attempt:

 

<?php
$takenick = mysql_query("SELECT Nickname FROM table");
$takemail = mysql_query("SELECT Email_Address FROM table");
$usednick = mysql_fetch_array($takenick);
$usedmail = mysql_fetch_array($takemail);

if ($nicktrim == $usednick)
{$error = "1";}
if ($_POST["email"] == $usedmail)
{$error = "2";}

if ($error != "1" && $error != "2")
{
/*Execute database input*/
}
?>

 

It's for a form. I connect to the database prior to this bit of code. The connection is fine, and if I leave the or die(mysql_error()) function in, it displays the duplicate entry error. However, I don't want that to happen. I want the above code to echo an error message like "This username is already taken" if the nickname is already in the database. But, it doesn't work. What do I change?

Link to comment
Share on other sites

How should it all be done?

 

I would simply use...

 

<?php

  if ($result = mysql_query("SELECT Nickname, Email_Address FROM table WHERE NickName = '$nicktrim' && Email_Address = '{$_POST['email']}'")) {
    if (mysql_num_rows($result)) {
      echo "Username or email already taken";
    }
  }

?>

 

If you want to be more precise with your description.

 

<?php

  if ($result = mysql_query("SELECT Nickname, Email_Address FROM table")) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        if ($row['NickName'] == $nicktrim)) {
          echo "Username already taken";
        }
        if ($row['Email_Address'] == $_POST['email'])) {
          echo "Email already taken";
        }
      }
    }
  }

?>

Link to comment
Share on other sites

So... All I really needed to do is use the while() statement?

 

<?php
$takenick = mysql_query("SELECT Nickname FROM table");
$takemail = mysql_query("SELECT Email_Address FROM table");

while ($usednick = mysql_fetch_array($takenick)
{
if ($nicktrim == $usednick)
{$error = "1";}
}

while ($usedmail = mysql_fetch_array($takemail)
{
if ($_POST["email"] == $usedmail)
{$error = "2";}
}

if ($error != "1" && $error != "2")
{
/*Execute database input*/
}
?>

Link to comment
Share on other sites

declare the fileds in the db as unique or query the data then condition

 

if ($nicktrim == $usednick)

{$error1 = "1";}

}

 

while ($usedmail = mysql_fetch_array($takemail);)

{

if ($_POST["email"] == $usedmail)

{$error2 = "2";}

}

 

if ($error1 != "1" && $error2 != "2")

{

/*Execute database input*/

}

Link to comment
Share on other sites

How about this...

 

<?php
$takenick = mysql_query("SELECT Nickname FROM Froobs WHERE Nickname = $_POST['nick']");
$takemail = mysql_query("SELECT Email_Address FROM Froobs WHERE Email_Address = $_POST['email']");

if (mysql_num_rows($takenick) >= "1")
{$error = "1";}

if (mysql_num_rows($takemail) >= "1")
{$error = "2";}

if ($error != "1" && $error != "2")
{
/*Execute database input*/
}
?>

 

Yes? YES??  :D

Link to comment
Share on other sites

This last effert is your best so far, but really, executing two queries where one is sufficient is rediculous. If you learn to program properly now you wont teach yourself bad habbits.

 

PS: Integers should not be surrouned in quotes.

Link to comment
Share on other sites

There is no parse error in that line, we need a few lines prior. That line would however produce a mysql error as values need quotes surrounding them.

 

Also, if your going to embed complex variables (arrays) within a double quoted string the correct syntax is to use {}. eg;

 

$takemail = mysql_query("SELECT Email_Address FROM Froobs WHERE Email_Address = '{$_POST['email']}'");

Link to comment
Share on other sites

shouldn't it be:

 

<?php
$takenick = mysql_query("SELECT Nickname FROM Froobs WHERE Nickname = '{$_POST['nick']}'");
$takemail = mysql_query("SELECT Email_Address FROM Froobs WHERE Email_Address = '{$_POST['email']}'");

if (mysql_num_rows($takenick))
{$error = "1";}

if (mysql_num_rows($takemail))
{$error = "2";}

if ($error != "1" && $error != "2")
{
/*Execute database input*/
}
?>

Link to comment
Share on other sites

No more error on line 25, but now there's one on line 50.

Something about the mysql_num_rows() argument not being valid.

 

if (mysql_num_rows($takenick) >= 1)

 

And now I've discovered a problem with the form's input values, but we'll get to that later.

 

<?php
$takenick = mysql_query("SELECT Nickname FROM Froobs WHERE Nickname = $_POST['nick']");
$takemail = mysql_query("SELECT Email_Address FROM Froobs WHERE Email_Address = $_POST[email]");

if (mysql_num_rows($takenick) >= 1)
{$error = "1";}

if (mysql_num_rows($takemail) >= 1)
{$error = "2";}

if ($error != "1" && $error != "2")
{
/*Execute database input*/
}
?>

Link to comment
Share on other sites

Wow, I am suprised at half you guys code, and disappointed.

 

<?php
$q = mysql_query('SELECT COUNT(*) FROM Froobs WHERE Nickname=\''.$_POST['nick'].'\' OR Email_Address=\''.$_POST['email'].'\'') or die(mysql_error());
if(mysql_result($q,0,0)!=0)
die('Already Exists');
?>

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.