Hello please help me my last inserted index return 0.
public function insert($client) {
$sql = "insert into client (nom,adresse,tel) values (:nom,:adresse,:tel)";
$stmt = $this->connect()->prepare($sql);
$nom = $client->getNom();$adresse = $client->getAdresse();$tel = $client->getTel();
$stmt->bindParam(':nom', $nom, PDO::PARAM_STR);
$stmt->bindParam(':adresse', $adresse, PDO::PARAM_STR);
$stmt->bindParam(':tel', $tel, PDO::PARAM_STR);
$query = $stmt->execute();
$lastId = $this->connect()->lastInsertId($sql);
if ($query) {
$client->setId($lastId);
$this->liste[$lastId] = $client;
$_SESSION['listeClient'] = $this->liste;
return TRUE;
}
}
my dbconnection
protected function connect() {
$this->servername = "localhost";
$this->username = "root";
$this->password = "";
$this->dbname = "pdo";
$this->charset = "utf8mb4";
try {
$dsn = "mysql:host=" . $this->servername . ";dbname=" . $this->dbname . ";charset=" . $this->charset;
$pdo = new PDO($dsn, $this->username, $this->password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch (\Exception $e) {
echo "connection failed: " . $e->getMessage();
}
}
Thanks for your help. I try all sugestent in précédent questions the same probleme persiste