简体   繁体   English

无法添加或更新子行:外键约束失败

[英]Cannot add or update a child row: a foreign key constraint fails

SQLSTATE[23000]: Integrity constraint violation: 1452 SQLSTATE [23000]:违反完整性约束:1452

Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败

( whnkepbk_Hland . interests , CONSTRAINT interests_ibfk_3 FOREIGN KEY ( scale_id ) REFERENCES scales_reference ( scale_id )) whnkepbk_Hlandinterests ,约束interests_ibfk_3外键( scale_id )参考scales_referencescale_id ))

I know that the problem is because I have to set value to scale_id to add this data line. 我知道问题是因为我必须将value设置为scale_id才能添加此数据行。

And I do it : 我做到了:

    $requete = "INSERT INTO interests (onetsoc_code,element_id,scale_id,data_value) VALUES (:onetsoc_code,:element_id,:scale_id,:data_value)"; 
...           
$InsertMetier->bindParam(':scale_id', $scale);
            var_dump($scale);
            var_dump($InsertMetier);
            $InsertMetier->execute();

Here the content of the var_dump : 这里是var_dump的内容:

</pre>Ol<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'Ol'</font> 

'INSERT INTO interests (onetsoc_code,element_id,scale_id,data_value) VALUES (:onetsoc_code,:element_id,:scale_id,:data_value)'

My scale variable do have a value according to the var_dump and my query sounds legitimate. 我的scale变量确实具有根据var_dump的值,并且我的查询听起来合法。

Here a screenshot of my database (interest) : 这是我的数据库的截图(兴趣):

在此处输入图片说明

scales_reference : scales_reference

在此处输入图片说明

Structure of interest : 兴趣结构:

在此处输入图片说明

Structure of scale_reference : scale_reference结构:

在此处输入图片说明

If someone has any idea what I have done wrong. 如果有人知道我做错了什么。

MySQL error Integrity constraint violation: 1452 from MySQL manual : MySQL错误Integrity constraint violation: 1452 MySQL手册提供的 Integrity constraint violation: 1452

For storage engines supporting foreign keys, MySQL rejects any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table. 对于支持外键的存储引擎,如果父表中没有匹配的候选键值,则MySQL拒绝任何试图在子表中创建外键值的INSERT或UPDATE操作。

That means you are either: 这意味着您是:

  1. Inserting into your interest table with scale_id value, which doesn't exist in the parent table ( scale_reference ). 使用scale_id值插入您的interest表, scale_id值在父表( scale_reference )中不存在。
  2. Having error in your query and inserting empty scale_id - from the code snippet it is not clear how you are replacing all placeholders ( :xx ) with actual values from PHP variables and we don't see the actual query as it is sent to the MySQL server. 查询中出错并插入空的scale_id从代码片段中尚不清楚如何用PHP变量的实际值替换所有占位符( :xx ),并且在发送到MySQL时我们看不到实际查询服务器。

You can try to run the desired query directly on MySQL without PHP. 您可以尝试直接在没有PHP的MySQL上运行所需的查询。 If it works, then you have issue in your PHP code. 如果可行,则您的PHP代码中有问题。

Ol和OI之间有什么区别,您甚至无法在stackoverflow上看到它,“ oi”和“ ol”怎么样,是的,有很大的不同,我没有放置“ OI”,但我放置了OL,所以那是问题。

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

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