I am getting this error whenever I try to Insert Data into my "Student" table. Below are the two tables, I am using MySql :
student table:
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+-------------------+----------------+
| S_id | int(20) | NO | PRI | NULL | auto_increment |
| U_id | int(11) | YES | MUL | NULL | |
| sname | varchar(20) | YES | | NULL | |
| gender | varchar(20) | YES | | NULL | |
| email | varchar(320) | YES | UNI | NULL | |
| Phone_Number | int(20) | YES | | NULL | |
user table:
Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+-------------------+----------------+
| U_id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| password | varchar(20) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
When I try to run this query ,
INSERT INTO student(U_id,sname,gender,email,Phone_Number) VALUES ('$U_id','$sname','$gender','$email','$Phone_Number');
I am getting this error:
Cannot add or update a child row: a foreign key constraint fails (`Learn`.`student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`U_id`) REFERENCES `user` (`U_id`))
I tried to look for the solution but most of them are saying keep a common Engine like InnoDB on both the tables but in my table its already defined.I have even followed the steps given here https://stackoverflow.com/a/9139206/2545197 but nothing worked for me.