skip to Main Content

I have a problem-saving data into the same line in phpmyAdmin using php.

I was expecting a result which the quiz grade will save into the account database because when different account logged in the result will appear to be different according to every account.

as I created a registration page which able to store user info in phpmyadmin, therefore I would like to save the result under the same line.

I will attach images and my codes below to be more specific.
Thank You.

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

Answers


  1. What you are looking for is the update statement in MySQL.

    Assuming you have a primary key user_id in your table:

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

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

    Login or Signup to reply.
  2. Insert query will insert new row in the table . but you need to update grade column for existing row (i.e existing user) . so you need to use update query with where clause instead of insert

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

    Note :

    1st : Try to use prepared statement to avoid sql injection .

    2nd : $user_Id currently logged in user id

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search