我的任务需要一些帮助。我想一次为mysql
添加多个问题答案,为此我使用了两个单独的表
首先,我必须在ques_table
中插入问题,然后从ques_id
获取ques_table
必须获取ques_id
,相关答案应插入与其ans_table
对应的ques_id
请在这方面帮助我。
这是表格
<input type='text' name='question[]' />
<input type='text' name='answer[]' />
<input type='text' name='answer[]' />
<input type='text' name='answer[]' />
<input type='text' name='question[]' />
<input type='text' name='answer[]' />
<input type='text' name='answer[]' />
<input type='text' name='answer[]' />
<input type='text' name='question[]' />
<input type='text' name='answer[]' />
<input type='text' name='answer[]' />
<input type='text' name='answer[]' />
这里是php代码
$questions = array();
$cat_id = $_POST['parent_cat'];
$sub_cat_id = $_POST['child_cat'];
$questions = $_POST['question'];
foreach ($questions as $ques) {
$q = $ques;
$SQL = "INSERT INTO question(`cat_id`, `sub_cat_id`, `questions`) VALUES ('$cat_id', '$sub_cat_id', '$q') ";
$Q = mysql_query($SQL);
if ($Q) {
$answer = $_POST['answer'];
$SQL = mysql_query("SELECT MAX(id) AS `id` FROM question");
$row = mysql_fetch_assoc($SQL);
$ques_id = $row['id'];
foreach ($answer as $ans) {
$a = $ans;
$SQL1 = "INSERT INTO answers(`question_id`, `answer`) VALUES ('$ques_id', '$a') ";
$Q1 = mysql_query($SQL1);
}
}
答案 0 :(得分:0)
希望这会对你有所帮助
<?php
$pos=1;
$questions=get_all_questions();
while($ques=mysql_fetch_array($questions))
{
$answers=get_all_answers($ques["id"]);
$ans=mysql_fetch_array($answers);
$id=$ques["id"];
echo "<table id=\"page\">";
echo "<tr><td>".$pos."."." ".$ques["content"]."</td></tr>";
echo "<tr><td width=\"200px\">".$ans["op1"]."</td><td width=\"200px\">".$ans["op2"]."</td></tr>";
echo "<tr><td></td></tr>";
echo "<tr><td width=\"200px\">".$ans["op3"]."</td><td width=\"200px\">".$ans["op4"]."</td>";
echo "</table>";
$pos++;
}
?>
<?php
function get_all_questions()
{
global $connection;
$query="SELECT *
FROM `questions`
LIMIT 0 , 30";
$questions=mysql_query($query,$connection);
confirm_query($questions);
return $questions;
}
function get_all_answers($ques_id)
{
global $connection;
$query="SELECT *
FROM `op_answers`
WHERE `ques_id` ={$ques_id}
LIMIT 0 , 30";
$answers=mysql_query($query,$connection);
confirm_query($answers);
return $answers;
}
?>