Jump to content

Subsequent If not working


oOmega

Recommended Posts

Hello !

 

First of all, I'm a newbie in php and english isn't my native language.

 

My website :

-index.php where the user create his account

-signup.php where every test are made, then if succesfull will create the user in the database.

The code bellow will be from signup.php. Variables in $_post comes from index.php.
 

DB connect

<? php

$bdd = new PDO('mysql:host=localhost;dbname=lolstats', 'martin', '');
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 

 

Tests :

    // Testing if every variables are existing

    If (Isset($_POST['sign_password']) AND ($_POST['sign_pseudo']) AND ($_POST['sign_email']) AND ($_POST['sign_password2']) )
    {
            $email= $_POST['sign_email'] ;
            $user= $_POST['sign_pseudo'] ;
            $test = $bdd->prepare('SELECT pseudo FROM membres WHERE pseudo= ? OR email = ?  ') ;
            $test -> execute(array ( $user, $email) );
        while ($test_test=$test->fetch()) {

                // testing if the user is already in the database
                if ( $test_test === NULL ) {

                    // testing if the password match
                    if ( ($_POST['sign_password']) == ($_POST['sign_password2']) ){
                        // Crypting
                        $pass_hache = sha1($_POST['sign_password']);
                        // Insert
                        $test->closeCursor();
                        $req = $bdd->prepare('INSERT INTO membres (pseudo, pass, email, date_inscription) VALUES(?, ?, ?, sysdate())');
                        $req->execute(array($_POST['sign_pseudo'], $pass_hache, $_POST['sign_email']));
                        echo '<p> Account Created ! </p>' ;
                    }
                    ELSE {
                            echo '<p>Non matching passwords</p>' ;
                            
                    }
                }
                ELSE {
                        echo '<p>User / Email already in used' ;
                }
        }
    }
    ELSE {
    echo '<p>Please, enter every information needed</p>';
    }
    
        
?>

 

 

 

The line in red is where I think the problem is. So, let's say $test_test <> NULL, then the else function just fine.            

ELSE {
                        echo '<p>User / Email already in used' ;
                }

 

But it seems like I never goes "inside" the $test_test == NULL. I'm saying that because the next if is working fine if I test it alone, but once put inside the if == NULL, it doesn't work anymore.

 

I hope I made myself clear, please ask if there are some logic you don't understand in my code.

 

Thanks.

Link to comment
Share on other sites

So it always says that user/email in use message? That must mean

if ( $test_test === NULL ) {
was never true. Well, what is the value of $test_test? It comes from

while ($test_test=$test->fetch()) {
and $test came from

$test = $bdd->prepare('SELECT pseudo FROM membres WHERE pseudo= ? OR email = ?  ') ;
That means $test is a PDOStatement object and $test->fetch is PDOStatement::fetch.

 

If you look at the documentation you'll see that is says (in English),

The return value of this function on success depends on the fetch type. In all cases, FALSE is returned on failure.

It never returns null. That's why $test_test === NULL is never true.
Link to comment
Share on other sites

Tried with FALSE, doesn't work either.

Tried this

 

        while ($test_test=$test->fetch()) {
        print_r($test_test);
        }

When the user already exist, it contains his pseudo / email

When the user doesn't exist, I get a blank page with noothing written. Which means it contains NULL no ? Or it would show me NULL ?

Link to comment
Share on other sites

So it always says that user/email in use message? That must mean

if ( $test_test === NULL ) {

was never true.

 

No, when the user / email is not already in use, I get a blank page.

It looks like it's kind of working, because if it's in use, it's working perfectly. If user/email not in use, then nothing happens.

It's like I don't enter the next if. But when I try the next IF alone, it works juste fine. (the password = password2 one).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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