【发布时间】:2013-11-13 20:25:17
【问题描述】:
我正在尝试为我的聊天发出一种好友请求,
所以我设置了一个名为cyb_user_friendlist的表
然后我放了一些这样的表格:
1 id_friendlist int(11) AUTO_INCREMENT
2 from int(11)
3 to int(11)
4 couple varchar(11)
5 accept int(11)
6 block int(11)
因此,对于每个朋友请求,都会在此表中插入发送者的 id 到 from 和接收者的 id 到 to,但是为了确保每对夫妇只有一个请求,我添加了一个名为 couple 的字段,其中有from 和 to 与垂直分隔符 | 的连接。该字段有一个 uniq 键,因为我想防止出现多条记录。
唯一的问题是它似乎不起作用,实际上我在这个字段中添加了我的 uniq 键和 id_friendlist 的主键,但它不起作用,我可以根据需要发送许多请求...
我的请求 $sql 如下:
$query = "INSERT INTO `cyb_users_friendlist` SET
`from` = {$from},
`to` = {$to},
`couple` = '{$from}|{$to}'";
我真的不知道我哪里错了……
我们将不胜感激。
【问题讨论】:
-
不确定这里的问题是什么
-
问题是即使使用索引唯一键它也会插入重复条目...我也尝试使用插入忽略但它不起作用
-
您不需要“情侣”,您可以在 2 个字段上添加唯一索引。更改表
YOUR TABLE NAME添加唯一unique(from,to) -
你能发布
show create table cyb_users_friendlist的输出吗?听起来您似乎没有正确创建唯一键。 -
REATE TABLE
cyb_users_friendlist(id_friendlistint(11) NOT NULL AUTO_INCREMENT,fromint(11) NOT NULL,toint(11) NOT NULL,couplevarchar( 11) 非空,acceptint(11) 默认空,blockint(11) 非空默认 '0',主键 (id_friendlist),唯一键id_friendlist(id_friendlist),唯一键couple(couple) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1
标签: php mysql sql indexing unique-key