Can I do this to update a row? This is the first time I am using a LEFT JOIN with an UPDATE.
UPDATE comments AS r
LEFT JOIN items AS i
ON i.items_id = r.item_id
LEFT JOIN master_cat AS c
ON c.cat_id = i.items_id
SET r.good = r.good + 1
WHERE i.item = '{$item}' AND c.category = '{$cat}' AND r.review = '{$review}';
It doesn’t throw an error, but it says 0 rows affected even though I have confirmed my variables are ok with right data. (Even if I hard code the data).
Edit: I have tried inner join as well, and does not work either.
Edit 2: This is actually what I am trying to do, I have these two queires and they work fine. I am trying to simplify code to one. Can this be done:
// This query returns rate_id which I use as "$review_id" in the second query.
$query = "
SELECT r.rate_id
FROM comments as r
LEFT JOIN items AS i
ON i.items_id = r.item_id
LEFT JOIN master_cat AS c
ON c.cat_id = i.cat_id
WHERE r.review = '{$review}'
AND i.item = '{$item}'
AND c.category = '{$cat}';";
$query = "
UPDATE comments
SET good = good + 1
WHERE rate_id = '{$review_id}';";
Your problem is that in the
UPDATEstatement you have a mismatching join (cat_idwithitems_id):while in the
SELECT(that is working), you have the join correctly: