$this-> db-> lastInsertId()
Bonjour, comme le titre l'indique, j'ai un souci quand j'essaye d'utiliser lastInsertId :
Citation:
Fatal error: Uncaught Error: Call to undefined method Database::lastInsertId() in \class\DatabaseAuth.php on line 19
Error: Call to undefined method Database::lastInsertId() in \class\DatabaseAuth.php on line 19
Je fais le traitement d'une inscription sur un site. Voici la classe qui pose souci : DatabaseAuth.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?php
class DatabaseAuth {
private $db;
public function __construct($db) {
$this->db = $db;
}
public function register($username, $password, $email) {
$pass = password_hash($password, PASSWORD_BCRYPT);
$token = Str::random(60);
$this->db->query("INSERT INTO users SET pseudo = ?, password = ?, email = ?, registration_date = now(), confirmation_token = ?", [
$username,
$password,
$email,
$token
]);
$user_id = $this->db->lastInsertId();
mail($email, 'Confirmation de compte', "Cliquez ici :\n\n http://localhost/kayo/confirm.php?id=$user_id&token=$token");
}
} |
Voici là ou je fais le traitement du formulaire : register.php
Code:
1 2 3 4 5 6
| $db = App::getDatabase();
...
$auth = new DatabaseAuth($db);
$auth->register($_POST['username'], $_POST['password'], $_POST['email']); |
Et voici ma classe App si ça peut aider :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?php
class App {
static $db = null;
static function getDatabase() {
if (!self::$db) {
self::$db = new Database('this', 'is', 'mine');
}
return self::$db;
}
static function redirect($location) {
header("Location: $location");
exit();
}
} |
J'ai l'impression que l'erreur est toute simple et pourtant j'arrive pas à mettre le doigt dessus :/ Merci par avance pour votre aide.