如何通过复选框设置正确的答案?

时间:2015-05-06 09:07:20

标签: php html mysql

我有这个程序(是PHP):

<div id="count2">
        <label>Question 1</label>
        <input type="text" name="pregunta1" class="form-control" style="width:300px;" placeholder="Pregunta 1">
        <br>
          <div class="form-group">
            <div class="col-sm-3" id="count">
                <label>Reply 1</label>
              <input type="text" name="respuesta1_1" class="form-control" id="inputEmail3" placeholder="Respuesta 1">
            </div>
            <div class="col-sm-3" id="count">
            <label>Correct?</label>
              <input type="radio" class="form-control" id="inputEmail3" name="correcta1" value="yes">
            </div>
            <div class="col-sm-3" id="count">
                <label>Help 1</label>
              <input type="text" name="ayuda1_1" class="form-control" id="inputEmail3" placeholder="Ayuda 1">
            </div>
          </div>

          <div class="form-group">
            <div class="col-sm-3" id="count">
                <label>Reply 2</label>
              <input type="text" name="respuesta2_1" class="form-control" id="inputEmail3" placeholder="Respuesta 2">
            </div>
            <div class="col-sm-3" id="count">
            <label>Correct?</label>
              <input type="radio" class="form-control" id="inputEmail3" name="correcta1" value="yes">
            </div>
            <div class="col-sm-3" id="count">
                <label>Help 2</label>
              <input type="text" name="ayuda2_1" class="form-control" id="inputEmail3" placeholder="Ayuda 2">
            </div>
          </div>
        </div>

我可以设置dinamically问题和答案,是一种考试,你可以,例如,再添加一个问题,在这个问题中,还有一个答案/正确吗?/ h​​elp

在此之后,我通过MYSQL插入此内容:

$numQuestions = $_GET['p'];
$numResponses = $_GET['r'];

$title= $_POST['titulo'];
$resumen = $_POST['resumen'];
$fecha = date('Y-m-d H:i:s');
$insertExame = $mysqli->query("INSERT INTO examenes(title,resumen,fecha,private,users_idUsers) VALUES('$title','$resumen','$fecha','no','$_SESSION[user]')");


    $numExam = $mysqli->insert_id;
    $question= array();
    $correct = array();
    $reply = array();
    $help = array();

    for ($i=1; $i <= $numQuestions; $i++) {
        $question[$i] = $_POST['question'.$i];
        $insertQuestion = $mysqli->query("INSERT INTO questions(question,privado,exam_idExam,user_idUser) VALUES('$question[$i]','no','$numExame','$_SESSION[user]')");

        $idQuestion = $mysqli->insert_id;

    for ($k=1; $k <= $numResponses; $k++) {
    $reply[$k] = $_POST['reply'.$k.'_'.$i];
    $help[$k] = $_POST['help'.$k.'_'.$i];
    $correct[$i] = $_POST['correct'.$i];

    if($reply[$k] != "" || $reply[$k] != null) {
    $insertaResponse = $mysqli->query("INSERT INTO replies(reply,help,correct,private,exam_idExam,question_idQuestion,user_idUser) VALUES('$reply[$k]','$help[$k]','$correct[$i]','no','$numExam','$idQuestion','$_SESSION[user]')");
    }
}

该节目是西班牙语,所以有些话我不会被翻译,抱歉。

数据库是这样的:

CREATE TABLE IF NOT EXISTS `examns` (
`idExam` int(11) NOT NULL,
  `title` varchar(85) NOT NULL,
  `resumen` text NOT NULL,
  `fecha` datetime NOT NULL,
  `private` varchar(10) NOT NULL,
  `token` varchar(64) NOT NULL,
  `user_idUser` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `questions` (
`idQuestion` int(11) NOT NULL,
  `question` varchar(64) NOT NULL,
  `private` varchar(10) NOT NULL,
  `exam_idExam` int(11) NOT NULL,
  `user_idUser` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `replies` (
`idReply` int(11) NOT NULL,
  `reply` text NOT NULL,
  `help` text NOT NULL,
  `correct` varchar(10) NOT NULL,
  `private` varchar(10) NOT NULL,
  `exam_idExam` int(11) NOT NULL,
  `question_idQuestion` int(11) NOT NULL,
  `user_idUser` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=latin1;

我想这样做:我想设置表格回复,正确答案。例如,如果我有:

问题1:你的fauvorite颜色是什么?

回复1:红色正确?:是(已检查)帮助1:“”

回复2:蓝色正确吗?否(未选中)帮助2:不是蓝色,因为......

而不是我想插入mysql进行两次新插入,第一个复选框有“yes”,第二个有“no”。

我希望你明白这一点。

由于

1 个答案:

答案 0 :(得分:0)

您需要收集多个变量中的所有复选框值,然后您可以使用以下语法通过单个插入查询插入所有这些记录:

INSERT INTO example
  (example_id, name, value, other_value)
VALUES
  (1, 'Name 1', '$var1', 'Other 1'),
  (2, 'Name 2', '$var2', 'Other 2'),
  (3, 'Name 3', '$var3', 'Other 3'),
  (4, 'Name 4', '$var4', 'Other 4');