if() problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bobocheez
Forum Commoner
Posts: 25
Joined: Mon Oct 27, 2008 8:06 pm

if() problem

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: if() problem

Post 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.
bobocheez
Forum Commoner
Posts: 25
Joined: Mon Oct 27, 2008 8:06 pm

Re: if() problem

Post 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)?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: if() problem

Post by aceconcepts »

Have you tried the code from my previous script?
bobocheez
Forum Commoner
Posts: 25
Joined: Mon Oct 27, 2008 8:06 pm

Re: if() problem

Post by bobocheez »

yup
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: if() problem

Post by Cirdan »

I would print_r($_POST) just to make sure it's being set
Post Reply