Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • update multiple values using php and mysql?

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 542
    Answer it


    i need to update multiple choices for question but every time i run the query it's only update the last choice only also i need to update the value of the correct answer which is (0 or 1 ) which the best using input text or radio button

    <?php require ("scripts/connect_db.php");   ?>
    <?php session_start() ?>
    <?php
    
    
    
    
    if (isset($_POST['submit'])) {
    
        $question_id = mysqli_real_escape_string ($con,strip_tags($_POST['question_id']));
    
        $question = mysqli_real_escape_string ($con,strip_tags($_POST['desc']));
    
        $sql1 = mysqli_query($con,"UPDATE `questions` SET `question`='".$question."'
                WHERE   questions.question_id = 1") or die(mysqli_error($con)); 
    
    
    
        $isCorrect  =  $_POST['iscorrect'];
        $answers = mysqli_real_escape_string ($con,strip_tags(count($_POST['answers'])));
        $answer_id =  mysqli_real_escape_string ($con,strip_tags($_POST['question_id']));
    
        for($i=0;$i<$answers;$i++) {
        mysqli_query($con,"UPDATE answers set  answer='" . $_POST["answers"][$i] . "', correct='" . $_POST["iscorrect"][$i] . "' 
        WHERE question_id = answers.question_id  AND  question_id = '".$question_id."'  ");
        }
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";  
    }else{
    
        $question_id = $_SESSION['question_id'];
    
         echo "   $question_id "; 
    
    
    
        $ques = mysqli_query ($con,"SELECT * FROM `questions` where questions.question_id= ".$question_id ." ") or die (mysqli_error($con));
    
        while ($row1 =  mysqli_fetch_assoc($ques)){
            $question_id = $row1 ['question_id'];
            $question = $row1 ['question'];
    
    
    
    
    }
    
    ?>
    <!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;
    
        }
        textarea{
            width:400px;
            height:95px;
        }
    </style>
    </head>
    
    <body>
    
    
     <div class="content" id="mc">
        <h3>  </h3>
        <form action="#" name="addMcQuestion" method="post">
          <strong>   </strong>
            <br />
            <textarea id="mcdesc" name="desc" ><?php echo $question;  ?></textarea>
            <br />
          <br />
          <?php
           $count = count(isset($_POST['answers']));
            for($answers = 0 ; $answers<$count ; $answers++){
                   $query = mysqli_query($con,"SELECT * FROM answers WHERE answers.question_id = '".$question_id [$answers] ."' ") or die (mysqli_error($con));
            while($rows[$answers] = mysqli_fetch_assoc($query)){
          ?>
          <p>
        <strong> </strong>
    
            <input type="text"  name="answers[]" value="<?php echo $rows[$answers]['answer'];  ?>" />
              <label>
              <input type="text" name="iscorrect[]" value="<?php echo $rows[$answers]['correct'];  ?>" />  
            </label>
         </p>  
    
       <input type="hidden" name = "answer_id[]" value="<?php echo  $rows[$answers]['id'];  ?>" />
    
       <?php
            }
            ?>
    
    
            <?php
            } 
            }
    
    
       ?>
        <input type="hidden" name = "question_id" value="<?php echo $question_id; ?>" />
    
    
        <input type="submit" name="submit" value=" ">
        </form>
     </div>
    </body>
    </html>

    this my database structure

    CREATE TABLE `answers` (
      `id` int(11) NOT NULL,
      `question_id` int(11) NOT NULL,
      `answer` varchar(255) NOT NULL,
      `correct` enum('0','1') NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    --
    -- Dumping data for table `answers`
    --
    
    INSERT INTO `answers` (`id`, `question_id`, `answer`, `correct`) VALUES
    (1, 1, 'java', '1'),
    (2, 1, 'C++', '1'),
    (3, 1, 'C#', '1'),
    (4, 1, 'php', '1');
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `questions`
    --
    
    CREATE TABLE `questions` (
      `id` int(11) NOT NULL,
      `question_id` int(11) NOT NULL,
      `question` varchar(255) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    --
    -- Dumping data for table `questions`
    --
    
    INSERT INTO `questions` (`id`, `question_id`, `question`) VALUES
    (1, 1, 'programming languange ?');
    
    -- 

     

 2 Answer(s)

  • First, to get answer for multi-choice question, you need to have either check box, radio button, single select or multi select, but I didn't see you have any of these format in your codes.
    Second, no matter which format you would use, you need to code (a loop), either in your main or function, to get the value(s) associated with the answer(s) from user.
    Last but not least, recommend to use PDO instead mysqli.
    Will post example if request.
    Good luck ;)
     
  • I just go through your code and i can see a form for questions and answers. you are using for loop to display the answers but i am unable to understand the code flow. What is working of this line? $count = count($_POST['answer']);

    Whenever page load first time then there should not be a post request. If there is no post request then you can not display the answers. Kindly clear the code flow.

    Thank You

    Deepak Verma

    Sr. Software Engineer

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: