Jump to content
Larry Ullman's Book Forums

Forgot_Password.Php File Issues


Recommended Posts

Dear Larry,

 

I wish to ask on the Chap 4 of the Effortless E-Commerce.

I dn't undestand the variable error being undefined on $q= UPDATE statement on updating random password that must be sent to user who want to reset his forgotten password:

The statement is on updating password where id = $uid LIMIT 1";

I wish to know the meaning of the value $uid LIMIT 1 that is being reported as undefined variable (undefined variable: uid).

 

How to fix it in other way.

 

I am sure you've understand my idea and the line giving problem.

Link to comment
Share on other sites

The "LIMIT 1" part is merely there to ensure that only one row in the DB is updated (in the event that something is weird about the DB and there is more than one user with a given user ID).

 

As for your error, that's happening because the $uid variable is not defined in your code.

Without seeing more of your code though, we unfortunately cannot help.

  • Upvote 1
Link to comment
Share on other sites

Please see the code below as per the book on the forgot_password.php file

 

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        // Make an email filter
        if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
            //Check the db a to find the email, if available send password
            $q = 'SELECT lngCus_Id
                  FROM tblcustomer
                  WHERE strCus_Email = "'. mysqli_real_escape_string($dbc, $_POST['email']). '"';
                  
            // Return rows
            $r = mysqli_query($dbc, $q);
            
            // If row is find with customer ID
            if (mysqli_num_rows($r) == 1){
                                
                list($uid) = mysqli_fetch_array($r, MYSQLI_NUM);
                
            }
            else {
                $pass_errors['email'] = 'Your email is not valid!';
            }
        }
        else { //No valid email address
            $pass_errors['email'] = 'Please enter a valid email address!';
        }
    }
    
    // If no error
    if (empty($pass_errors)) {
        //Take a randon password and substract from 11 characheter for 15 characters from 32 characters given
        $p = substr(md5(uniqid(rand(), true)), 10, 15) ;
        
        
        //Add the new password to the db
        $q = "UPDATE tblcustomer
              SET strCus_Password = '".get_password_hash($p)."'
              WHERE lngCus_Id = $uid LIMIT 1";
        
    }
}

 

Please explain me:     list($uid) = mysqli_fetch_array($r, MYSQLI_NUM);

 

The error is on the line of:    WHERE lngCus_Id = $uid LIMIT 1";

 

Hope to hearing from you soon!

Costa

Link to comment
Share on other sites

The list "function" (it's not really a function, but kind of) allows you to assign array values to multiple variables at once.

Similar to an example on the PHP.net site (http://php.net/manual/en/function.list.php), here's a simple example of how to use list:

$pets = array('dog', 'cat');

list($pet1, $pet2) = $pets;

By executing the above code, $pet1 would be equal to 'dog', and $pet2 would be equal to 'cat'.

 

Similarly, mysqli_fetch_array returns an array of values that corresponds to one row returned from the DB. Since you are only grabbing the lngCus_Id column from the DB, you only need to assign one value to a variable in your list function.

 

In other words, $uid *should* be equal to the lngCus_Id value returned from the DB, but based on your error, $uid is never being set, which would imply that lngCus_Id is not being returned from the DB.

More than likely, your query isn't returning any results, I'd look into that.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...