PHP将数据保存到phpmyadmin中的同一行

时间:2017-06-30 08:59:23

标签: php mysql

我使用php在phpmyAdmin中将问题保存到同一行。

我期待测验成绩将保存到帐户数据库中的结果,因为根据每个帐户登录结果的不同帐户似乎会有所不同。

因为我创建了一个能够在phpmyadmin中存储用户信息的注册页面,所以我想将结果保存在同一行。

我会在下面附上图片和我的代码,以便更具体。 谢谢。

    

    <?php    
    $con = mysqli_connect('127.0.0.1', 'root', '');

  if(!$con)
  {
      echo 'Not connected to server';    
  }
  if (!mysqli_select_db($con, 'accounts'))
  {
      echo 'Database not selected';
  }

        $answer1 = $_POST['question-1-answers'];
        $answer2 = $_POST['question-2-answers'];
        $answer3 = $_POST['question-3-answers'];
        $answer4 = $_POST['question-4-answers'];
        $answer5 = $_POST['question-5-answers'];

        $totalCorrect = 0;

        if ($answer1 == "B") { $totalCorrect++; }
        if ($answer2 == "A") { $totalCorrect++; }
        if ($answer3 == "C") { $totalCorrect++; }
        if ($answer4 == "D") { $totalCorrect++; }
        if ($answer5) { $totalCorrect++; }

        if ($totalCorrect < 3) {
        $grade = "failed";
        } else {
        $grade = "pass";
        }
        $sql = "INSERT INTO users (grade) VALUES ('$grade')";

        if(!mysqli_query($con, $sql))
        {
        echo 'Not inserted';
        }
        else {
            echo 'Inserted';
        }   
        echo "<div id='results'>$totalCorrect / 5 correct</div>";
        echo "<div id='results'>You have $grade the assesment </div>";       
    ?>

    

    <form action="grade.php" method="post" id="quiz">

        <ol>

            <li>

                <h3>CSS Stands for...</h3>

                <div>
                    <input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
                    <label for="question-1-answers-A">A) Computer Styled Sections </label>
                </div>

                <div>
                    <input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
                    <label for="question-1-answers-B">B) Cascading Style Sheets</label>
                </div>

                <div>
                    <input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
                    <label for="question-1-answers-C">C) Crazy Solid Shapes</label>
                </div>

                <div>
                    <input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
                    <label for="question-1-answers-D">D) None of the above</label>
                </div>

            </li>

            <li>

                <h3>Internet Explorer 6 was released in...</h3>

                <div>
                    <input type="radio" name="question-2-answers" id="question-2-answers-A" value="A" />
                    <label for="question-2-answers-A">A) 2001</label>
                </div>

                <div>
                    <input type="radio" name="question-2-answers" id="question-2-answers-B" value="B" />
                    <label for="question-2-answers-B">B) 1998</label>
                </div>

                <div>
                    <input type="radio" name="question-2-answers" id="question-2-answers-C" value="C" />
                    <label for="question-2-answers-C">C) 2006</label>
                </div>

                <div>
                    <input type="radio" name="question-2-answers" id="question-2-answers-D" value="D" />
                    <label for="question-2-answers-D">D) 2003</label>
                </div>

            </li>

            <li>

                <h3>SEO Stand for...</h3>

                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-A" value="A" />
                    <label for="question-3-answers-A">A) Secret Enterprise Organizations</label>
                </div>

                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-B" value="B" />
                    <label for="question-3-answers-B">B) Special Endowment Opportunity</label>
                </div>

                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-C" value="C" />
                    <label for="question-3-answers-C">C) Search Engine Optimization</label>
                </div>

                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-D" value="D" />
                    <label for="question-3-answers-D">D) Seals End Olives</label>
                </div>

            </li>

            <li>

                <h3>A 404 Error...</h3>

                <div>
                    <input type="radio" name="question-4-answers" id="question-4-answers-A" value="A" />
                    <label for="question-4-answers-A">A) is an HTTP Status Code meaning Page Not Found</label>
                </div>

                <div>
                    <input type="radio" name="question-4-answers" id="question-4-answers-B" value="B" />
                    <label for="question-4-answers-B">B) is a good excuse for a clever design</label>
                </div>

                <div>
                    <input type="radio" name="question-4-answers" id="question-4-answers-C" value="C" />
                    <label for="question-4-answers-C">C) should be monitored for in web analytics</label>
                </div>

                <div>
                    <input type="radio" name="question-4-answers" id="question-4-answers-D" value="D" />
                    <label for="question-4-answers-D">D) All of the above</label>
                </div>

            </li>

            <li>

                <h3>Your favorite website is</h3>

                <div>
                    <input type="radio" name="question-5-answers" id="question-5-answers-A" value="A" />
                    <label for="question-5-answers-A">A) CSS-Tricks</label>
                </div>

                <div>
                    <input type="radio" name="question-5-answers" id="question-5-answers-B" value="B" />
                    <label for="question-5-answers-B">B) CSS-Tricks</label>
                </div>

                <div>
                    <input type="radio" name="question-5-answers" id="question-5-answers-C" value="C" />
                    <label for="question-5-answers-C">C) CSS-Tricks</label>
                </div>

                <div>
                    <input type="radio" name="question-5-answers" id="question-5-answers-D" value="D" />
                    <label for="question-5-answers-D">D) CSS-Tricks</label>
                </div>
            </li>
        </ol> 
        <input type="submit" value="Submit Quiz" />
    </form>
</div>

image attachment

2 个答案:

答案 0 :(得分:0)

您正在寻找的是MySQL中的update语句。

假设您的表中有一个主键user_id

UPDATE users SET grade = '{$grade}' WHERE user_id = {$userId}

https://dev.mysql.com/doc/refman/5.5/en/update.html

答案 1 :(得分:0)

Insert查询将在表格中插入new行。但您需要更新现有行grade的{​​{1}}列。因此您需要使用(i.e existing user)代替where clause

的更新查询

insert

注意

第一:尝试使用UPDATE users SET grade = '{$grade}' WHERE user_id = {$user_Id} prepared来避免statement sql

第二次injection当前已登录用户ID