Jump to content

Archived

This topic is now archived and is closed to further replies.

Kingskin

If data exists already update, otherwise insert

Recommended Posts

Using MySQL & PHP, how can I first, check if a row with a given primary id exists and if so, update it, or if not insert it. Secondly, if data already exists in a given field, I want to append the new data onto it rather than overwrite it.

To clarify a bit, if I have 2 columns: id & value

I want to check if id (primary key) exists in the table.

If it doesn't exist, I want to insert a row with that 'id', and some data into 'values', such as '2 3 7 87'

If it [i]does[/i] exist, i want to update the row with that id, the append some data to whats already in the 'value' field, so if the 'value' field already contains the data '2 3 7 87' and i also want to add '13 5 9', i end up with '2 3 7 87 13 5 9' in that column.

Thanks for any help you can give me :)


Share this post


Link to post
Share on other sites
Recent versions of MySQL support INSERT... ON DUPLICATE KEY UPDATE... statements, which for certain cases, is preferable to a REPLACE statement. Check the refman for the appropriate syntax.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.