结构化表以使用 PDO 接受唯一注释


Structuring table to accept unique comments using PDO

如何构建我的表格,使其不接受来自特定帖子的相同名称的双重"相同"评论,同时允许用户在另一个帖子下使用相同的评论和名称发表评论?

$con->query("create table comment(id int(11) not null auto_increment, post_id varchar(20) not null, comment varchar(1000) not null, name varchar(20) not null, date datetime not null, primary key(id))");

您可以使用UNIQUE KEY一起定义 id、name 和注释的唯一性。 这样做 mySQL 将不允许在表中插入相同的组合。

尝试
$con->query("create table comment(id int(11) not null auto_increment, post_id varchar(20) not null, comment varchar(1000) not null, name varchar(20) not null, date datetime not null, primary key(id),UNIQUE KEY `unique_together` (`id`,`name`,`comment`))");

希望对:)有所帮助