PDO的lastInsertId()在$ _SESSION ['xxxxxx'] =?返回一行中的正确ID而不是我需要它的另一行

Im working on a login script but am having trouble with lastInsertId();

Here's the code;

 <?php
    session_start();

    db = new PDO("mysql:host=hostname;dbname=database name", user, password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    try { 
        $db->beginTransaction();
        $db->query("INSERT INTO `subscribers`(`subscribers`.`first_name`, `subscribers`.`last_name`) VALUES ('Jane', 'Doe')");
        $db->query("UPDATE `subscribers` SET `first_name`='John' WHERE `ID` =".$db->lastInsertId());
        $db->commit();
        $LastID = $db->lastInsertId();
        $_SESSION['user_id'] = $LastID; 

    } catch (PDOException $e) {
        $db->rollBack();
        die($e->getMessage());
        }
?>

My problem is that the UPDATE query performs properly (based off of WHERE ID =".$db->lastInsertId());) but when I try to use the function for setting the $_SESSION as seen in the code all I get back is 0.

If anyone wouldn't mind showing me what's askew I'd be most appreciative and I thank you in advance.

Why not just:

$db->beginTransaction();
$db->query("INSERT INTO `subscribers`(`subscribers`.`first_name`, `subscribers`.`last_name`) VALUES ('Jane', 'Doe')");
// Grab, save
$LastID = $db->lastInsertId();
// Query by last ID
$db->query("UPDATE `subscribers` SET `first_name`='John' WHERE `ID` =".$LastID);
$db->commit();
// Write last ID to session
$_SESSION['user_id'] = $LastID;

My guess is that the update destroys the lastInsertId of your insert. Try storing the id in a variable for use later:

    $db->query("INSERT INTO `subscribers`(`subscribers`.`first_name`, `subscribers`.`last_name`) VALUES ('Jane', 'Doe')");
    $last_id=$db->lastInsertId();
    $db->query("UPDATE `subscribers` SET `first_name`='John' WHERE `ID` =".$last_id);
    $db->commit();
    $_SESSION['user_id'] = $last_id