I have a table of items that are each individually associated with a category by means of an integer in a row called category. I also have a display order for each item, relevant to its associated category. This is saved in a row called pictureorder. Straightforward?
My problem arrises when the user deletes a category that contains items/pictures that are associated with it. I tell mySQL to search for any items associated with the category and to associate them with a ‘default’ category which is denoted by 0.
// IF THERE'S ANY PICTURES THAT WERE IN THAT CATEGORY,
// MOVE THEM INTO THE DEFAULT CATEGORY.
$uncategorise = "UPDATE pictures
SET category = '0'
WHERE category = '$categoryID'
AND username = '$userID'";
$queryUncat = mysql_query($uncategorise) or die(mysql_error());
This works. However, the items’ pictureorder is broken as items transferred from the deleted category may create duplicate pictureorders in the default category. I’m aware of how to solve this problem in a manner which would make multiple calls to the database. This feels too expensive. My query is whether I can use some technique I’m unaware of to update an incrementing pictureorder integer to the database as it’s called without making that row a key? Something along the lines of:
$uncategorise = "UPDATE pictures
SET category = '0',
pictureorder = 'TotalofDefaultCategory++'
WHERE category = '$categoryID'
AND username = '$useID'";
$queryUncat = mysql_query($uncategorise) or die(mysql_error());
Thanks in advance!
Update1
Based on Kyle’s suggestion below, I am trying the following without success:
UPDATE pictures
SET category = '0',
pictureorder = (SELECT COUNT(category) + 1 WHERE category='0' AND username='$useID')
WHERE category = '$categoryID'
AND username = '$useID';
If I omit pictureorder = (SELECT COUNT(category) + 1 WHERE category='0' AND username='$useID'), the category does indeed get changed to ‘0’ whenever the category matches $categoryID and $useID. However, if I include the addtional line, nothing changes. I don’t get any errors but pictureorder doesn’t change. Nor does category. Is the syntax correct?
Also, if I replace pictureorder = (SELECT... with pictureorder = 2 it works as expected. So I can’t see a possible problem other than the syntax being incorrect (I hope it’s not, as I feared, that it was too good to be true that you could include a COUNT in this manner).
I’ve been scratching my head for two days solid on this one as it turns out none of the solutions were quite right. Marc B led me on the right path although I couldn’t add to a user-defined variable in a second query in PHP, though I was able to call on it (hence some misguided trials and errors). There is a way of querying MySQL in PHP that can make multiple queries and connect them but the
mysql_querymethod I was already using was too deeply interwoven in my site to make it an easy change.Instead, based on the fact that the category to be deleted ($categoryID) had existing pictureorders starting from 1 and incrementing. I was able to use this existing information to add onto the COUNT suggestion made on this page. So, although I had to use two queries, for the second one I simply added the existing pictureorder of the deleted category onto the total count of the default category for the current user.