0
I’m trying to insert into 2 data tables, but I always have the error of
Cannot add or update a child row: a foreign key constraint fails
that is to say that it cannot insert in the second table, because in the first table the PK doesn’t exist yet.
I did so:
$insere_um = "INSERT INTO utilizadores(n_processo,nome,password,id,cod_grupo)
VALUES('".$n_processo."','".$nome."','".$pass."', '".$tipo['id']."', '".$grupo['cod_grupo']."')";
if(!mysqli_query($conexao,$insere_um))
{
echo mysqli_error($conexao);
}
else
{
$insere_dois = mysqli_query($conexao,"INSERT INTO utilizador_modulos(codigo_ut_mod,n_processo,cod_modulo)
VALUES
('".$n_processo."1',LAST_INSERT_ID(),'".$sql_mod1_001['cod_modulo']."'),
('".$n_processo."2',LAST_INSERT_ID(),'".$sql_mod1_002['cod_modulo']."'),
('".$n_processo."3',LAST_INSERT_ID(),'".$sql_mod1_003['cod_modulo']."'),
('".$n_processo."4',LAST_INSERT_ID(),'".$sql_mod1_004['cod_modulo']."'),
('".$n_processo."5',LAST_INSERT_ID(),'".$sql_mod1_005['cod_modulo']."'),
('".$n_processo."6',LAST_INSERT_ID(),'".$sql_mod2_001['cod_modulo']."'),
('".$n_processo."7',LAST_INSERT_ID(),'".$sql_mod2_002['cod_modulo']."'),
('".$n_processo."8',LAST_INSERT_ID(),'".$sql_mod2_003['cod_modulo']."'),
('".$n_processo."9',LAST_INSERT_ID(),'".$sql_mod2_004['cod_modulo']."'),
('".$n_processo."10',LAST_INSERT_ID(),'".$sql_mod2_005['cod_modulo']."'),
('".$n_processo."11',LAST_INSERT_ID(),'".$sql_mod3_001['cod_modulo']."'),
('".$n_processo."12',LAST_INSERT_ID(),'".$sql_mod3_002['cod_modulo']."'),
('".$n_processo."13',LAST_INSERT_ID(),'".$sql_mod3_003['cod_modulo']."'),
('".$n_processo."14',LAST_INSERT_ID(),'".$sql_mod3_004['cod_modulo']."'),
('".$n_processo."15',LAST_INSERT_ID(),'".$sql_mod3_005['cod_modulo']."'),
('".$n_processo."16',LAST_INSERT_ID(),'".$sql_mod4_001['cod_modulo']."'),
('".$n_processo."17',LAST_INSERT_ID(),'".$sql_mod4_002['cod_modulo']."'),
('".$n_processo."18',LAST_INSERT_ID(),'".$sql_mod4_003['cod_modulo']."'),
('".$n_processo."19',LAST_INSERT_ID(),'".$sql_mod4_004['cod_modulo']."'),
('".$n_processo."20',LAST_INSERT_ID(),'".$sql_mod4_005['cod_modulo']."')"
);
}
Strange interpretation that you made of the error message: "Cannot add or update a Child Row: the Foreign key Constraint fails that is to say that it cannot insert in the second table, because in the first table the PK does not exist yet." - could you tell us how you came to this conclusion to better understand the doubt?
– Bacco
Out of curiosity, because you’re doing the
insere_um
2 times, once in if and again in Else? I think so will become difficult to work. It would be nice to learn to do the most basic things before, this code of yours is too complicated for the kind of mistake you’re making. Go slowly, step by step, which helps to master the steps, otherwise accumulates a lot of place to then find the problem.– Bacco
I usually complicate where there is no @Bacco :) But this conclusion came about because I had already happened to me before this error but it worked with normal mysql instead of mysqli. I also met several people with the same mistake, and they said it was about that. This is a whole piece of code, basically
– programmer2016
As to the "I usually complicate where there is no @Bacco" Rest assured, that virtually every human being dominates this art of complicating :) - I just mentioned, because suddenly the problem is much simpler, and it is important that you do not rely on analyzing one possibility and forgetting others. Probably double insertion is already getting in your way. Try taking out the second occurrence on Else. and see if it changes the problem/error.
– Bacco
True @Bacco ::p. But I had already tried this, so I put it to try, but it did not. Just enter the data in the first table :\
– programmer2016