Jump to content

[SOLVED] Update ALL fields in database


timmah1

Recommended Posts

This might be hard to explain, but hopefully someone will be able to understand this.

I have a field in my database named 'main', that field either has a yes or no in it, with only 1 item can have yes in, the rest MUST have no.

The items in this will constantly change, with either more or less items.

When I update the field, how can I change every item in the database at the same time?

Meaning, when one is yes, the rest automatically change to no?

This goes back to my update thread, for a better example, think of MySpace, when you change your default picture.

Thanks again in advance
Link to comment
https://forums.phpfreaks.com/topic/32241-solved-update-all-fields-in-database/
Share on other sites

[quote]when one is yes, the rest automatically change to no?[/quote]

You would be best of to do this with a trigger but I think only mysql 5> support them.

You could simply do it this way though.

[code]
<?php
if (mysql_query("UPDATE tbl SET main = 'yes'")) { // update all to yes.
  if (mysql_query("UPDATE tbl SET main = 'no' WHERE id = 1")) { // update a particular row to no.
    echo "update success";
  }
}
?>
[/code]

Archived

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

×
×
  • Create New...

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.