I have a strange issue with my PHP PDO with MySQL on Windows 10, which fails with an Integrity Constraint Exception while attempting to store a record in the child table despite the entry in parent gets created successfully.
I have the following tables in my Database, namely TblUser, TblRole, TblUserRole and TblStudent – where I capture the essential info of the logged in User in TblUser, and the master roles in TblRole, the mapping of User and Roles in TblUserRole, the details of the students in TblStudent.
The ID column of the TblUser is a FK in TblUserRole and TblStudent, and the ID Column of TblRole is a FK in TblUserRole.
TblUser
CREATE TABLE TblUser
(
Id int primary key auto_increment not null,
UserId VARCHAR(50) UNIQUE NOT NULL COMMENT 'Unique Registration No',
Password VARCHAR(50) NOT NULL
) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;
TblRole
CREATE TABLE TblRole
(
Id int primary key auto_increment not null,
Name VARCHAR(50) UNIQUE NOT NULL COMMENT 'The name of the Role',
Description VARCHAR(50) NOT NULL COMMENT 'The Description of the Role'
) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;
TblUserRole
CREATE TABLE TblUserRole
(
Id int primary key auto_increment not null,
UserId INT NOT NULL COMMENT 'The Id of the User',
RoleId INT NOT NULL COMMENT 'The Id of the Role',
UNIQUE INDEX (UserId, RoleId),
CONSTRAINT fk_UserRole_UserId
FOREIGN KEY (UserId)
REFERENCES TblUser(Id)
CONSTRAINT fk_UserRole_RoleId
FOREIGN KEY (RoleId)
REFERENCES TblRole(Id)
) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;
TblStudent
CREATE TABLE TblStudent
(
Id int primary key auto_increment not null,
UserId INT NOT NULL COMMENT 'The Id of the User',
RegnNo VARCHAR(15) UNIQUE NOT NULL,
Name VARCHAR(50) NOT NULL,
DOB DATE NOT NULL,
Gender CHAR(1) NOT NULL,
Department VARCHAR(50) NOT NULL,
Year INT NOT NULL
CONSTRAINT fk_TblStudent_UserId
FOREIGN KEY (UserId)
REFERENCES TblUser(Id)
) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;
Problem Statement: In my PHP application, I need to check and add a Student with the Registration Id in the System – in the tables as follows.
- First add an entry in the TblUser (both UserId and Password will be the same as that Registration Id initially, later it will be changed) — PASS
- Create a Student Role for the user in the TblUserRole table — PASS
- Create an entry in the TblStudent with the rest of the attributes like Age, Gender, Department ,Year etc., — FAIL
I do it all in one flow from from the PHP Script acts a Controller to the DAO (with different methods for each Table). I use a little older of version of XAMPP (v3.2.2) on Windows 10 – FYI.
Issue: A strange scenario / issue I face is that I am able to successfully add an entry in TblUser and also in TblUserRole (where UserId is a FK), but NOT in the TblStudent, which always keep failing with the IntegrityConstraintViolation exception that says the Child/Foreign key is missing.
I have even verified and printed the value from TblUser in the middle of the flow, just to ensure the saved data was flushed by PDO to the MySQL Database. I have been struggling with this strange behavior of PHP PDO. I have also tried adding many debugging statements with the help of print_r and var_dump and all prove the values are valid.
I will really appreciate if someone throws a light on this strange behavior and the gaps if any I have in my flow of execution.
Let me know if you need any other information pertaining to the issue.