0

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.

Community
  • 1
  • 1
Abhinay
  • 1,796
  • 4
  • 28
  • 52
  • 1
    The dumb question: Does a record exist in the user table where U_id = $U_id? – AgRizzo Apr 05 '14 at 19:12
  • @AgRizzo Yes record is present in the user table – Abhinay Apr 05 '14 at 19:23
  • Which engines are you using on each of the two tables? – citizen404 Apr 05 '14 at 19:45
  • 1
    I made an [**SQL Fiddle**](http://sqlfiddle.com/#!2/f0e2e/1) and everything works perferctly. The only way I can reproduce the error is when a row is inserted into `student` before the corresponding row in `user` exists. But as you already wrote, such a record is present. Can you please double check that this is really true for each inserted row. And you should `var_dump()` your PHP variables used for the `INSERT` and check if the problem lies there. IMHO, it can only have to do with a non-existing record in the `user` table. – citizen404 Apr 05 '14 at 20:01
  • @mind-404 I found the issue actually I wasn't getting the U_id and that is why its not able to insert it into other table. thanks – Abhinay Apr 05 '14 at 21:17
  • Please post the code making the query! – djfm Apr 05 '14 at 21:37

1 Answers1

1

I made an SQL Fiddle and everything works perferctly.

The only way I can reproduce the error is when a row is inserted into student before the corresponding row in user exists. Can you please double check that this is really true for each inserted row.

Also, you should var_dump() your PHP variables used for the INSERT and check if the problem lies there. IMHO, it can only have to do with a non-existing record in the user table.

citizen404
  • 1,485
  • 1
  • 10
  • 19