Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Nov 10, 2016 Nov 10, 2016

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

752
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation