Skip to main content
dpavlo81
Participant
November 10, 2016
Question

Cannot add or update a child row: a foreign key constraint fails

  • November 10, 2016
  • 0 replies
  • 748 views

I try to create song list and when I try to insert song into list by singer i get this error:

Cannot add or update a child row: a foreign key constraint fails (`testdatabase`.`SongTable`, CONSTRAINT `SongTable_ibfk_1` FOREIGN KEY (`SingerID`) REFERENCES `SingerTable` (`SingerID`))

here is my code:

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "SongList")) {

  $insertSQL = sprintf("INSERT INTO SongTable (SongName) VALUES (%s)",

                       GetSQLValueString($_POST['SongName'], "text"));

  mysql_select_db($database_testing, $testing);

  $Result1 = mysql_query($insertSQL, $testing) or die(mysql_error());

  $insertGoTo = "index.php";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));

}

and my form code:

<form action="<?php echo $editFormAction; ?>" id="SongList" name="SongList" method="POST">

        <input type="text" name="SongName" id="SongName">

        <input type="submit" name="submit" id="submit" value="enter song">

        <input type="hidden" name="MM_insert" value="SongList">

</form>

thanks for any help

Davor

This topic has been closed for replies.