求幫助老師
Caught exception: 注冊(cè)失敗 Array ( [userId] => 0 [username] => admin1 [createAt] => 1502333216 ),老師我這個(gè)問(wèn)題什么情況,我的userId設(shè)置的是自增的
我的代碼
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);
? ? ? ?}
? ? ? ?//寫入數(shù)據(jù)庫(kù)
? ? ? ?$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('注冊(cè)失敗', 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'));
2017-12-03
...