简体   繁体   English

尝试自动将外键值插入我的mysql表

[英]Trying to automatically insert a foreign key value into my mysql table

I am new with PHP and MySQL and need help... 我是PHP和MySQL的新手,需要帮助...

Im cant seem to get my head around how to automatically insert a foreign key into a table in my database. 我似乎无法理解如何自动将外键插入数据库的表中。 The primary key gets added as 'id' in my 'mem' table but I want this same id to be added to a foreign key in my 'location' table. 主键在我的“内存”表中添加为“ id”,但我希望将此相同的ID添加到“位置”表的外键中。 Can anyone please help? 谁能帮忙吗?

I have two tables... 我有两张桌子

'mem table' id, username, email. “内存表” ID,用户名,电子邮件。

  |    1    |    2    |    3    |
  |   paul  |   john  | francis |       
  | paul@gm.| john@gm.|francis@.|

'location table' id, location, user_id << forign key “位置表” ID,位置,用户ID <<外键

  |    1    |    2    |    3    |
  |  leeds  |liverpool| london  |       
  |    ?    |    ?    |    ?    | 

how do I get the 'id' in the user table to automatically get inserted here as a foreign key? 如何获取用户表中的“ id”以自动将其作为外键插入此处?

Im using this php code to insert the data... 我正在使用此php代码插入数据...

$sql = mysql_query("INSERT INTO mem (username, country, state, city, accounttype, email, password, signupdate) 
        VALUES('$username','$country','$state','$city','$accounttype','$email','$hashedPass', now())")      or die(mysql_error());
$id = mysql_insert_id();

$sql = mysql_query("INSERT INTO location (location) 
VALUES('$location')") or die(mysql_error());
$user_id = mysql_insert_id();

Im getting this error when I submit the form... Cannot add or update a child row: a foreign key constraint fails ( mem . location , CONSTRAINT location_ibfk_1 FOREIGN KEY ( user_id ) REFERENCES location ( id ) ON DELETE CASCADE ON UPDATE CASCADE) 我在提交表单时收到此错误...无法添加或更新子行:外键约束失败( mem location ,CONSTRAINT location_ibfk_1键( user_id )参考locationid )在更新级联时删除级联

Can anyone help? 有人可以帮忙吗?

your FK is in the wrong direction. 您的FK方向错误。 In the moment it says: "Every entry in user table MUST have a entry in location table." 现在它说:“用户表中的每个条目都必须在位置表中具有一个条目。” You try to insert in the user table, without creating a location entry first. 您尝试插入用户表中,而不先创建位置条目。 So the FK fails. 因此FK失败。

solution: Set the FK on the location table "user_id -> users.user_id" so it means "every user_id on the location table should have an entry in the users table.". 解决方案:在位置表“ user_id-> users.user_id”上设置FK,因此这意味着“位置表上的每个user_id在users表中都应有一个条目”。 The only Problem is: the "ON DELETE CASCADE" is no longer working, when you delete a entry in the users table... - This can be solved with a database trigger (PSEUDOCODE) "BEFORE delete FROM users DELETE FROM locations WHERE user_id" or you just do 2 queries in PHP. 唯一的问题是:当您删除用户表中的条目时,“ ON DELETE CASCADE”不再起作用...-可以通过数据库触发器(PSEUDOCODE)解决“在从用户中删除之前,请先从用户所在的位置删除用户ID。 ”,或者您只需在PHP中执行2个查询。 (which is the easier solution. 1st delete location row, 2nd the user row.) (这是更简单的解决方案。第一个删除位置行,第二个删除用户行。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM