Please take a look of my two table definition : Domain and DomainEquivalentList and then the code of inserting data in the "DomainEquivalentList" table. But I am getting the following error:
SQL error:
Cannot add or update a child row: a foreign key constraint fails
Could you please help me?
CREATE TABLE Domain
(
INDEX (Domain_ID),
Domain_ID MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
UNIQUE (Name),
Name VARCHAR(255) NOT NULL,
Description BLOB NOT NULL,
PRIMARY KEY (Domain_ID)
) TYPE = INNODB;
CREATE TABLE DomainEquivalentList
(
INDEX (DomainEquivalentList_ID),
DomainEquivalentList_ID MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
INDEX (Domain_ID),
Domain_ID MEDIUMINT UNSIGNED NOT NULL,
INDEX (Next_ID),
Next_ID MEDIUMINT UNSIGNED,
FOREIGN KEY (Domain_ID) REFERENCES Domain (Domain_ID) ON DELETE CASCADE,
FOREIGN KEY (Next_ID) REFERENCES DomainEquivalentList (DomainEquivalentList_ID) ON DELETE CASCADE,
PRIMARY KEY (DomainEquivalentList_ID)
) TYPE = INNODB;
<?php
require(".......");
$link = mysql_connect($host,$user,$pass);
if (!$link)
die("Couldn't connect to MySQL");
mysql_select_db($db)
or die("Couldn't open $db: ".mysql_error());
$table = "DomainEquivalentList";
$Domain_ID = 5;
$Next_ID = NULL;
$query = "insert into $table values ('', '$Domain_ID', '$Next_ID')";
$result = mysql_query($query);
if (!$result)
{
print "<h1>SQL error:</h1>" .mysql_error();
}
else
print "Inserted";
?>