I've hit a bit of a roadblock with one of my projects and I cant seem to get past it. I am trying to add a new row to one of my tables, but the foreign key seems to be causing me problems. As you will see from my code below, I am trying to insert a row into my 'job' table.
Error:
Query failed: Cannot add or update a child row: a foreign key constraint fails (`kanix_support_desk`.`job`, CONSTRAINT `job_ibfk_1` FOREIGN KEY (`agentId`) REFERENCES `agents` (`agentId`))
Tables:
CREATE TABLE IF NOT EXISTS `agents` (
`agentId` int(11) NOT NULL AUTO_INCREMENT,
`AgentFirstname` varchar(50) DEFAULT NULL,
`AgentSurname` varchar(50) DEFAULT NULL,
`username` varchar(25) DEFAULT NULL,
`password` varchar(25) DEFAULT NULL,
`rank` varchar(12) NOT NULL,
PRIMARY KEY (`agentId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `job` (
`jobId` int(11) NOT NULL AUTO_INCREMENT,
`agentId` int(11) NOT NULL,
`date` date NOT NULL,
`time` time DEFAULT NULL,
`jobName` varchar(100) NOT NULL,
`jobDescription` varchar(9999) NOT NULL,
`jobstatus` varchar(50) NOT NULL,
`customerFirstname` varchar(50) DEFAULT NULL,
`customerSurname` varchar(50) DEFAULT NULL,
`CustomerTelephoneNo` varchar(50) DEFAULT NULL,
PRIMARY KEY (`jobId`),
KEY `agentId` (`agentId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
Php:
session_start();
include("dbConnect.php");
$agent = $_SESSION['agent']; //Set agent name and ID variables
$agentid = $_SESSION['identity'];
//Check that no section of the form was left blank
if($_POST['fname'] != null && $_POST['sname'] != null && $_POST['phone'] != null && $_POST['jtitle'] != null && $_POST['jdesc'] != null){
mysql_query("INSERT INTO job (jobId, agentId, date, time, jobName, jobDescription, jobstatus, customerFirstname, customerSurname, customerTelephoneNo) VALUES (null, '".$agentid."', CURDATE(), NOW(), '".$_POST['jtitle']."', '".$_POST['jdesc']."', 'Live', '".$_POST['fname']."', '".$_POST['sname']."', '".$_POST['phone']."')") or die ("Query failed: " . mysql_error());
header("location:reception.php?content=3");
} else{ //Error message
header("location:reception.php?content=4");
}