Jump to content


Photo

duplicate sql entries


  • Please log in to reply
3 replies to this topic

#1 wiggly

wiggly
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 22 April 2006 - 10:29 PM

Hi i am a complete newbe to php and thus i dont know very much however i have started writing a registration/login system for my website and i can not seem to figure out how to stop duplicate usernames or email addresses i have managed to get the actualy registration part working and i just need a bit of advice on how i could stop multiple people using the same details. i did find a tutorial that i tried to use some code from on phpfreaks and i had no luck.
this is the code i tried:

$sql_email_check = mysql_query("SELECT email form users
WHERE email_address='$email");
$sql_username_check = mysql_query("SELECT username from users
WHERE username='$username'");

$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);

if(($mail_check > 0) || ($username_check > 0)){
echo "Please fix the following errors: <br />";
if($email_check > 0){
echo "<strong>Your Email Address has already been used by another member in our database please submit a diffrent Email Address!</strong> <br />";
unset($email);
}
if($username_check > 0){
echo "<strong>The Username you selected has alreadey been used by another member in our database. Please choose a diffrent Username</strong> <br />";
unset($username);
}

i would be most greatfull if someone could help me as i have been bugged for the past week with this problem, maybee i have just typed somthing wrong or missed somthing out i have no idea.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 22 April 2006 - 11:43 PM

You don't say what the problem is. Just posting code and saying "It doesn't work" does not give anyone much of clue about what to look for.

I did notice in this line below you have used $mail_check instead of "$email_check"
if(($mail_check > 0) || ($username_check > 0)){

Also there are a lot of old tutes out there which assume register_globals is set ON whereas with later version of php it is set OFF by default. This means you may have explicitly get the the username and email address from the $_GET or $_POST arrays at the beginning of your script

$username = $_POST['username'];
$email = $_POST['email'];

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 wiggly

wiggly
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 April 2006 - 04:39 PM

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Server\Apache2\htdocs\login2\register.php on line 45

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Server\Apache2\htdocs\login2\register.php on line 46

these are the errors i get when i use this piece of code however when i take the code out i get no errors what i want to do is add code to my register.php to stop people registering with a username and email address that is already registered.

here is my code with the above problems removed maybee that will be more helpfull to you

<?

include('include/db_connect.php');

$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$info = $_POST['info'];

$first = stripslashes($first);
$last = stripslashes($last);
$email = stripslashes($email);
$phone = stripslashes($phone);
$user = stripslashes($user);
$pass = stripslashes($pass);
$info = stripslashes($info);

if((!$first) || (!$last) || (!$email) || (!$user) || (!$pass)){
    echo '<strong>You must fill in all required fields!!!</strong> <br />';
    if(!$first){
        echo "You must fill in the First Name field! <br />";
    }
    if(!$last){
        echo "You must fill in the Last Name field! <br />";
    }
    if(!$email){
        echo "You must fill in the E-Mail field! <br />";
    }
    if(!$user){
        echo "You must fill in the Username field! <br />";
    }
    if(!$pass){
        echo "You must fill in the Password field! <br />";
    }
    include 'register.html';
    exit();
}
$pass=md5($pass);

$sql = mysql_query("INSERT INTO users (first, last, email, phone, user, pass, info) VALUES('$first', '$last', '$email', '$phone', '$user', '$pass', '$info')")
    or die (mysql_error());
    
if(!$sql){
    echo 'There has been a problem during the registration process please try again later! <br />';
}
else{
$userid = mysql_insert_id();
    echo 'Your account was created sucsesfully <br />';
}
?>


#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 23 April 2006 - 06:27 PM

You get those messages because of errors in the sql queries.

There is certainly an error in the first, FROM is mis-spelt as FORM

$sql_email_check = mysql_query("SELECT email form users
WHERE email_address='$email");
$sql_username_check = mysql_query("SELECT username from users
WHERE username='$username'");

Use this code to get the error messages

$sql_email_check = mysql_query("SELECT email from users
WHERE email_address='$email") or die (mysql_error() . ' in query ' . $sql_email_check);
$sql_username_check = mysql_query("SELECT username from users
WHERE username='$username'") or die (mysql_error() . ' in query ' . $sql_username_check);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users