我需要一些帮助来完成我的任务。我想一次向mysql
添加多个问题答案,为此我使用了两个单独的表格
首先,我必须在ques_table
中插入问题,然后从ques_table
中获取ques_id
必须得到ques_id
并且相关答案应插入与其ques_id
相对应的ans_table
请在这方面帮助我。
这是形式
<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);
}
}
希望这对您有所帮助
<?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;
}
?>