I have a table called player and after inserting the data into there, which successfully works, I have the following piece of code to capture the value of the auto incremented primary key.
$one = $pdo->lastInsertId();
What I then wish to do is to take the value that is stored in this variable and insert it as a value in another table. I have tried a couple of ways but to no avail. Please see below:
ATTEMPT ONE
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();
}
ATTEMPT TWO
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();
}
The code doesn’t actually throw any errors but when I look in the database, the value is not getting passed in.
Can anyone explain what is going wrong please?
Thanks for your time and help.
just use
$one