令牌未在mysql数据库中更新

时间:2018-07-14 03:27:00

标签: php mysql ajax

<?php
if (isset($_POST['email'])) {
    include 'dbconn.php';
    $email = $conn->real_escape_string($_POST['email']);

    $sql = $conn->query("SELECT id from users WHERE userEmail='$email'");

    if (!empty($sql) && $sql->num_rows > 0) {

        $token = "poiuytrewqlkjhgfdsamnbvcxz1234567890";
        $token = str_shuffle($token);
        $token = substr($token, 0, 10);

        $conn->query("UPDATE users SET token = '$token', 
                        tokenExpire = DATE_ADD(NOW(), INTERVAL 5 MINUTE) 
                        WHERE userEmail='$email'");

        exit(json_encode(array("status" => 1, "msg" => "Please check your Email inbox!")));
    }
    else{
        exit(json_encode(array("status" => 0, "msg" => "Please check your inputs!")));
    }
}?>

<div class="container">
    <div class="row">
        <div class="col-lg-6 col-md-6" align="center">
            <img src="images/logo.jpg" width="300" height="300">
            <input type="email" name="email" class="form-control" id="email" placeholder="Email">
            <input type="submit" name="" class="btn btn-primary" value="Reset password">
            <br>
            <p id="response"></p>
        </div>
    </div>
</div>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script type="text/javascript">
    var email = $('#email');

    $(document).ready(function(){
        $('.btn-primary').on('click', function(){
            if(email.val() != ''){
                email.css('border', '1px solid green');

                $.ajax({
                    url: 'forgotPassword.php',
                    method: 'POST',
                    dataType: 'json',
                    data: {
                        email: email.val()
                    }, success: function(response){
                        if (!response.success) {
                            $('#response').html(response.msg).css('color','red');
                        }else{
                            $('#response').html(response.msg).css('color','green');
                        }
                    }
                });
            }else{
                email.css('border', '1px solid red');
            }
        })
    });
</script>

tokenExpire已在我的数据库中更新,但不仅已更新令牌,而且未在数据库中创建令牌。这是通过令牌忘记了密码系统,并通过邮件将其发送到的,但是我只是在做此系统的开始。

请帮助我,因为我是初学者,很长一段时间都无法解决此问题。

1 个答案:

答案 0 :(得分:0)

检查令牌字段类型(如果其为整数或Varchar)。如果是整数,则转换为Varchar(255)。