-1

我的任务需要一些帮助。我想mysql一次添加多个问题的答案,为此我使用了两个单独的表格

首先我必须插入问题ques_table然后ques_idques_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);
        }           
    }
4

1 回答 1

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;
}
?>
于 2013-10-18T07:16:37.750 回答