PDOを使用してデータベースにデータを挿入するための次のコードがあります。
データベースにデータを挿入しますが、最後に挿入された ID は返しません。
ここuseridに主キーがあります
  try {
        $dbh = new PDO('mysql:host=localhost;dbname=crud_demo', "username", "password");
        $sqlQuery = "INSERT INTO users(userid,first_name,last_name,email,password)
            VALUES(:userid,:first_name,:last_name,:email,:password)";
        $statement = $dbh->prepare($sqlQuery);
        $bind = array(
            ":userid" => "bhavik",
            ":first_name" => "Bhavik",
            ":last_name" => "Patel",
            ":email" => "bhavitk@live.in",
            ":password" => "1234567"
        );
        $statement->execute($bind);
        echo $dbh->lastInsertId();
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
$dbh->lastInsertId();ユーザーIDの値を挿入しても、常に0を返します