I am having trouble to let foreign key updating itself, the constraint was set as on update cascade on delete cascade in phpmyadmin. I am able to insert everything else, just the foreign key user_id appearing as null rather than updating to the correct id. Where was my mistake? id the the table id, I know I don't need to put into the sql statement; user_id is the foreign key linked with tbl_user
Insert to database codes
try
{
$dbh = new PDO("mysql:host=localhost;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt =$dbh->prepare( "INSERT INTO tbl_details (user_id, name,address,postcode)
VALUES(:user_id,:name,:address,:postcode)");
$stmt->bindParam(':user_id',$user_id);
$stmt->bindparam(':name', $name);
........
$stmt->execute();
$dbh = null;
}catch (PDOException $e) {
$dbh = null;
print "Error!: " . $e->getMessage() . "<br/>";
print "PHP Line Number: " . $e->getLine() . "<br/>";
print "PHP File: " . $e->getFile() . "<br/>";
die();
}
Yes, its working now, I am missing this
$stmt = $auth_user->runQuery("SELECT * FROM tbl_details WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
Thank you @sean and @Darwin von Corax