php - MySQL Foreign Key Constraint Fail -


so, clarify things: know why failing.
question is: how work around that?
setup:
enter image description here
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, constraintdataforeign key (id) referencesuser_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

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -