Hi
I am trying a simple InnoDB table with foreign key constraints, but i can’t seem to pass the constrained value from HTML/PHP form … I have been on this for days and trawled forums and books, but no luck … any hints will be greatly appreaciated.
Here are the table structures for both tables
–
– Table structure for table parent
CREATE TABLE IF NOT EXISTS `parent` (
`id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
–
– Table structure for table child
CREATE TABLE IF NOT EXISTS `child` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`child_name` varchar(50) NOT NULL,
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `par_ind` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=61 ;
And this is the form for inserting a child, it retrieves the foreing parent_id constraint on child insert, and loads it into the form:
<?php include("database.php");
$id = $_GET['id'];
$query = "SELECT * FROM parent WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array( $result ); // Get selected entry ?>
<form name="test_fk" action="test_exec.php" method="POST">
<table >
<tr>
<td valign="top"><label for="parent_id"> Parent id </label></td>
<td><input type="text" disabled="disabled" name="parent_id" id="parent_id" value="<?php echo $row['id']; ?>" /> </td>
</tr>
<tr>
<td valign="top"><label for="child_name"> Child name </label></td>
<td><input type="text" name="child_name" id="child_name" /> </td>
</tr>
<tr>
<td> </td>
<td> <input name="Submit" type="submit" value="Save "/></td>
</tr>
</table>
</form>
and here is the PHP ‘insert’ script
<?php
// Error reporting
error_reporting (E_ALL ^ E_NOTICE);
include("database.php");
if(isset($_POST[Submit])){
$id = $_GET['id'];
$query = "INSERT INTO child (id, parent_id, child_name) VALUES ('', '$_POST[parent_id]','$_POST[child_name]')";
echo $query;
$result = mysql_query($query) or die(mysql_error());
//$result = mysql_query($query) or die('<br /><br /> MySQL query failed');
} // End else
echo $child_name;
?>
And this is the MySQL error i get:
INSERT INTO child (id, parent_id, child_name) VALUES (‘’, ‘’,‘Insert’)Cannot add or update a child row: a foreign key constraint fails (
widss
.child
, CONSTRAINTchild_ibfk_1
FOREIGN KEY (parent_id
) REFERENCESparent
(id
) ON DELETE CASCADE)
What i cant figure out is why the parent_id value is not inserting … it retrieves fine in the form …
Any help would be greatly appreaciated. Thanks in advance.