Page 1 of 1

if() problem

Posted: Fri Aug 07, 2009 7:38 pm
by bobocheez
Hi,
I want to verify that the user has entered the correct password on a register page by making him enter it twice. However, if the passwords match or not, the same error comes up saying that they do not match. What am I doing wrong?

Code: Select all

if(isset ($_POST['submit'])) {
    $username = @mysql_escape_string($_POST['username']);
    $password = @$_POST['password'];
    $vpassword = @$_POST['vpassword'];
    $email = @mysql_escape_string($_POST['email']);
 
    if(@$_POST['password'] != @$_POST['vpassword']) {
        echo 'Your passwords do not match';
    }   
    elseif (!empty($username) && !empty($password) && !empty($email)) {
        $query = mysql_query("INSERT INTO members (userid,username,password,email) VALUES ('0','".$username."','".$password."','".$email."')");
        echo "You are now registered!";
    }else{
        echo 'You must enter a username, a password, and an e-mail address!';
         }
 
} else { //echo form

Re: if() problem

Posted: Fri Aug 07, 2009 8:21 pm
by aceconcepts
What's your logic? What are you trying to achieve?

I think i can see what your goal is but please clarify. Your conditions look conflicting.

I would probably write it like this (un-tested):

Code: Select all

 
if(isset ($_POST['submit'])) {
    $username = @mysql_escape_string($_POST['username']);
    $password = @$_POST['password'];
    $vpassword = @$_POST['vpassword'];
    $email = @mysql_escape_string($_POST['email']);
 
    if (!empty($username) && !empty($password) && !empty($email)) {
    $query = mysql_query("INSERT INTO members (userid,username,password,email) VALUES ('0','".$username."','".$password."','".$email."')");
        echo "You are now registered!";
    } elseif(!empty($_POST['password']) && !empty($_POST['vpassword']) && $_POST['password'] != $_POST['vpassword']) {
        echo 'Your passwords do not match';
    } else{
        echo 'You must enter a username, a password, and an e-mail address!';
    }
}
 
Also, just as a tip, mysql_escape_string is deprecated. Use mysql_real_escape_string instead.

Re: if() problem

Posted: Fri Aug 07, 2009 8:24 pm
by bobocheez
its a users registration page for a membership script

im getting an unidentified index error from vpassword

and what do you mean it looks conflicting(how so)?

Re: if() problem

Posted: Fri Aug 07, 2009 8:25 pm
by aceconcepts
Have you tried the code from my previous script?

Re: if() problem

Posted: Sat Aug 08, 2009 12:32 am
by bobocheez
yup

Re: if() problem

Posted: Sat Aug 08, 2009 12:55 am
by Cirdan
I would print_r($_POST) just to make sure it's being set