Home Ask Login Register

Developers Planet

Your answer is one click away!

Jip van Kakerken February 2016

MYSQL - UPDATE but keep old information

I've read tons of questions who are in many ways the same as this one. But I just can't seem to understand how I am supposed to do this the proper way.

  • I've got one table with my pages.
  • And one table with portfolio items.
  • I want to be able to say to a portfolio item: You are linked to the welcome page now.

My approach:

  • In my pages table i've created a "items_linked" column. Inside this column the id's of the linked portfolio items get stored.
  • In the html of the 'edit page' I have a select with all my portfolio items, whichever I select gets stored inside the "items_linked".
  • I use the mysql UPDATE to get the information inside the database.

However this way I can't link a portfolio item to more than 1 page. Because UPDATE removes the old information. So I was guessing I needed a way to keep the old info, and add new info if the item is linked to a second page. Can someone push me into the right direction?

Answers


Jay Blanchard February 2016

Since we have no way of knowing what we are replacing I can only give you an idea of what to do:

UPDATE `table`
SET `myColumn` = CONCAT(`myColumn`, other_data)
WHERE some_condition_exists

This causes the information in myColumn to have additional data appended to it.

Here's the thing (caveat ahead): If you need one item to be linked to many items what I have described is not the way to go. You should have a table between the two you have already which will allow a one-to-many relationship between pages and portfolios. Please consider refactoring your database design.

Post Status

Asked in February 2016
Viewed 3,715 times
Voted 14
Answered 1 times

Search




Leave an answer


Quote of the day: live life