Insert a row and get its new id

Use a prepared INSERT, then lastInsertId() to read the auto-increment id.

PHP · Insert a row and get its new idIntermediate
$st = $pdo->prepare('INSERT INTO users (name, email) VALUES (:name, :email)');
$st->execute([':name' => 'Bilal', ':email' => 'bilal@example.com']);

$newId = (int)$pdo->lastInsertId();
echo "Created user #$newId";
pdoinsertmysql