Error entering data into BD using mysqli

Asked

Viewed 37 times

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']."')"
    );
  }
  • 2

    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?

  • 2

    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.

  • 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

  • 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.

  • 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 :\

1 answer

0

I just found it strange one thing, you use in the first INSERT the variable $n_process, however in the second INSERT you concatenate $n_process with another character, is that this is not what is making the script give the error to perform the insertion?

  • This was a way that I arranged to be the primary key of the second table. using the primary of the previous table with an "increment". : ) But that’s not the problem.

  • But this must be the problem, if in Table 1 the record has the primary key with the value 10, it has to relate in Table 2 with the value 2, otherwise, I believe that there will not be a link between the records.

  • I got it working, I just switched the LAST_INSERT_ID() for $n_processo which is the primary key of the first table and FK of the second. But the LAST_INSERT_ID() supposedly not doing the same?

  • 1

    The way you used it wouldn’t work, it needs to know which table it will request the primary key from.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.