我有这个脚本向MySql添加新用户,现在我已经为黑名单创建了辅助Mysql表。所以我希望MySql在允许插入数据之前检查黑名单。
$all = implode(",",$_POST);
$all = explode(",",$all);
$insert = "INSERT INTO name VALUES (";
for ($x=0;$x<5;$x++)
{
$all[$x] = clean($all[$x]);
if ($all[$x] == "" || strlen($all[$x]) > 300)
die("<p>Please fill in the whole form." . $x);
$insert .= "'" . $all[$x] . "',";
}
$insert .= "'')";
$res = @mysql_query($insert) ;
我已改为
$insert = "INSERT INTO name VALUES (";
for ($x=0;$x<5;$x++) NOT IN (SELECT `blacklist` FROM `exclude`)
但没有得到我想要的东西。我该如何解决这个问题?
答案 0 :(得分:0)
据我所知,您将不得不使用与该名称表共享的该表的关键字来对您的排除表进行单独选择,以检查该用户是否在排除列表中。 没有INSERT语法允许像INSERT这样的概念,除非在另一个表中存在某些东西。
答案 1 :(得分:0)
事实上,你可以(ab)使用INSERT ... SELECT
做你想做的事:
-- insert one name:
INSERT INTO user SELECT "john" FROM DUAL
WHERE "john" NOT IN (SELECT name FROM blacklist);
INSERT INTO user SELECT "ringo" FROM DUAL
WHERE "ringo" NOT IN (SELECT name FROM blacklist);
-- insert several names
CREATE TEMPORARY TABLE candidate(name char(40));
INSERT INTO candidate VALUES
("paul"), ("ringo"), ("georges");
INSERT INTO user SELECT name FROM candidate
WHERE name NOT IN (SELECT name FROM blacklist);
有关示例,请参阅http://sqlfiddle.com/#!2/7ea68/1。