Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6687423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:17:14+00:00 2026-05-26T05:17:14+00:00

I’m building a quiz web application that dynamically generates from a database on my

  • 0

I’m building a quiz web application that dynamically generates from a database on my server. I’m having trouble passing a value for quiz_id using a hidden input in the form field. Here is the quiz.php script:

<?php
    // Start the session
    require_once('startsession.php');

    // Insert the Page Header
    $page_title = "Quiz Time!";
    require_once('header.php');

    require_once('connectvars.php');

    // Make sure user is logged in
    if (!isset($_SESSION['user_id'])) {
        echo '<p>Please <a href="login.php">log in</a> to access this page.</p>';
        exit();
    }

    // Show navigation menu
    require_once('navmenu.php');

    // Connect to database
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    // Declare $quiz_id
    $quiz_title = $_GET['title'];
    $quiz_id = $_GET['id'];
    // print_r($quiz_title);
    // print_r($quiz_id);

    // Grab list of question_id's for this quiz
    $query = "SELECT question_id FROM question WHERE quiz_id = '" . $quiz_id . "'";
    $data = mysqli_query($dbc, $query);
    $questionIDs = array();
    while ($row = mysqli_fetch_array($data)) {
        array_push($questionIDs, $row['question_id']);
    }


    // Create empty responses in 'quiz_response' table
    foreach ($questionIDs as $questionID) {
        $query = "INSERT INTO quiz_response (user_id, question_id) VALUES ('" . $_SESSION['user_id'] . "', '" . $questionID . "')";
        mysqli_query($dbc, $query);
    }


    // If form is submitted, update choice_id column of quiz_response table
    if (isset($_POST['submit'])) {
        // Inserting choices into the response table
        foreach ($_POST as $choice_id => $choice) {
            $query = "UPDATE quiz_response SET choice_id = '$choice', answer_time=NOW() " .
            "WHERE response_id = '$choice_id'";
            mysqli_query($dbc, $query);
        }

        // Update the 'is_correct' column
        // Pull all is_correct data from question_choice table relating to specific response_id
        $total_Qs = 0;
        $correct_As = 0;
        foreach ($_POST as $choice_id => $choice) {
            $query = "SELECT qr.response_id, qr.choice_id, qc.is_correct " . 
            "FROM quiz_response AS qr " . 
            "INNER JOIN question_choice AS qc USING (choice_id) " . 
            "WHERE response_id = '$choice_id'"; 
            $data=mysqli_query($dbc, $query);
            // Update is_correct column in quiz_response table
            while ($row = mysqli_fetch_array($data, MYSQLI_ASSOC)) {
                $total_Qs ++;
                if ($row['is_correct'] == 1) {
                    $query2 = "UPDATE quiz_response SET is_correct = '1' " . 
                    "WHERE response_id = '$row[response_id]'";
                    mysqli_query($dbc, $query2);
                    $correct_As ++;
                }
            }   
        }       

        // Update high_score table with $correct_As
        $quiz_id = $_POST['quiz_id'];
        $query = "INSERT INTO high_score " . 
        "VALUES ('0', '" . $_SESSION['user_id'] . "', '" . $quiz_id . "', '" . $correct_As . "', NOW())";
        mysqli_query($dbc, $query);

        // Display score after storing choices in database
        echo 'You got ' . $correct_As . ' out of ' . $total_Qs . ' correct';
        exit();
        mysqli_close($dbc);
    }



    // Grab the question data from the database to generate the form
    $Q_and_Cs = array();
    foreach ($questionIDs as $questionID) {
        $query = "SELECT qr.response_id AS r_id, qr.question_id, q.question " . 
           "FROM quiz_response AS qr " . 
           "INNER JOIN question AS q USING (question_id) " . 
           "WHERE qr.user_id = '" . $_SESSION['user_id'] . "' " .
           "AND qr.question_id = '" . $questionID . "'";
        $data = mysqli_query($dbc, $query) 
            or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query");
        // Store in $questions array, then push into $Q_and_Cs array
        while ($row = mysqli_fetch_array($data, MYSQL_ASSOC)) {
            print_r($row);
            $questions = array();
            $questions['r_id'] = $row['r_id'];
            $questions['question_id'] = $row['question_id'];
            $questions['question'] = $row['question'];
            // Pull up the choices for each question
            $query2 = "SELECT choice_id, choice FROM question_choice " .
            "WHERE question_id = '" . $row['question_id'] . "'";
            $data2 = mysqli_query($dbc, $query2);
            while ($row2 = mysqli_fetch_array($data2, MYSQL_NUM)) {
                $questions[] = $row2[0];
                $questions[] = $row2[1];
            }
            array_push($Q_and_Cs, $questions);
        }
    }
    mysqli_close($dbc);


    // Generate the quiz form by looping through the questions array
    echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
    echo '<h2>' . $quiz_title . '</h2>';
    $question_title = $Q_and_Cs[0]['question'];
    echo '<label for="' . $Q_and_Cs[0]['r_id'] . '">' . $Q_and_Cs[0]['question'] . '</label><br />';
    foreach ($Q_and_Cs as $Q_and_C) {
        // Only start a new question if the question changes
        if ($question_title != $Q_and_C['question']) {
            $question_title = $Q_and_C['question'];
            echo '<br /><label for="' . $Q_and_C['r_id'] . '">' . $Q_and_C['question'] . '</label><br />';
        }
        // Display the choices
        // Choice #1
        echo '<input type="radio" id="' . $Q_and_C['r_id'] . '" name="' . $Q_and_C['r_id'] . '" value="' . $Q_and_C[0] . '" />' . $Q_and_C[1] . '<br />';
        // Choice#2
        echo '<input type="radio" id="' . $Q_and_C['r_id'] . '" name="' . $Q_and_C['r_id'] . '" value="' . $Q_and_C[2] . '" />' . $Q_and_C[3] . '<br />';
        // Choice #3
        echo '<input type="radio" id="' . $Q_and_C['r_id'] . '" name="' . $Q_and_C['r_id'] . '" value="' . $Q_and_C[4] . '" />' . $Q_and_C[5] . '<br />';
        // Choice #4
        echo '<input type="radio" id="' . $Q_and_C['r_id'] . '" name="' . $Q_and_C['r_id'] . '" value="' . $Q_and_C[6] . '" />' . $Q_and_C[7] . '<br />';
    }
    echo '<br /><br />';
    echo '<input type="hidden" name="quiz_id" value"'.$quiz_id.'" />';
    echo '<input type="submit" value="Grade Me!" name="submit" />';
    echo '</form>';


    // Insert the page footer
    require_once('footer.php'); 

?>

After the code runs to generate the form I echo the value of $quiz_id and it displays correctly. However, when I try to grab the hidden value using $quiz_id = $_POST['quiz_id']; and echo it’s value it doesn’t work.

So, it leads me to believe that I am not correctly passing this hidden value. Furthermore, the INSERT query where I update the high_score table works for every column except where I am passing the value of $quiz_id. Although it works if I pass it an integer in its place.

My question is am I doing something wrong in the process of sending the hidden variable $quiz_id?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:17:14+00:00Added an answer on May 26, 2026 at 5:17 am

    Change

     echo '<input type="hidden" name="quiz_id" value"'.$quiz_id.'" />';
    

    to

    echo '<input type="hidden" name="quiz_id" value = "'.$quiz_id.'" />';
    

    I think you missed an “=”.

    Edit : Plus you’re using a $_GET for a hidden form field? You should do this instead :

    $quiz_id = $_POST['id'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.