Hi all,
I'm coding a photo portfolio site & have a simple database of images, the primary key of which is the field 'img_id' (which is auto increment). My client has asked whether it will be possible to re-arrange the order in which the images are displayed on the site, e.g. if he wanted to 'move up' an image in priority from 10th to 1st.
I know that I could ORDER BY 'img_id', or by any other field for that matter. However, as I understand it, it wouldn't be possible to change the primary key of a row, so I created a separate 'sequence' field, 'sort_id'. However, if for example I want to change the sort_id of an image from 5 to 2, to avoid duplication I need to 'move up' all images with a 'sort_id' >=2. I've tried using the code below which I found in thse forums, but it's not working 😕 :
$query1 = "UPDATE images SET sort_id = (sort_id + 1) WHERE sort_id >= '$sort_id';";
$result1 = mysql_query($query1) or die ("Error in query: $query1. " . mysql_error());
$query2 = "UPDATE images SET sort_id = '$sort_id' WHERE img_id = '$img_id'";
$result2 = mysql_query($query2) or die ("Error in query: $query2. " . mysql_error());
Any help really appreciated 🙂