将正确的答案设为1?

时间:2016-07-03 10:02:32

标签: php mysql

我正在查询中将答案插入答案表中,我需要设置正确的答案等于1,并且错误的答案等于0,因为我使用单选按钮将值分配到回答,但每次当我运行查询时,即使我点击了不同的单选按钮,第一个答案总是得到值1

   <?php 


require_once("scripts/connect_db.php");
if(isset($_POST['submit'])){

$question = $_POST['desc'];
$is_correct = $_POST['iscorrect'];

$answers = array($_POST['answer1'],$_POST['answer2']);
var_dump($is_correct);
var_dump($answers);
var_dump($_POST);

    $sql = mysqli_query($con,"INSERT INTO questions (question) VALUES ('".$question."')") or die(mysqli_error($con));
    $lastId = mysqli_insert_id($con);
    mysqli_query($con,"UPDATE questions SET question_id='".$lastId."' WHERE id='".$lastId."' LIMIT 1") or die(mysqli_error($con));  


    if($sql){

        foreach($answers as $answer => $value){

                if($value != ""){

                    if($is_correct == "iscorrect" ){
                        $is_correct = 1 ;

                    }else {

                        $is_correct = 0 ;
                    }
                    $query = mysqli_query($con,"INSERT INTO answers (question_id, answer, correct) VALUES ('".$lastId."', '".$value."', '".$is_correct."')") or die(mysqli_error($con));

                    if($query ){
                        continue;
                    }else{
                        dir("Error".mysqli_error($con));
                    }

                }



        }// Foreach
    }





    }// End of submit

?>



<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>اضافة أسئلة</title>

<style type="text/css">
    body{
        direction:RTL;
    }
    .content {

        margin-top: 50px;
        margin-left: auto;
        margin-right: auto;
        width: 780px;
        border: #333 1px solid;
        border-radius: 12px;
        -moz-border-radius: 12px;
        padding: 12px;

    }
</style>
</head>

<body>


 <div class="content" id="mc">
    <h3>اسئلة اختيارات تعددية</h3>
    <form action="questions.php" name="addMcQuestion" method="post">
      <strong>الحقل المخصص لكتابة السؤال</strong>
        <br />
        <textarea  name="desc" style="width:400px;height:95px;"></textarea>
        <br />
      <br />
    <strong>الخيار الاول</strong>
        <br />
        <input type="text"  name="answer1">&nbsp;
          <label style="cursor:pointer; color:#06F;">
          <input type="radio" name="iscorrect" value="iscorrect" />الاجابة الصححيحة ؟
        </label>
      <br />
    <br />
    <strong>الخيار الثانى</strong>
    <br />
        <input type="text"  name="answer2">&nbsp;
          <label style="cursor:pointer; color:#06F;">
          <input type="radio" name="iscorrect" value="iscorrect" />الاجابة الصححيحة ؟
        </label>
      <br />
    <br />

    <input type="submit" name="submit" value="اضافة سؤال">
    </form>
 </div>
</body>
</html>






CREATE TABLE IF NOT EXISTS `questions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `question_id` int(11) NOT NULL,
  `question` varchar(255) NOT NULL,
  `type` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;


CREATE TABLE IF NOT EXISTS `answers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `question_id` int(11) NOT NULL,
  `answer` varchar(255) NOT NULL,
  `correct` enum('0','1') NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

2 个答案:

答案 0 :(得分:0)

试试这个

if($sql){

    foreach($answers as $answer => $value){

            if($value != ""){

                if($is_correct == "iscorrect" ){
                    $is_correct = "1" ;

                }else {

                    $is_correct = "0" ;
                }
                $query = mysqli_query($con,"INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$value', '$is_correct')") or die(mysqli_error($con));

                if($query ){
                    continue;
                }else{
                    dir("Error".mysqli_error($con));
                }

            }
    }
}

答案 1 :(得分:0)

我做了一些mods,看看这有帮助..

<?php
require_once("scripts/connect_db.php");
if(isset($_POST['submit'])){

$question = $_POST['desc'];
$is_correct = $_POST['iscorrect'];

$answer1 = $_POST['answer1']
$answer2 = $_POST['answer2']);
var_dump($is_correct);
var_dump($answers);
var_dump($_POST);

$sql = mysqli_query($con,"INSERT INTO questions (question) VALUES ('".$question."')") or die(mysqli_error($con));
$lastId = mysqli_insert_id($con);
mysqli_query($con,"UPDATE questions SET question_id='".$lastId."' WHERE id='".$lastId."' LIMIT 1") or die(mysqli_error($con));  

                $is_correct = 1 ;
                $is_wrong = 0;
                if($is_correct == "iscorrect1" ){
                $query = mysqli_query($con,"INSERT INTO answers (question_id, answer, correct) VALUES ('".$lastId."', '".$answer1."', '".$is_correct."'),('".$lastId."', '".$answer2."', '".$is_wrong."')") or die(mysqli_error($con));
                }
                else {
                $query = mysqli_query($con,"INSERT INTO answers (question_id, answer, correct) VALUES ('".$lastId."', '".$answer1."', '".$is_wrong."'),('".$lastId."', '".$answer2."', '".$is_correct."')") or die(mysqli_error($con));
                }
                if($query ){
                    continue;
                }else{
                    dir("Error".mysqli_error($con));
                }


}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>اضافة أسئلة</title>
    <style type="text/css">
        body{
        direction:RTL;
        }
        .content {
        margin-top: 50px;
        margin-left: auto;
        margin-right: auto;
        width: 780px;
        border: #333 1px solid;
        border-radius: 12px;
        -moz-border-radius: 12px;
        padding: 12px;
        }
    </style>
</head>
<body>
    <div class="content" id="mc">
        <h3>اسئلة اختيارات تعددية</h3>
        <form action="questions.php" name="addMcQuestion" method="post">
            <strong>الحقل المخصص لكتابة السؤال</strong>
            <br />
            <textarea  name="desc" style="width:400px;height:95px;">        </textarea>
            <br />
            <br />
            <strong>الخيار الاول</strong>
            <br />
            <input type="text"  name="answer1">&nbsp;
            <label style="cursor:pointer; color:#06F;">
            <input type="radio" name="iscorrect" value="iscorrect1" />الاجابة    الصححيحة ؟
            </label>
            <br />
            <br />
            <strong>الخيار الثانى</strong>
            <br />
            <input type="text"  name="answer2">&nbsp;
            <label style="cursor:pointer; color:#06F;">
            <input type="radio" name="iscorrect" value="iscorrect2" />الاجابة الصححيحة ؟
            </label>
            <br />
            <br />
            <input type="submit" name="submit" value="اضافة سؤال">
        </form>
    </div>
</body>
</html>