How do I insert multiple rows of data into a mysql table using php? -
i have table in mysql contain user created anti-bot questions. when installing tables however, want insert default anti-bot questions beforehand. came following code i'm not sure how put correct syntax. basically, created 2 arrays (one questions , answers, in respective , matching order.) , want cycle through each pair of question , answer using foreach() or perhaps while() function.
here's code:
$questions = array( "question1" => "what 2+6?", "question2" => "what color sky?", "question3" => "what number betwee 6 , 8?", "question4" => "how many letters there in english alphabet?", "question5" => "how many hours there in day?", "question6" => "are human or bot?" ); $answers = array( "answer1" => "8", "answer2" => "blue", "answer3" => "7", "answer4" => "26", "answer5" => "24", "answer6" => "human" ); $x = 0; foreach ($question[$x]) { $sql = "insert administrator_instructions (question, question_naswer) values ('" . $question[$x] . "','" . $answer[$x] . "')"; $x++; }
something along line of:
$q_a = array( 'what 2 + 6' => '8', 'what color sky?' => 'blue', // etc ... } $i = 0; $cnt = count(array_keys($q_a)); $sql = 'insert administrator_instructions (question, question_answer) values '; foreach ($q_a $q => $a) { $sql .= sprintf("('%s', '%s')", mysql_real_escape_string($q), mysql_real_escape_string($a)); $i++; if($i < $cnt) $sql .= ", "; }
Comments
Post a Comment