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.
Related
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
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.
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
Here is what I want to ask:
I want to make a system to register patients so then they will be able to login. I have 3 type of users though.
admin (no need for registration)
doctor (standard number of doctors, no need for registration)
patient (they will be registered)
I want to keep more info for them than just id, username, password, email.
I am thinking of having more than 1 tables to do this and link them with primary and foreign keys:
1st table
accounts (it will store the login data)
Example:
acc_id(primary key)
acc_password
acc_username
acc_type
2nd table
doctors_extra_info
Example:
acc_id (foreign key)
doc_info_id (primary key)
doc_name
...
...
3rd table
patients_extra_info
Example:
acc_id (foreign key)
pat_info_id (primary key)
pat_name
...
...
4th table
admin_info
Example:
acc_id (foreign key)
admin_id (primary key)
admin_email
a. Which is the best way of doing this?
b. In the part of
registration, how to deal with primary and foreign keys? Two insert
commands in two different tables? [In order to have the same acc_id
in the account table and the extra info table]
c. At the login part,
I need to check the type of user and redirect (header(Location: ..);)
to a page? Is this the right way of doing it?
Any suggestions?
Thank you.
If you're using PHP then when you insert a record you can instantly retrieve the ID created using mysql_insert_id(). You then use this to create other records as your foreign key.
With regards to redirects, I'd simply get the user type from the database and then check the type of user and redirect to page required.
Generally though the tables you have created do not correlate properly. Remember the defining thing about the people using the system is that they are a person, and shouldn't be deined by their job role. They should have a account_type_id linking to another table. Otherwise you have three tables essentially holding the same information.
For example you should have your tables like this
User table
user_id
first_name
last_name
email
account_type_id*
Accounts type table
user_id
account_type_id*
account_type //e.g. patient, doctor, admin etc
This means now that you can easily extend the database with new tables, user access levels, new columns without having to duplicate the same column across three tables and so on. Try reading up on database normalization. A very good video from youtube is http://www.youtube.com/watch?v=fg7r3DgS3rA
using php and mysql
I have two tables, a users table and a profiles table.
When a new account is created on my site the user and profile rows are created at the same time.
I want a foreign key to reference from the users table to the profiles table so that the users profile information will be stored in a seperate table.
My questions is, how do I ensure that the foreign key links to the correct profiles table.
The current way I would go about doing this is, after the new user and profiles row was created I would query the profiles in descending order to get the last row created and grab the profile id and insert it into the users table profile id foreign key reference. I'm sure that there must be a better way to do this though.. isn't there?
Create user with mysql_query();
Assuming the primary key of the users table is an auto-increment field, call mysql_insert_id();
Use that as the foreign key when you insert the profile.