If-else condition not working...

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
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

If-else condition not working...

Post by aditya2071990 »

Hello all...

I have a little piece of code here that has a few nested if-else conditions... it's working fine on all the conditions, but the condition which checks for previously registered users is not working... can you please point out the error in this? Here's my code...

Code: Select all

 
    $regUserId=$_POST['regUserId'];
    $register=$_POST['regSubmit'];
    //check if register button is clicked
    if(isset($register)){
        $regPWord=$_POST['regPWord'];
        $regPWordConfirm=$_POST['regPWordConfirm'];
        $name=$_POST['regName'];
        $phone=$_POST['regPhone'];
        $email=$_POST['regEmail'];
        //check if any of the fields are empty
        if(empty($name) || empty($phone) || empty($email) || empty($regPWord) || empty($regPWordConfirm)){
            echo 'Please don\'t leave any fields empty.<br /><br />';
        }else{
            //check if the passwords don't match
            if($regPWord!=$regPWordConfirm){
                echo 'The Passwords don\'t match.<br /><br />';
            }else{
                //check if the user name exists in the database already
                $checkForTwins=@mysql_query("select id from users where userId='".$regUserId."'");
                //if the check isn't empty, the name was taken before
                if(!empty($checkForTwins)){
                    echo 'Sorry. That username is already in use. Please try a different name.';
                }else{
                        //start a session
                        session_start();
                        //connect to mysql database server
                        $connect=@mysql_connect('localhost','root');
                        //check if there's an error in connecting
                        if(!$connect){
                            echo 'We are sorry, but there is an error with the server. Please try again later.<br /><br />';
                            echo mysql_error();
                            exit();
                        }
                        //use the required database
                        $selectDb=@mysql_query('use talentvisions');
                        //check if there's an error in using the db
                        if(!$selectDb){
                            echo 'We are sorry, but there\'s an error in using the required database. Please try again later.<br /><br />';
                            echo mysql_error();
                            exit();
                        }
                        //generate unique id string
                        $uniqId=str_replace(".","",uniqid(microtime(true),true).$_SERVER['REMOTE_ADDR']);
                        //if user id don't exist, enter the given data into the database
                        $regPWordMd5=md5($regPWord);
                        $enterData=@mysql_query("insert into users (userId,pWord,name,phone,email,uniqId) values ('$regUserId', '$regPWordMd5', '$name', '$phone', '$email', '$uniqId')");
                        //check if there's an error with entering the data
                        if(!$enterData){
                            echo 'Sorry, but there was an error while entering the data into the database, please try again later.';
                        }
                        //store the uniqid for a session
                        $_SESSION['uniqId']=$uniqId;
                        //send user to payment type
                        header('location:choosePayType.php');
                }
            }
        }
    }
 
My problems is apparently from line no. 19...
Woolf
Forum Newbie
Posts: 1
Joined: Mon Dec 22, 2008 12:01 pm

Re: If-else condition not working...

Post by Woolf »

Change:

Code: Select all

 
                //check if the user name exists in the database already
                $checkForTwins=@mysql_query("select id from users where userId='".$regUserId."'");
                //if the check isn't empty, the name was taken before
                if(!empty($checkForTwins)){
                    echo 'Sorry. That username is already in use. Please try a different name.';
                }else{
                        //start a session
 
To:

Code: Select all

 
                //check if the user name exists in the database already
                $checkForTwins=@mysql_query("select id from users where userId='".$regUserId."'");
                //if the check isn't empty, the name was taken before
                if(mysql_num_rows($checkForTwins)){
                    echo 'Sorry. That username is already in use. Please try a different name.';
                }else{
                        //start a session
 
Adding mysql_num_rows() returns how many results were found (e.g. 0 or 5 users). If there is more than 0 results, it will display the username in use message.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: If-else condition not working...

Post by aditya2071990 »

Thanks for the reply, but the solution did not work.

Following your logic, I also tried making the condition

Code: Select all

if(mysql_num_rows($checkForTwins)!=0)
, but even that did not work...
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: If-else condition not working...

Post by aditya2071990 »

Okay I figured it out myself...

I was trying to use the database before I initialized the connection to the database, that's why it was giving an access denied message, and as I was suppressing all mysql errors with @, I did not know...

Thanks for trying to help though :)
Post Reply