我的任务需要一些帮助。我想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);
}
}