I have a table that’s PK is an int field.
My problem is I need to swap two of the values round.
This is my current code:
$newId = (int)$id; $newId = $newId - 1;
$result = mysql_query("UPDATE homeScroller SET id = '$newId' WHERE id = '$id'") or die('error '.mysql_error());
$result2 = mysql_query("UPDATE homeScroller SET id = '$id ' WHERE id = '$newId'") or die('error '.mysql_error());
The table contains at max 3 rows that contain info for an slideshow on the frontpage of the website. The frontpage orders them by their IDs. So to change the order I need to edit the ID’s.
If that’s the case, then your application/database is poorly architect-ed, and you need to re-think your database design before you go any further.
Primary keys should be immutable, i.e.: they should never change. What you’re talking about doing is changing primary keys.
Fix your application/database, and you won’t have this problem. Are you sure you shouldn’t just be adding a new field ‘sort_order’ and ORDER BY sort_order instead or ordering by the ID field?