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?

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";
        }
      }
    }
  }

?>

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*/
}
?>

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*/

}

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

Ermm...

 

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /join.php5 on line 25

 

Line 25:

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

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']}'");

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*/
}
?>

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*/
}
?>

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');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.