Insert Mysql error

Asked

Viewed 114 times

0

Well, I’m trying to create a menu, and my sql is returning the following error: These are the results you’re picking up, so I gave an echo to see if it was going properly. Who we are is the name that identifies the menu, 2 is the code of who is the superior of it and 0 is the publication it is linked to. For min the error is in 0, because there is no post so it is 0, but 0 is a valid value to be a menu id. What is the right way to correct.

Quem Somos20

Cannot add or update a child row: a foreign key constraint fails (`jrcomunicacoes`.`tbl_MENU`, CONSTRAINT `FK00` FOREIGN KEY (`COD_PUBLI_VINCU`) REFERENCES `tbl_PUBLICACOES` (`COD_IDENT_PUBLI`) ON DELETE NO ACTION ON UPDATE NO ACTION)

When I am sending to the bank I do a validation, where I take the field and equals zero, if it is true it returns null, if it is false I return its value, however it is not working.

        <div class="form-group">
        <?php   
            $query = mysql_query("SELECT COD_IDENT_MENUX, TXT_DESCR_MENUX FROM tbl_MENU");
        ?>
          <label>Superior:</label>
          <select class="form-control" id="COD_IDENT_SUPER" name="COD_IDENT_SUPER" >
            <option value="0" selected="selected">Menu Pai</option>
            <?php while($menu = mysql_fetch_array($query)) { ?>
              <option value="<?php echo $menu['COD_IDENT_MENUX'] ?>"><?php echo $menu['TXT_DESCR_MENUX'] ?></option>
            <?php } ?>
          </select>
        </div>

My query is this:

$query = "INSERT INTO tbl_MENU (TXT_DESCR_MENUX, COD_IDENT_SUPER, COD_PUBLI_VINCU) VALUES";
$query .=  "('$titulo','$pai','$publica')";

//executando a query
$inserir = mysql_query($query)
  • What part of your code are you entering? By error message you are wanting to change a record that is foreign key in another table.

  • I switched to go null, but also continues the same message @gmsantos

  • Renan probably this field of your table does not support nulls. It has the structure of her create ai ?

  • Have not can I get ? I am using Mysql Workbench

  • Right click on the table Copy to Clipboard > Create Statment

2 answers

1


This is happening because you are trying to insert an invalid value into your COD_PUBLI_VINCU foreign key. So

The first thing you should do is take the zero value of the option:

<option value="" selected="selected">Menu Pai</option>

If you don’t want to zero you can treat in your application and swap zero for null.

After that make sure that the column COD_PUBLI_VINCU accepts the value NULL.

0

The solution for this problem is to do an If, for if the person was entering a value == 0, it does not include the value in the query, and if it enters a value greater than zero or different, it only uses the value.

if ($publica == 0) {
        $query = "INSERT INTO tbl_MENU (TXT_DESCR_MENUX, COD_IDENT_SUPER) VALUES";
        $query .=  "('$titulo','$pai')";

        //executando a query
        $inserir = mysql_query($query)
        or die(mysql_error());

        $response = array("success" => true);

        //fechando a conexao com o banco
        mysql_close($conn);

        header("Location: cadastroMenu.php"); exit; // Redireciona o visitante

    }else{

    $query = "INSERT INTO tbl_MENU (TXT_DESCR_MENUX, COD_IDENT_SUPER, COD_PUBLI_VINCU) VALUES";
    $query .=  "('$titulo','$pai','$publica')";

    //executando a query
    $inserir = mysql_query($query)
    or die(mysql_error());

    $response = array("success" => true);

    //fechando a conexao com o banco
    mysql_close($conn);

    header("Location: cadastroMenu.php"); exit; // Redireciona o visitante
}

Browser other questions tagged

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