Foreign Key "can not add or update a child row" - php

First off I am using both LibreBase and phpmyadmin
In short I have 2 tables a Student and a StudentFeedback table
Student contains:
StudentUserID(primary key) type=int(11) Null=no Default=none Extra=Auto_Increment
FirstName type=varchar(50) Null=Yes Default=Null
Surname type=varchar(50) Null=Yes Default=Null
StudentFeedback contains:
FeedbackID(primary key) type=int(11) Null=no Default=none Extra=Auto_Increment
Student(I think is foreign key) type=int(11) Null=Yes Default=Null
Feedback type=varchar(150) Null=no Default=none
Now what I want to happen is a student can be created using a form on librebase which is then added to the table e.g.
1 Joe Bloggs, 2 Bill Gates, 3 Steve Jobs.
However I have created another form to then add data to the feedback table so that Joe Bloggs or indeed Bill or Steve can use this form enter there user id and then add there feedback. This data will then be displayed by a variety of queries and reports which I am confident I can do. The problem arises when I try to add data via the librebase form.
The form to add a new student consists of:
Firstname (textbook)
Surname (textbox)
The data will however not enter/be input in to the table as librebase throws up an error box saying
Error inserting the new record
SQL Status: 23000
Error code: 1452
Cannot add or update a child row: a foreign key constraint fails >(`mydb`.`student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`StudentUserID`) >REFERENCES `StudentFeedback` (`Student`) ON DELETE CASCADE ON UPDATE >CASCADE)
The same error appears if I try to add data into the form I have created to add feedback to the StudentFeedback table
Apologies if this makes next to no sense I'll happily answer any questions you may have.

While Creating foreign key you have to give referencing properties:
ON UPDATE CASCADE
ON DELETE CASCADE
please go through the referencing properties.

I've solved the problem now, I had a constraint blocking the input or certain data and so used an sql query to view all my database constraints and then used another query to remove the relevant constraint. After that it's as simple as adding the foreign key ensuring that I used on update cascade on delete cascade

Related

SQL Table row is not getting deleted

I am having 4 tables
1. **user**
primary key:user_id
2. **task_details**
primary key:task_id
foreign key:user_id
3.**task_folder**
primary key:task_folder_id
foreign key:user_id
4.**task_details_folder_mapping**
primary key:task_details_folder_mapping_id
foreign key:task_id,task_folder_id
If I delete one folder from my project then that particular task in that folder must be deleted. But folder and tasks is deleting and updated in the front end. But task which is deleted is still shown in the table task_details. But the folder details are deleted from the tables task_folder and task_details_folder_mapping.
Please do help me to rectify the error. I am using codeigniter
Set ON DELETE CASCADE in your foreign key column task_Folder_id of task_details_folder_mapping table
ON DELETE CASCADE allows you to delete data from child tables automatically when you delete the data from the parent table.
In phpmyadmin..first of all should make data engine innodb..then go to parent table and add a cascade on deleting primary key ...
I wish this is helpful for you .thanks

Making duplicate Entries in php my admin

I want to make a table in php my admin where I can add in duplicate data into a column so I'd have Event ID and Band ID column and it'd be like (1,3), (1,5) (2,4)
So I can have more than one band relate to one event.
My two tables look like this:
Band_ID and Event_ID should be a composite primary key in the second table. How do I do this in PHPMyAdmin?
I edited your question to reflect your clarifications shown above. The new question is "how do I create a composite key in PhpMyAdmin?" (A composite key is when two columns make up a primary key. You asked "how do I make both Event ID and Band ID a unique key combination?")
To do this in PhpMyAdmin, go into the structure of the table, and select both columns at once. Then click the "key" icon below it, as shown:

Creating foreign key in phpmyadmin mysql

Creating a comment system with a simple rating system for each comment.
tables : 1.For the comments and it is called comments and it has three columns : id, name, comment
2. for the IP of the user that did the rating and it is called voted_ipand it has three columns id, comment_id, user_ip
The purpose of the voted_ip table is that i need to save the IP address for each rate to validate it that it cannot rate again if it exists.
I created a foreign key from the child table voted_ip in the column comment_id connecting it to the parent table comments in the column id following the steps at this link and this video on how to create a working foreign key except that the child table still do not update after a comment or a rate is inserted.
as follow :
I thought about that there might be another step or I have to do something in the php side of the project. What am I missing?
Data is not inserted in the other table "voted_ip" on insertion in "comment" by itself you have to add it explicitly this constraints are just for checking not for adding data in other table automatically.

Mysql:Cannot add or update a child row: a foreign key constraint fails

First of all, i know there are many posts about this subject but the ones i checked didnt help me so i wanted to post my problem.
I am building a website that people can register as user then fill some different forms for me. I have 2 table one is "user" and the other one is "form" table. In user table i have user_id which is auto_incremented and this id connected to "form" table as foreign key. I want to see which user filled forms via user_id. When i try to register filled form to database i get "Cannot add or update a child row: a foreign key constraint fails" message. How can i fix this?
If you have set the user_id on on your form table as a foreign key pointing at the user_id in the user table, you must supply a valid user_id when you insert the form row.
You could set the user_id field on the form table to be allowed to be NULL this will mean it does not have to be set.
you are trying to add some data to your foreign key column that there is no same data in other table .
This mostly happens when there is a data in table and you want to create/ edit/ delete a foreign_key OR
the field that you want to make it foreign_key is not the same format with Table field
solution:
You have to make a
1)backup or copy of you data
2)remove the foreign_keys
3)and Truncate data from table
4)Finally re attach the foreign_keys
5)update your data
I hope it helps

MySQL commit and rollback

I develop a webpage in that i face this problem. please see below.
In user table i have following fields:
Userid
password
Address
phone
Email
In service table i have the following fields:
Ser_name
ser_id
USER_Refid
All these are in same page in php.
When i add values in service table it stores under the same USER_Refid name .
For example the user xxxx is going to add service means the USER_Refid field contain the same name for all services .
My problem is when the user add values in services table and forgot to add the values in user table means i want to delete the values that inserted by that particular user . Please help me to solve this , Thanks in advance.
If your tables are InnoDB, you can a FOREIGN KEY constraint to your service table:
ALTER TABLE service_table ADD CONSTRAINT fk_service_user_refid FOREIGN KEY (USER_refid) REFERENCES user_table (USER_id)
To add the values into services, create a memory table with a session id, populate it with your services, and when the user clicks Save, do the following:
Create an entry for your user in the user_table
Move the entries from the temporary table into the service_table.

Categories