Caught exception: 注册失败 Array ( [userId] => 0 [username] => admin1 [createAt] => 1502333216 ),老师我这个问题什么情况,我的userId设置的是自增的
我的代码
user.php
public function register($username,$password)
{
if (empty($username)) {
throw new Exception('用户名不能为空', ErrorCode::USERNAME_CANNOT_EMPTY);
}
if (empty($password)) {
throw new Exception('密码不能为空', ErrorCode::PASSWORD_CANNOT_EMPTY);
}
if ($this->_isUsernameExists($username)) {
throw new Exception('用户名已存在', ErrorCode::USERNAME_EXISTS);
}
//写入数据库
$sql = "INSERT INTO 'user'('username','password','createdAt') VALUES (:username,:password,:createdAt)";
$createdAt = time();
$password = $this->_md5($password);
$stmt = $this->_db->prepare($sql);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':createdAt', $createdAt);
// print_r($stmt);exit();
if (!$stmt->execute()) {
try{
throw new Exception('注册失败', ErrorCode::REGISTER_FAIL);
}catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(),'<br>';
}
}
return[
'userId' => $this->_db->lastInsertId(),
'username' => $username,
'createAt' => $createdAt
];
}
index.php
require_once __DIR__.'/lib/User.php';
$pdo = require __DIR__.'/lib/db.php';
$user=new User($pdo);
//print_r( $pdo -> errorInfo(), true);
print_r($user->register('admin1','admin1s'));
...