简体   繁体   English

外键约束的错误失败

[英]error with a foreign key constraint fails

i'm creating a form to insert data to logement table and lit table and also table espace. 我正在创建一个表单,以将数据插入logement表和light表以及表espace中。 i'm trying to insert data in all this tables that are connected like in the image enter image description here 我试图在所有连接的表中插入数据,就像在图像中那样在此处输入图像描述

but i'm getting this error : 但我收到此错误:

Warning: mysql_insert_id() expects parameter 1 to be resource, object given in F:\-- SOFTWARE\WEB-SERVER\esyphp\ep0002\data\localweb\school\buckup\BU-Projet\0008\ajoutez.php on line 88
ERROR: Could not able to execute INSERT INTO logement(Titre, AdresseLogement, Prix, NombrePerson, idTypeLogement) VALUES ('hgfrte','125669 hgfer','1236','33',''). Cannot add or update a child row: a foreign key constraint fails (`yandexd`.`logement`, CONSTRAINT `FK_Logement_idTypeLogement` FOREIGN KEY (`idTypeLogement`) REFERENCES `typedelogement` (`idTypeLogement`)) ERROR: Could not able to execute INSERT INTO lit(idLit, TypeDeLit) VALUES ('','kanape, canape'). Cannot add or update a child row: a foreign key constraint fails (`yandexd`.`lit`, CONSTRAINT `FK_Lit_idLogement` FOREIGN KEY (`idLogement`) REFERENCES `logement` (`idLogement`)) 

my php code is 我的PHP代码是

 if(isset($_POST['Submit'])){ /* Attempt MySQL server connection. Assuming you are running MySQL server with default setting (user 'root' with no password) */ $link = @mysqli_connect("localhost", "root", "", "yandexd"); // Check connection if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } switch ($_POST['logm-type']) { case "Maison": $_POST['logm-type'] = 1; break; case "Appartement": $_POST['logm-type'] = 2; break; case "Chambre_privée": $_POST['logm-type'] = 3; break; case "Chambre_partagée": $_POST['logm-type'] = 4; break; } $sql = "INSERT INTO logement(Titre, AdresseLogement, Prix, NombrePerson, idTypeLogement) VALUES ('".$_POST['logm-titre']."','".$_POST['logm-adresse']."','".$_POST['logm-prix']."','".$_POST['logm-personne']."','".$_POST['logm-type']."')"; $thelogm_id = mysql_insert_id( $link ); $sql1 = "INSERT INTO espace(TypeDEspace, idLogement) VALUES ('".$_POST['logm-espace']."','".$thelogm_id."')"; $sql2 = "INSERT INTO lit(TypeDeLit, idLogement) VALUES ('".$_POST['logm-typelit']."','".$thelogm_id."')"; if(mysqli_query($link, $sql)){ echo "Le logement est ajouté."; } else{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } if(mysqli_query($link, $sql1)){ echo "Le logement est ajouté."; } else{ echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link); } if(mysqli_query($link, $sql2)){ echo "Le logement est ajouté."; } else{ echo "ERROR: Could not able to execute $sql2. " . mysqli_error($link); } // close connection mysqli_close($link); } else { 

I tried all solution found on stackoverflow, but i can't solve the problem 我尝试了在stackoverflow上找到的所有解决方案,但无法解决问题

If the client sends you a string in logm-type that doesn't exactly match one of the four you've mentioned in your switch statement, your code will pass the string they gave you as if it was the ID, in which case the error message you're seeing is the best case scenario. 如果客户端向您发送的logm-type字符串与您在switch语句中提到的四个字符串之一不完全匹配,则您的代码将传递给他们的字符串,就好像它是ID一样,在这种情况下您看到的错误消息是最好的情况。

Your code is using POST values in SQL queries directly, which is not a good idea - what if the client (accidentally or deliberately) includes an apostrophe? 您的代码直接在SQL查询中使用POST值,这不是一个好主意-如果客户端(偶然或故意)包含撇号怎么办?

now i changed the code to 现在我将代码更改为

 $sql = "INSERT INTO logement(Titre, AdresseLogement, Prix, NombrePerson, idTypeLogement) VALUES ('".$_POST['logm-titre']."','".$_POST['logm-adresse']."','".$_POST['logm-prix']."','".$_POST['logm-personne']."','".$_POST['logm-type']."')"; $thelogm_id = mysql_insert_id( $link ); $sql1 = "INSERT INTO espace(TypeDEspace, idLogement) VALUES ('".$_POST['logm-espace']."','".$thelogm_id."')"; $sql2 = "INSERT INTO lit(TypeDeLit, idLogement) VALUES ('".$_POST['logm-typelit']."','".$thelogm_id."')"; 

but i did received this error 但是我确实收到了这个错误

Warning: mysql_insert_id() expects parameter 1 to be resource, object given in F:-- SOFTWARE\\WEB-SERVER\\esyphp\\ep0002\\data\\localweb\\school\\buckup\\BU-Projet\\0008\\ajoutez.php on line 87 Le logement est ajouté.Le logement est ajouté.ERROR: Could not able to execute INSERT INTO lit(TypeDeLit, idLogement) VALUES ('sofa, canape, lit',''). 警告:mysql_insert_id()期望参数1为资源,是F中给定的对象:-第87行的SOFTWARE \\ WEB-SERVER \\ esyphp \\ ep0002 \\ data \\ localweb \\ school \\ buckup \\ BU-Projet \\ 0008 \\ ajoutez.php错误:无法执行INSERT INTO lit(TypeDeLit,idLogement)VALUES('sofa,canape,lit','')。 Cannot add or update a child row: a foreign key constraint fails ( yandexd . lit , CONSTRAINT FK_Lit_idLogement FOREIGN KEY ( idLogement ) REFERENCES logement ( idLogement )) 不能添加或更新子行,外键约束失败( yandexdlit ,约束FK_Lit_idLogement外键( idLogement )参考logementidLogement ))

Your calling mysql_insert_id out of sequence. 您的mysql_insert_id调用不正确。 It returns the generated ID from the last successful query, which means you have to execute $sql first. 它从最后一次成功的查询返回生成的ID,这意味着您必须首先执行$ sql。

Try this: 尝试这个:

// this declares the sql query, but hasn't executed it. 
$sql = "INSERT INTO logement(Titre, AdresseLogement, Prix, NombrePerson, idTypeLogement)
    VALUES ('".$_POST['logm-titre']."','".$_POST['logm-adresse']."','".$_POST['logm-prix']."','".$_POST['logm-personne']."','".$_POST['logm-type']."')";

// this executes the sql.  Now mysql_insert_id can fetch the id. 
if(mysql_query($link, $sql)){
    echo "Le logement est ajouté.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// call mysql_insert_id after executing $sql, otherwise you'll just get null
$thelogm_id = mysql_insert_id( $link );
if( is_null($thelogm_id)){
    echo "ERROR: no id".
}

$sql1 = "INSERT INTO espace(TypeDEspace, idLogement)
    VALUES ('".$_POST['logm-espace']."','".$thelogm_id."')";


$sql2 = "INSERT INTO lit(TypeDeLit, idLogement)
    VALUES ('".$_POST['logm-typelit']."','".$thelogm_id."')";  

if(mysqli_query($link, $sql1)){
    echo "Le logement est ajouté.";
} else{
    echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link);
}

Be aware, if the query for $sql fails you'll not want to execute $sql1 and $sql2. 请注意,如果对$ sql的查询失败,则您将不希望执行$ sql1和$ sql2。

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

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