为了账号安全,请及时绑定邮箱和手机立即绑定

register 返回的是正常的时间,但是login能够正常返回username和id,时间是0000-00-00

http://img1.sycdn.imooc.com//594e19a000016fe007140113.jpg


register 返回的是正常的时间,但是login能够正常返回username和id,时间是0000-00-00

正在回答

3 回答

created_at 字段是 DATETIME 类型,似乎需要存储日期时间格式的字符串。

我进行了以下修改:

public function register($username, $password)

{

    ......

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

    ......

}

1 回复 有任何疑惑可以回复我~
#1

枫叶咚咚咚

我要是报错的时候看见这个答案就好了,自己排错排了半天。。orz
2019-01-20 回复 有任何疑惑可以回复我~

<?php
/**
* Created by PhpStorm.
* User: lee
* Date: 6/18/2017
* Time: 3:18 PM
*/

require __DIR__.'/lib/User.php';
$pdo=require __DIR__.'/lib/db.php';
$user=new User($pdo);
//

print_r($user->register('admin5','1111'));
echo "<br>";
print_r($user->login('admin5','1111'));
//echo time();



0 回复 有任何疑惑可以回复我~
<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 6/18/2017
 * Time: 3:20 PM
 */
require __DIR__.'/ErrorCode.php';
class User
{
    /**
     * 数据库链接句柄
     * @var
     */

    private $_db;

    /**
     * 构造方法
     * User constructor.
     * @param $_db
     */


    public function __construct($_db)
    {
        $this->_db=$_db;
    }

    /**
     * 用户登陆
     * @param $username
     * @param $password
     */
    public function login($username,$password)
    {
        $sql='select * from `user` where `username`=:username and `password`=:password';
        $password=$this->_md5($password);
        $stmt=$this->_db->prepare($sql);
        $stmt->bindParam(':username',$username);
        $stmt->bindParam(':password',$password);
        $stmt->execute();
        $user=$stmt->fetch(PDO::FETCH_ASSOC);
        if(empty($user)){
            throw new Exception('USERNAME_OR_PASSWORD_INVALID',ErrorCode::USERNAME_OR_PASSWORD_INVALID);
        }
        unset($user['password']);
        return $user;
    }

    /**
     * 用户注册
     * @param $username
     * @param $password
     */
    public function register($username,$password)
    {
        if(empty($username)){
            throw new Exception('USERNAME_CANNOT_EMPTY',ErrorCode::USERNAME_CANNOT_EMPTY);
        }


        if(empty($password)){
            throw new Exception('PASSWORD_CANNOT_EMPTY',ErrorCode::PASSWORD_CANNOT_EMPTY);
        }

        if($this->_isUsernameExists($username)){
            throw new Exception('USERNAME_EXISTS',ErrorCode::USERNAME_EXISTS);
        }

        //写入数据库

        $sql='INSERT INTO `user` (`username`,`password`,`createdAt`) values (:username,:password,:createdAt)';
        $createdAt=time();//插入mysql变成00000  print_r($stmt);

        $password=$this->_md5($password);
        $stmt=$this->_db->prepare($sql);
        $stmt->bindParam(':username',$username);
        $stmt->bindParam(':password',$password);
        $stmt->bindParam(':createdAt',$createdAt);
        //echo $createdAt;
        //print_r($stmt);

        if(!$stmt->execute()){
            throw new Exception('fail',ErrorCode::REGISTER_FAIL);
        }

        return [
            'userId'=>$this->_db->lastInsertId(),
            'username'=>$username,
            'createdAt'=>$createdAt
        ];


    }

    private function _md5($string,$key='imooc')
    {
        return md5($string.$key);
    }

    private function _isUsernameExists($username)
    {
        $sql='SELECT * from `user` where `username`=:username';
        $stmt=$this->_db->prepare($sql);
        $stmt->bindParam(':username',$username);
        $stmt->execute();
        $result=$stmt->fetch(PDO::FETCH_ASSOC);
        return !empty($result);
    }
}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

register 返回的是正常的时间,但是login能够正常返回username和id,时间是0000-00-00

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信