Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • set the correct answers equal to 1?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 496
    Answer it

    i'm working in query to insert answer into answers table and i need to set to correct answer equal to 1 and the incorrect answer equal to 0 for that i'm using radio buttons to assigned value into the answer but every time when i run the query only the first answer always gets the value of 1 even if i clicked different radio button

      <?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 ;

     

 1 Answer(s)

  • Hi Nasiya,

    I have checked your code , the reason behind your problem that your first answer always gets the value 1, even if you clicked the second radio button is that you have given same value "iscorrect" to both the radio buttons, and you have assigned the value of $_POST['iscorrect'] to a variable $is_correct, no matter which radio button you have selected, $is_correct always have value "iscorrect".

    so when you will check the condition 'if($is_correct == "iscorrect")', condition will always true and your first answer always gets 1.

    So you need to change the value of your radio buttons, you should give different values to both radio buttons like iscorrect1 and iscorrect2. There is no need of foreach loop and also the condition "if($is_correct == "iscorrect" )" is not true.

    The correct condition is:

                                       if($answers[0] != "")
                                            {
                                                if($is&#95;correct == "iscorrect1" )
                                                {
                                                    $is&#95;correct = 1 ;
                                                }else {
                                                    $is&#95;correct = 0 ;
                                                }
    
    
                                           $ans&#95;query = mysqli&#95;query($con,"INSERT INTO answers (question&#95;id, answer, correct) VALUES ('".$lastId."', '".$answers[0]."', '".$is&#95;correct."')") or die(mysqli&#95;error($con));
    
    
                                            }
    
                                      if($answers[1] != "")
                                            {
                                                if($is&#95;correct == "iscorrect2" )
                                                {
                                                    $is&#95;correct = 1 ;
                                                }else {
                                                    $is&#95;correct = 0 ;
                                                }
    
                                           $ans&#95;query = mysqli&#95;query($con,"INSERT INTO answers (question&#95;id, answer, correct) VALUES ('".$lastId."', '".$answers[1]."', '".$is&#95;correct."')") or die(mysqli&#95;error($con));
    
    
                                            }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: