Insert MySql Error

0

Well, I'm trying to create a menu, and my sql is returning the following error: This is the result they are getting, I gave an echo to see if it was going correctly. Who we are is the name that identifies the menu, 2 is the code of who is the superior of it and 0 is which publication it is linked to. For min the error is in 0 because there is no publication so it assumes the value 0, but 0 is a valid value to be a menu id. Which is the right way to fix.

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 make a validation, where I take the field and equal it to zero, if true it returns null, if it is false I return the value of it, but 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)
    
asked by anonymous 15.06.2015 / 14:01

2 answers

1

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

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

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

If you do not want to remove the zero you can treat in your application and change zero by null.

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

    
15.06.2015 / 14:15
0

The solution to this problem is to make an If, in case the person was entering a value == 0, it does not include the value in the query, and if it inserts 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
}
    
15.06.2015 / 14:16