Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

Update table not working

I have written a function that will store a token and expiry date/time in a table. However when I update the table it only allows it to be updated once and does not put the correct value in for the token. For example its a password reset system that when a user puts in their email it should create a token and a timestamp and then store that in the db, so that later on I can run some checks to see if the timestamp has expired. At the moment my function look like this;

    public function sendResetPasswordEmail($email){
        $email = mysqli_real_escape_string($this->link, $email);

        $result = mysqli_query($this->link, "select email,name from user where email = '{$email}'");

        $row = mysqli_fetch_assoc($result);

        $token = bin2hex(openssl_random_pseudo_bytes(16));


        $hash = password_hash($token,PASSWORD_BCRYPT);



        $date = date('Y-m-d H:i:s');



        $res = mysqli_query($this->link,"update user set token='{$hash}' and expirytime='{$date}' where email='{$email}'");

//after this will have code  to send email via smtp

So I have run var_dump tests on the $token , $hash , $date and $res variables and they all work. However when I look into my table user in the db the token column has value of 0 and the expirytime column has the correct date/time however if I try to run it again nothing updates.

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

I managed to sort it out. The problem was with my date function as I was storing that in a timestamp value in mysql it was not needed as the timestamp created the current timestamp on its own. So my code now becomes;

    public function sendResetPasswordEmail($email){
        $email = mysqli_real_escape_string($this->link, $email);

        $result = mysqli_query($this->link, "select email,name from user where email = '{$email}'");

        $row = mysqli_fetch_assoc($result);

        $token = bin2hex(openssl_random_pseudo_bytes(16));


        $hash = password_hash($token,PASSWORD_BCRYPT)

       $res = mysqli_query($this->link,"update user set token='{$hash}'  where email='{$email}'");

//after this will have code  to send email via smtp