我在MySQL中有一个表来包含用户创建的反机器人问题。但是,在安装表时,我想事先插入一些默认的反机器人问题。我提出了以下代码,但我不确定如何将其置于正确的语法中。基本上,我创建了两个数组(一个用于问题,另一个用于答案,分别用于匹配顺序。)并且希望使用foreach()或者while()函数循环遍历每对问答。
这是我的代码:
$questions = array(
"question1" => "What is 2+6?",
"question2" => "What color is the sky?",
"question3" => "What number is betwee 6 and 8?",
"question4" => "How many letters are there in the English alphabet?",
"question5" => "How many hours are there in a day?",
"question6" => "Are you a human or a bot?"
);
$answers = array(
"answer1" => "8",
"answer2" => "blue",
"answer3" => "7",
"answer4" => "26",
"answer5" => "24",
"answer6" => "human"
);
$x = 0;
foreach ($question[$x]) {
$sql = "INSERT INTO
administrator_instructions
(question, question_naswer)
VALUES
('" . $question[$x] . "','" . $answer[$x] . "')";
$x++;
}
答案 0 :(得分:1)
实际上,使用PDO会比制作一个巨大的查询字符串快得多。
$db = new PDO("connect string"); $stm = $db->prepare("INSERT INTO administrator_instructions (question, question_naswer) VALUES(?,?)"); foreach( $q_a as $q => $a ) { if( $stm && $stm->execute(array($q, $a)) ) { // Handle insert } else { // Handle error } }
不仅每次都不需要重新解析查询,每次调用只传递预准备语句的数据。此外,如果插入失败,您将立即得到响应并处理它。做一个巨大的单个插入是不好的,因为如果它失败了,你真的不知道什么是被插入的,什么没有,除非你实际上重新查询所有内容。
答案 1 :(得分:0)
$questions = array(
"question1" => "What is 2+6?",
"question2" => "What color is the sky?",
"question3" => "What number is betwee 6 and 8?",
"question4" => "How many letters are there in the English alphabet?",
"question5" => "How many hours are there in a day?",
"question6" => "Are you a human or a bot?"
);
$answers = array(
"answer1" => "8",
"answer2" => "blue",
"answer3" => "7",
"answer4" => "26",
"answer5" => "24",
"answer6" => "human"
);
$x = 0;
$sql = 'INSERT INTO
administrator_instructions
(question, question_naswer)
VALUES'
for ($i = 0; $i < count($question); $i++) {
if($i){
$sql .= ','
}
$sql . = "('" . $question[$i] . "','" . $answer[$i] . "')";
}
mysql_query($sql);
mysql支持使用此语法的一些批量插入(这不是SQL标准)
INSERT INTO TABLE(col1,col2) VALUES(1,1),(2,2),(3,3)
我也发现像这样的数组更容易
$questions_answers = array(
array('q' => 'How are you?', 'a' => 'Good')
);
答案 2 :(得分:0)
您可以构建这样的命令并执行一次
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
有关详细信息,请参阅this
答案 3 :(得分:0)
$questions = array(
"0" => "What is 2+6?",
"1" => "What color is the sky?",
"2" => "What number is betwee 6 and 8?",
"3" => "How many letters are there in the English alphabet?",
"4" => "How many hours are there in a day?",
"5" => "Are you a human or a bot?"
);
$answers = array(
"0" => "8",
"1" => "blue",
"2" => "7",
"3" => "26",
"4" => "24",
"5" => "human"
);
$row = array_combine($answers,$questions);
foreach($row as $k=>$v){
$sql = "INSERT INTO
administrator_instructions
(question, question_naswer)
VALUES
('" . $k . "','" . $v . "')";
}
我觉得这很容易
你摆脱了柜台,你可以简单地做 $问题[] = “”; $答案[] = “”; 每次你想写一个问题和答案都可以让你不再考虑所有的数字和所有数字,并让你专注于重要的事情
像这样@$questions[] = "What is 2+6?";
$questions[] = "What color is the sky?";
$questions[] = "What number is betwee 6 and 8?";
$questions[] = "How many letters are there in the English alphabet?";
$questions[] = "How many hours are there in a day?";
$questions[] = "Are you a human or a bot?";
@$answers[] = "8";
$answers[] = "blue";
$answers[] = "7";
$answers[] = "26";
$answers[] = "24";
$answers[] = "human";
答案 4 :(得分:0)
以下内容:
$q_a = array(
'what is 2 + 6' => '8',
'what color is the sky?' => 'blue',
// etc ...
}
$i = 0;
$cnt = count(array_keys($q_a));
$sql = 'insert into administrator_instructions (question, question_answer) values ';
foreach ($q_a as $q => $a) {
$sql .= sprintf("('%s', '%s')", mysql_real_escape_string($q), mysql_real_escape_string($a));
$i++;
if($i < $cnt) $sql .= ", ";
}