2010-12-02 123 views
1

我在MySQL中有一個表格來包含用戶創建的反機器人問題。但是,在安裝表格時,我想事先插入一些默認的反機器人問題。我想出了下面的代碼,但我不確定如何將它放入正確的語法。基本上,我創建了兩個數組(一個用於問題,另一個用於答案,按照各自的順序排列),並且想要使用foreach()或者while()函數循環訪問每個問題和答案。如何使用php將多行數據插入到mysql表中?

這裏是我的代碼:

$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

沿着線的東西:

$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 .= ", "; 
} 
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(這不SQL標準)

INSERT INTO TABLE(col1,col2) VALUES(1,1),(2,2),(3,3) 

我也發現它會更容易有像這樣的數組

$questions_answers = array(
    array('q' => 'How are you?', 'a' => 'Good') 
); 
+0

謝謝,那工作 – 2010-12-02 06:39:50

+0

@ Qlidnaque:歡迎你 – RageZ 2010-12-02 06:40:52

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

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 [] =「」; $ answers [] =「」; 你想寫一個問題和答案,每次從思考所有nummbers節省您和所有和讓你集中在什麼是重要的

這樣

@$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"; 
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 
    } 
} 

不僅每次查詢都不必重新分析,每次調用只傳遞準備好的語句的數據。另外,如果插入失敗,您將立即得到答覆,並處理它。做一個巨大的單一插入是不好的,因爲如果失敗了,你不會真正知道什麼是被忽略的,什麼都沒有,除非你實際上重新查詢了所有的東西。

相關問題