简体   繁体   中英

UPDATE database for single user / Database updating for all users

I have an applet made using mysqli and php where people have to answer questions that are from a database table. I have two tables in one database. One is for the questions and the answers while the other one is to record what the users have inputted in the form (user id is generated by auto_increment once someone clicks "Start Quiz").

I have everything working perfectly, but the only problem is that once a user answers a question, the first table (has a column named "solved" tinyint(1) that changes from 0 to 1 depending on whether the person solved it or not) updates its solved column for all the users.

I want to make it so that it only updates for that certain id, but I have no idea how to. This is the following code for my php.

    <?php require("../require/exponents_database_connection.php"); ?>
<center><div style="overflow:hidden; border:solid; border-color:#39AEB9; display:inline-block; width: 500px; height:auto;">
<div style="vertical-align: middle; padding:25px;">

  <?php 



        // 3. Use returned data



              $exponents = "SELECT * FROM q_exponents WHERE solved = 0 ORDER BY id ASC LIMIT 1 ";
              $result_exponents = mysqli_query($connection, $exponents);
              $question = mysqli_fetch_assoc($result_exponents);


              $getinfo = "SELECT * FROM q_exponents";
              $result_getinfo = mysqli_query($connection, $getinfo);

              $totsolved = "SELECT * FROM q_exponents WHERE solved=1";
              $result_totsolved = mysqli_query($connection, $totsolved);

              $totalsolved=mysqli_num_rows($result_totsolved);            
              $totalquestions=mysqli_num_rows($result_getinfo);




  ?> 



              <?php if(!isset($_POST['Submit']) || isset($_POST['Reset'])) { ?>


              <div style="display: inline-block;">
                <p><b> <?php echo "<p style='font-family:sans-serif;'>".$question["question"]."</p>"; ?> </b></p>
                <?php $question_id = (int) $question['id']; ?>
              <div style="float:left;">
                <form action="" method="post">
                <div>            
                  <input type="radio" name="response" value="<?php echo $question["a1"]?>" checked><?php echo "<p style='display:inline; font-family:sans-serif;'>".$question["a1"]."</p>"?>
                  <input type="radio" name="response" value="<?php echo $question["a2"]?>" style="margin-left:50px;" ><?php echo "<p style='display:inline; font-family:sans-serif;'>".$question["a2"]."</p>"?>       
                  <input type="radio" name="response" value="<?php echo $question["a3"]?>" style="margin-left:50px;" ><?php echo "<p style='display:inline; font-family:sans-serif;'>".$question["a3"]."</p>"?>        
                  <input type="radio" name="response" value="<?php echo $question["a4"]?>" style="margin-left:50px;" ><?php echo "<p style='display:inline; font-family:sans-serif;'>".$question["a4"]."</p>"?>
                  <input type="hidden" name="question_id" value="<?php echo $question["id"] ?>" />
                </div>  
                </div>
                  <div class="empty_block"> </div> <br/>
                  <br><input type="Submit" name="Submit" value="Submit" style="width:150px; height: auto;">
                  <div class="empty_block"> </div> 
                </form>
                </div>



                <?php } elseif(isset($_POST['Submit'])) { ?>

                    <?php 
                      $previous_question_id = (int) $_POST['question_id']; //calls for previous id before randomising

                      $exponents = "SELECT * FROM q_exponents WHERE id = $previous_question_id";
                      $result_exponents=mysqli_query($connection, $exponents);
                      $question=mysqli_fetch_array($result_exponents); //fetches database randomised data where id = previous id


                      $response=$_POST['response'];
                      $a = "a".$previous_question_id;


                      $current_id=mysqli_query($connection, "SELECT AUTO_INCREMENT FROM information_schema.tables WHERE TABLE_SCHEMA = 'exponents' AND TABLE_NAME = 'a_exponents'")->fetch_object()->AUTO_INCREMENT;

                      $answers="UPDATE a_exponents SET $a = $response WHERE id = $current_id - 1";
                      $record_answer=mysqli_query($connection, $answers);

                      $check_answer = "SELECT * FROM a_exponents";
                      $result_check_answer = mysqli_query($connection, $check_answer);
                      $score_result = mysqli_fetch_assoc($result_check_answer);

                      $totsolved = "SELECT * FROM q_exponents WHERE solved=1";
                      $result_totsolved = mysqli_query($connection, $totsolved);
                      $totalsolved=mysqli_num_rows($result_totsolved); //counts how many we have solved


                        if($response == $question['correct']){
                          echo "<p style='font-family:sans-serif; color:#83E046;'><b>CORRECT</b></p>"; ?>
                          <p><b> <?php echo "<p style='display: inline; font-family:sans-serif;'>".$question["question"]."</p>";?>

                          <p style='display: inline; font-family:sans-serif;'>, x = </p>

                          <?php echo "<p style='display: inline; font-family:sans-serif;'>".$question["correct"]."</p>";?></b></p>

                          <?php
                          $solved = "UPDATE q_exponents SET solved = 1 WHERE id = $previous_question_id";
                          mysqli_query($connection, $solved); //updates database so that those with previous id are marked as solved
                          $totalsolved = $totalsolved + 1;?>




                      <?php


                      } else {

                            $solved = "UPDATE q_exponents SET solved =0 WHERE id=$previous_question_id"; 
                            mysqli_query($connection, $solved); 


                        echo "<p style='font-family:sans-serif; color:#E04646;'><b>INCORRECT</b></p>"; ?>
                        <p style='display: inline; font-family: sans-serif;'>Your response was: </p>
                        <?php
                        echo $response;
                        ?>
                          <p><b> <?php echo "<p style='display:inline; font-family:sans-serif;'>".$question["question"]."</p>";?>
                          <p style='display: inline; font-family:sans-serif;'>, x = </p>
                          <?php echo "<p style='display:inline; font-family:sans-serif;'>".$question["correct"]."</p>"; ?></b></p>



                      <?php }?>





                        <?php if($totalsolved==$totalquestions){?>

                          <br><form action="" method="post"><input type="submit" name="" value="One More Time" onclick="<?php $solved = "UPDATE q_exponents SET solved = 0"; mysqli_query($connection,$solved);?>"></form>

                        <?php } elseif ($response !== $question['correct']) { ?>
                          <br><form action="" method="get"><input type="submit" name="next" value="Try Again"/></form>

                        <?php } else { ?>

                          <br><form action="" method="get"><input type="submit" name="next" value="Next Question"/></form>

                        <?php }              

                        } ?>





</div>
</div></center>



<?php 
// 4. Release returned data
 mysqli_free_result($result_exponents);

?>


<?php
require("../require/close_connection.php");
?>



<?php 
// 2. Perform database query
  //collects data from database by a resource
// Test if there was a query error

  if (!$result_exponents) {
    die("Database query failed.");
  }

?>

These are my tables: 1. Table for questions (q_exponents)

+----+-----------------------------------------------------------------------------------+-----+----+-----+-----------+---------+--------+
| id | question                                                                          | a1  | a2 | a3  | a4        | correct | solved |
+----+-----------------------------------------------------------------------------------+-----+----+-----+-----------+---------+--------+
|  1 | 2<sup>4</sup> &times; 2<sup>3</sup> = 2<sup>x</sup>                               | 7   | 4  | 12  | 10        | 7       |      1 |
|  2 | 2<sup>6</sup> &times; 2<sup>x</sup> = 2<sup>8</sup>                               | 5   | 2  | 1.5 | 3         | 2       |      1 |
|  3 | <sup>2<sup>12</sup></sup> &frasl; <sub>2<sup>4</sup></sub> = 2<sup>x</sup>        | 15  | 5  | 8   | 6         | 8       |      1 |
|  4 | <sup>2<sup>x</sup></sup> &frasl; <sub>2<sup>5</sup></sub> = 2<sup>3</sup>         | 3   | 48 | 7   | 8         | 8       |      1 |
|  5 | 2<sup>x</sup> &times; 2<sup>2</sup> = 2<sup>5</sup>                               | 3   | 1  | 2.5 | 10        | 3       |      1 |
|  6 | 5<sup>x</sup> &times; 5<sup>7</sup> = 5<sup>25</sup>                              | 1   | 32 | 18  | 175       | 18      |      1 |
|  7 | <sup>103<sup>14</sup></sup> &frasl; <sub>103<sup>15</sup></sub> = 103<sup>x</sup> | -1  | 29 | 1   | No Answer | -1      |      0 |
|  8 | <sup>35<sup>17</sup></sup> &frasl; <sub>35<sup>x</sup></sub> = 35<sup>34</sup>    | 41  | 2  | 0.5 | -17       | -17     |      0 |
|  9 | 103<sup>x</sup> &times; 103<sup>-1</sup> = 103<sup>89</sup>                       | 88  | 89 | 90  | 91        | 90      |      0 |
| 10 | 65<sup>-x</sup> &times; 65<sup>1</sup> = 65<sup>9</sup>                           | -10 | 10 | 8   | -8        | -8      |      0 |
| 11 | <sup>3<sup>3x</sup></sup> &frasl; <sub>3<sup>2x</sup></sub> = 3<sup>4</sup>       | 2   | 3  | 4   | 5         | 4       |      0 |
| 12 | 4<sup>3x</sup> &times; 4<sup>2x</sup> = 4<sup>20</sup>                            | 2   | 3  | 4   | 5         | 4       |      0 |
+----+-----------------------------------------------------------------------------------+-----+----+-----+-----------+---------+--------+
  1. Table for answers (a_exponents)

a_exponents

The link you supplied for your answers table is broken.

Do you have a users table? You need one. Not being able to see what your answers table looks like, I can only suggestion that it look something like this:

+--------+-------------+----------------------------+
|user_id | question_id | answer                     |
+--------+-------------+----------------------------+
|   1    |      1      | their answer               |
|   1    |      2      | their next answer          |
+--------+-------------+----------------------------+

You'll also need to use session handling (to keep track of the user as they go from question to question, but that's outside the scope of this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM