1

I need to catch the id of an Event after i have inserted this event. I use the function lastInsertId() but it doesn't work and I don't know why

I tried many pieces of code I found on the web and also followed videos. I tried exactly the same code I found on the web but does not work .. I'm on MySQL and PHP 7.

$query="INSERT INTO reunion (objet_reunion, heure_reunion, date_reunion, adresse_reunion, lieu_reunion, nbPlace_reunion, duree_reunion, photo_reunion, id_profil) VALUES(:objet, :heure, :date, :adresse, :lieu, :nbPlace, :duree, :photo, :id)";
$prep=$pdo->prepare($query) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
$prep->bindValue(':objet', $_POST['objet']);
$prep->bindValue(':heure', $_POST['heure']);
$prep->bindValue(':date', Date("Y-m-d", strtotime($_POST['date'])));
$prep->bindValue(':adresse', $_POST['adresse']);
$prep->bindValue(':lieu', $_POST['lieu']);
$prep->bindValue(':nbPlace', $_POST['nbPlace']);
$prep->bindValue(':duree', $_POST['duree']);
$prep->bindValue(':photo', null, PDO::PARAM_INT) ;
$prep->bindValue(':id', $_POST['id']);
$prep->execute();
$idR=$pdo->lastInsertId();
var_dump($idR);

I expect the output of the id of the last index inserted into the database for updating an index with this id in another champ but i can't find the error in my code.

jakob
  • 3,265
  • 4
  • 23
  • 40
  • 1
    If you're using PDO, `mysql_error()` won't give you anything. That said, are you certain that the execution was successful? Is `$pdo` a valid PDO object? – Qirel Jun 06 '19 at 11:37
  • Do you have an autoincrement column? – nacho Jun 06 '19 at 12:03
  • Yes the execution of insert request is successful because the event is insert in the database, i don"t try to use the mysql_error(). I try it and give you answer soon and yes the column of id_reunion is in autoincrement – antoine foulon Jun 06 '19 at 12:39

0 Answers0