php - MySQL Foreign Key Constraint Fail -
so, clarify things: know why failing.
question is: how work around that?
setup:
referencing table users , referencing column id, referenced table user_data, , referenced column id. want store email, username, salt , password in 1 table, user data login tokens , such in another. thought right way it. apparently missing something.
and oh i'm using php pdo.
function registeruser($email, $username, $password) { global $db; try { $prep = $db->prepare("insert users (email, username, salt, password) values(':email', ':username', ':salt', ':password')"); $salt = "abcd"; $prep->bindparam(':email', $email); $prep->bindparam(':username', $username); $prep->bindparam(':salt', $salt); $prep->bindparam(':password', $password); $prep->execute(); echo "success"; } catch (pdoexception $e) { die("query failed: " . $e->getmessage()); } }
edit: forgot error... query failed: sqlstate[23000]: integrity constraint violation: 1452 cannot add or update child row: foreign key constraint fails (
infokup2016.
users, constraint
dataforeign key (
id) references
user_data(
id) on delete no action on update no action)
you setting foreign key on wrong table.
you have set foreign key on field id
of table user_data
referencing filed id
on table users
now trying insert data in table users
, but, id
should present in table user_data
(since have foreign key in table user
referencing table user_data
) gives error
Comments
Post a Comment