shaunysj — 2012-07-04T09:58:46-04:00 — #1
I can submit new article and upload picture for that article.
So lets say i submitted a new article with an image. Base on the table below, my new article_id will be 2. Then problem is how the foreign key in image table auto update the article_id = 2. So that the image name auto matches the article_id i newly submitted. How do i do that?
image table(stored all the article images):
id article_id(foreign key) name
1 1 xyz.jpg
system — 2012-07-04T10:16:41-04:00 — #2
You normally don't change id keys values.
PK/FK are not meant to be changed. That's the whole idea about id keys: values that say nothing about the data, so they should not change.
But if you have to, you could rely on ON triggers. And triggers have a history that doesn't recommend them for this job at all, keeping together the data integrity.
guelphdad — 2012-07-04T10:24:20-04:00 — #3
you use LAST_INSERT_ID to get the value inserted in the article table and use that to insert into the images table.
system — 2012-07-04T10:26:36-04:00 — #4
^ Right you are. I misread what the OP was asking. Sorry.
I don't use or recommend LAST_INSERT_ID myself, but that's another story altogether.
shaunysj — 2012-07-04T10:36:21-04:00 — #5
BIG THANKS to you. I solved my problem tyty!
mittineague — 2014-09-22T11:40:52-04:00 — #6
This topic is now archived. It is frozen and cannot be changed in any way.