简体   繁体   English

单个用户的UPDATE数据库/所有用户的数据库更新

[英]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. 我有一个使用mysqli和php制作的applet,人们不得不回答来自数据库表的问题。 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"). 一种用于问题和答案,另一种用于记录用户以表格形式输入的内容(用户单击“开始测验”后,用户ID由auto_increment生成)。

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. 我一切正常,但是唯一的问题是,一旦用户回答了一个问题,第一个表(有一个名为“已解决”的tinyint(1)的列,该列从0变为1,具体取决于用户是否解决了该问题)为所有用户更新其已解决的列。

I want to make it so that it only updates for that certain id, but I have no idea how to. 我想要使​​其仅针对该特定ID进行更新,但是我不知道如何更新。 This is the following code for my php. 这是我的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) 这些是我的表:1.问题表(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)

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. 您还需要使用会话处理(跟踪用户在问题之间的跟踪,但这不在此范围之内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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