我正在尝试将组名插入到我的数据库中。由于它们是用户组,我只想插入一次,所以如果我不小心再次插入它们,它应该拒绝它,因为它已经存在。我也在使用 PDO 语句。
// groups
$db->query("CREATE TABLE IF NOT EXISTS groups ( ID INT NOT NULL AUTO_INCREMENT,
name VARCHAR(200),
primary key (ID)
)");
// Fill tables
//Groups
$db = connectDatabase();
$name = 'Administrator';
$query = $db->prepare("INSERT INTO groups (name) SELECT :name WHERE NOT EXISTS (SELECT * FROM groups WHERE name = :name)");
$query->bindParam(':name', $name);
$query->execute();
$name = 'Moderator';
$query = $db->prepare("INSERT INTO groups (name) SELECT :name WHERE NOT EXISTS (SELECT * FROM groups WHERE name = :name)");
$query->bindParam(':name', $name);
$query->execute();
由于某种原因,它没有向表组添加任何内容。谁能帮我这个?
非常感谢