如何更改此 SQL 以执行以下操作:
- 如果用户不在表中,请添加它们。[这部分有效]
- 如果用户在表中
favourite_active = 0
并将其更改为favourite_active = 1
. - 如果用户在表中
favourite_active = 1
并将其更改为favourite_active = 0
.
我需要添加我认为的 WHERE 子句。
$result = mysql_query("SELECT * FROM tbl_favourites WHERE (user_id = '$who' AND favourite_id = '$usernum' AND favourite_active = '1')");
// if user does not exist in favourites, add them
if(mysql_num_rows($result) == 0)
{
mysql_query("INSERT INTO tbl_favourites (user_id, favourite_id, favourite_active) VALUES ('$who', '$usernum', '1')");
echo"You have added this user as a favourite";
}
// if user does exist and favourite_active = 1 change to 0
else {
mysql_query("UPDATE tbl_favourites SET favourite_active='0' WHERE user_id='$who' AND favourite_id='$usernum'");
echo"You have removed this user as a favourite";
}
// if user does NOT exist and favourite_active = 0 change to 1
else {
mysql_query("UPDATE tbl_favourites SET favourite_active='1' WHERE user_id='$who' AND favourite_id='$usernum'");
echo"You have removed this user as a favourite";
}