使用lastInsertId()值填充另一个表中的列


Using a lastInsertId() value to populate a column in another table

我有一个名为player的表,在成功地将数据插入其中后,我有以下代码来捕获自动递增主键的值。

$one = $pdo->lastInsertId();

然后,我希望做的是获取存储在该变量中的值,并将其作为值插入另一个表中。我试过几种方法,但都没有用。请参阅以下内容:

尝试一次

try
{
    $sql = "INSERT INTO links SET
            link = :link,
            playerid = '$one'";
    $s = $pdo->prepare($sql);
    $s->bindValue(':link', $_POST['link']);
    $s->bindValue(':playerid', $_POST[':playerid']);
    $s->execute();
}
catch (PDOException $e)
{
    $error = 'Error adding link for player.' . $e->getMessage();
    include 'error.html.php';
    exit();
}

尝试两次

try
{
    $sql = 'INSERT INTO links SET
            link = :link,
            playerid = :playerid';
    $s = $pdo->prepare($sql);
    $s->bindValue(':link', $_POST['link']);
    $s->bindValue(':playerid', $_POST['$one']);
    $s->execute();
}
catch (PDOException $e)
{
    $error = 'Error adding link for player.' . $e->getMessage();
    include 'error.html.php';
    exit();
}

代码实际上并没有抛出任何错误,但当我查看数据库时,值并没有被传入

有人能解释一下出了什么问题吗?

谢谢你的时间和帮助。

只需使用$one

$sql = 'INSERT INTO links SET
        link = :link,
        playerid = :playerid';
$s = $pdo->prepare($sql);
$s->bindValue(':link', $_POST['link']);
$s->bindValue(':playerid', $one);