伙计们,我在尝试更新表中的列时遇到问题。
我有存储朋友关系和朋友请求的桌子。
好友表如下所示:
`friend_one` INT(11) ,
`friend_two` INT(11) ,
`status` ENUM('0','1','2') DEFAULT '0'
我使用以下插入语句来添加朋友。
INSERT INTO friends
(friend_one,friend_two)
VALUES
('$user_id','$friend_id');
所有工作正常,但是我尝试确认朋友请求将状态更新为 1 返回我 0 行受影响,但查询成功。
对于确认请求:
UPDATE friends
SET status="1"
WHERE
(friend_one="$user_id" OR friend_two="$user_id")
AND
(friend_one="$friend_id" OR friend_two="$friend_id");
所有的时间都是一些:
执行 1 个查询,1 个成功,0 个错误,0 个警告
查询:更新好友设置状态=1,其中(friend_one=2或friend_two= 2) 和 (friend_one=1 或 friend_two=1)
0 行受影响
执行时间 : 0.037 秒 传输时间 : 0 秒 总时间 : 0.038 秒
我在表中只有一条记录,但从未将状态更改为 1:
friend_one friend_two status
---------- ---------- --------
2 1 0
这样做:
UPDATE friends
SET status="1"
WHERE
(friend_one="$user_id" AND friend_two="$friend_id") // check the change in line
OR // replace AND with OR
(friend_one="$friend_id" AND friend_two="$user_id");// check the change in line
放入运算符
OR
而不是AND
:
UPDATE friends
SET status="1"
WHERE
( (friend_one="$user_id" OR friend_two="$user_id")
OR
(friend_one="$friend_id" OR friend_two="$friend_id") );
嗨,
我认为更好的方法是使用唯一的友谊 ID,例如:
friendship_id | sender_id | reciever_id | status
1 5 6 0
因此,当您发送确认请求时,查询将如下所示
$sql="SELECT friendship_id from friends where sender_id='$sender' and reciever_id='$reciever' OR sender_id='$reciever' and reciever_id='$sender'";
$row=mysqli_fetch_assoc($sql);
然后使用此查询将其发送到确认脚本
$confirm="UPDATE friends SET STATUS=0/1/2 where friendship_id='$row[friendship_id]'";
希望这有效!我愿意讨论!谢谢!
更改 OR 运算符而不是 AND 运算符试试这个。
UPDATE friends
SET status="1"
WHERE
(friend_one="$user_id" AND friend_two="$friend_id") // check the change in line
OR // replace AND with OR
(friend_one="$friend_id" AND friend_two="$user_id");// check the change in line