Insert statement yields "Cannot add or update a child row: a foreign key constraint fails"
===

I am working on a PHP application that uses a database extensively. My coworker who set up the database, set up the tables to use foreign keys. This question looks long but I feel that it's a simple one. I'm just providing all the relevant details. Anyway, here is my PHP script that is not working.

<!-- language: lang-php -->

	$query = $database->prepare(<<<HD
		INSERT INTO patients (ethnicity, gender)
		VALUES (:ethnicity, :gender);
	HD
	);
	$query->execute(array(
		':ethnicity'	=> $ethnicity + 1,
		':gender'		=> $gender + 1
	));

	$query = $database->prepare(<<<HD
		INSERT INTO sessions (patient_id, submitted, age_in_years, annotated)
		VALUES (LAST_INSERT_ID(), NOW(), :age_in_years, FALSE);
	HD
	);
	$query->execute(array(
		':age_in_years'	=> 0
	));

The second SQL statement is failing. When I do `print_r($query->errorInfo())`, I get the following.

<!-- language: lang-php -->

	Array
	(
	    [0] => 00000
	    [1] => 
	    [2] => 
	)

However, when I input this directly into phpMyAdmin (replacing `:age_in_years` with `0`), I get the following.

\#1452 - Cannot add or update a child row: a foreign key constraint fails (\`&lt;database name&gt;/sessions\`, CONSTRAINT \`fk_sessions_1\` FOREIGN KEY (\`patient_id\`) REFERENCES \`patient\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE)

How can I fix this?

Below is how we set up the `sessions` table

<!-- language: lang-sql -->

	CREATE TABLE IF NOT EXISTS `sessions` (
	  `id` int(11) NOT NULL auto_increment,
	  `patient_id` int(11) NOT NULL,
	  `severity` enum('Minimal Symptoms of Autism Spectrum Disorder','Mild-to-Moderate Symptoms of Autism Spectrum Disorder','Severe Symtoms of Autism Spectrum Disorder') default NULL,
	  `submitted` datetime default NULL,
	  `age_in_years` int(11) NOT NULL,
	  `video` text NOT NULL,
	  `annotated` tinyint(1) default '0',
	  PRIMARY KEY  (`id`),
	  KEY `fk_sessions_1` (`patient_id`),
	  KEY `age_in_years` (`age_in_years`)
	) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

	ALTER TABLE `sessions`
	  ADD CONSTRAINT `fk_sessions_1` FOREIGN KEY (`patient_id`) REFERENCES `patient` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;