外键约束错误

时间:2016-08-29 10:11:45

标签: php mysql sql

我正在创建一个表单来将数据插入到logement表和lit表以及表espace中。 我试图在所有这些像图像中那样连接的表中插入数据 enter image description here

但是我收到了这个错误:

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`)) 

我的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 {




我尝试了在stackoverflow上找到的所有解决方案,但我无法解决问题

3 个答案:

答案 0 :(得分:0)

如果客户端在logm-type中向您发送的字符串与您在switch语句中提到的四个字符串中的一个字符串不完全匹配,则您的代码将传递他们为您提供的字符串,就好像它是ID,在这种情况下,您看到的错误消息是最好的情况。

您的代码直接在SQL查询中使用POST值,这不是一个好主意 - 如果客户端(意外或故意)包含撇号会怎么样?

答案 1 :(得分:0)

现在我将代码更改为

$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."')";

但我收到了这个错误

警告:mysql_insert_id()期望参数1是资源,F中给出的对象: - SOFTWARE \ WEB-SERVER \ esyphp \ ep0002 \ data \ localweb \ school \ buckup \ BU-Projet \ 0008 \ ajoutez.php on第87行 Le logementestajouté.Lelogementestajouté.ERROR:无法执行INSERT INTO lit(TypeDeLit,idLogement)VALUES('沙发,小食,点亮','')。无法添加或更新子行:外键约束失败(yandexdlit,CONSTRAINT FK_Lit_idLogement FOREIGN KEY(idLogement)REFERENCES logement({{ 1}}))

答案 2 :(得分:0)

你的mysql_insert_id不按顺序调用。 它返回上次成功查询生成的ID,这意味着您必须先执行$ sql。

试试这个:

// 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);
}

请注意,如果$ sql的查询失败,则您不想执行$ sql1和$ sql2。