Reputation: 87
I have two tables in my PHP/MySQL database. On table which stores users details usernames etc and one that stores images that users have uploaded along with the username of who has uploaded that image. But if a user changes their username it changes in the user details table fine but not in the image upload table. So i can have a user in the user details table with one username e.g tom12 but in the image upload table i will have tom12 and all his previous usernames from when he has uploaded an image e.g Tom34, TMan etc.
How can i make it so that when the username changes in the user details table it will change in the image upload table aswell?
Upvotes: 1
Views: 102
Reputation: 1084
First: No need to get the reference of username on the image table. You need to do the reference of ID field as user_id in the image table.
If you donot want to change your approch, then you can do the casecading on username when you update it.
Upvotes: 0
Reputation: 3048
The best way to handle this is to have the user identified by a key (like a number) and store that number in the image table. That way when the user changes their name, the key can remain the same and not break the link.
Sample User table id - int name - string email - string
Sample Image table id - int user - int image_url - string
USERS
===================
ID NAME EMAIL
1 joe [email protected]
2 ben [email protected]
3 jerry [email protected]
IMAGES
===================
ID USER IMAGE_URL
1 1 http://somesite.com/images/mypic.jpg
2 1 http://somesite.com/images/myotherpic.jpg
3 1 http://somesite.com/images/funny.gif
4 3 http://yahoo.xj/users/jk/personal/holiday.jpg
Upvotes: 3