<?php include  "database.php";?> 
             <?php session_start();?> 
             <?php

            // Getting total rows there in table
            $query="SELECT * FROM questions";
            $results=$mysqli->query($query) or die($mysqli->error.__LINE__);
            $total=$results->num_rows;
            $row=$results->fetch_assoc();
            //Reading the rows next by next and vice versa

            $offset=$row["question_number"];
            // when button next is clicked it will fetch the next row
            if(isset($_POST["NEXT"])){
                $offset++;
            }
            // if previous is clicked the previous row will be displayed
            elseif(isset($_POST["Previous"])){
                if($offset>1){
                    $offset--;
                }
            }
            $sql="select * from questions where question_number=$offset";
            $result=$mysqli->query($sql);

            if($result->num_rows > 0){

                while($row=$result->fetch_assoc()){

                    ?> <!DOCTYPE html> 
                    <html> 
                    <head> 
                    <meta charset="utf-8"> 
                    <title> PHP Quizzer</title> 
                    <link rel="stylesheet" href="css/style.css" type="text/css"/> 
                    <link rel="stylesheet" href="style.css" type="text/css"/> 
                    </head>
                    <body>
                    <header> 
                    <div class="container">
                    <h1>PHP Quizzer </h1> 
                    </div> 
                    </header> 
                    <main> 
                    <div class="container"> 
                    <div class="current">Question <?php echo $row["question_number"];?> of <?php echo $total;?> 
                    </div> 
                    <p class="question"> 
                    <?php echo $row["question_number"].".".$row['text'];?> </p>
                    <form method="post" action="question2.php"> 
                    <ul class="choices"> 
                    <li><input name="choice" type="radio" value="A"/><?php echo $row['A'];?> 
                    <li><input name="choice" type="radio" value="B"/><?php echo $row['B'];?> 
                    <li><input name="choice" type="radio" value="C"/><?php echo $row['C'];?> 
                    <li><input name="choice" type="radio" value="D"/><?php echo $row['D'];?> 
                    </ul>
                    <input type="submit" value="submit"/> 
                    <input type="submit" value="Previous" name="Previous"/> 
                    <input type="submit" value="NEXT" name="NEXT"/> 
                    <input type="hidden"  name="offset" value=""/> 
                    </form> 
                    </div> 
                    </main> 
                    <footer> 
                    <div class="container">
                            Copyright ©2020, PHP Quizzer.
                        </div> </footer> </body> </html> <?php
                }
            }
            $mysqli->close();
            ?>

Dani AI

Generated

The page always re-initializes the “current question” to the first row on every request, so clicking NEXT only moves you to question 2 and any further clicks go back to 2. 's script fetches the first row on each load and never persists the current question index between POSTs (the hidden offset field is empty), so the server has no memory of which question was being shown.

Fix (short checklist)

  • Persist the current question index across requests: either carry it in a hidden input, in the session, or in the URL.
  • On POST read that persisted value (use intval() for safety), then apply NEXT/PREV adjustments and clamp the value to 1..$total.
  • Fetch the desired question by ORDER BY + LIMIT/OFFSET (safer when question_number has gaps) or by primary id if you want direct lookups.
  • Use prepared statements and always escape HTML output.

Minimal example (pattern to use)

// get total
$total = (int)$mysqli->query("SELECT COUNT(*) AS c FROM questions")->fetch_assoc()['c'];

// get persisted index (default to 1)
$index = isset($_POST['offset']) ? intval($_POST['offset']) : 1;

// action buttons: use a single name/value to detect which was clicked
$action = $_POST['action'] ?? '';
if ($action === 'next' && $index < $total) $index++;
elseif ($action === 'prev' && $index > 1) $index--;

// fetch the Nth question (ORDER BY question_number)
$stmt = $mysqli->prepare("SELECT question_number,text,A,B,C,D FROM questions ORDER BY question_number LIMIT 1 OFFSET ?");
$off = $index - 1;
$stmt->bind_param('i',$off);
$stmt->execute();
$question = $stmt->get_result()->fetch_assoc();

In the form include the current index and use named action buttons:

<input type="hidden" name="offset" value="<?php echo $index; ?>">
<button type="submit" name="action" value="prev">Previous</button>
<button type="submit" name="action" value="next">Next</button>

Notes: 's suggestion to loop all rows is useful when you want to list every question, but for a one-at-a-time quiz you should persist a single index as above. Also verify your form posts to the script that reads the offset (posting to a different file without the same logic will produce the behavior you saw).

  // Get all rows that exist in table
    $query="SELECT * FROM questions";
    $results=$mysqli->query($query) or die($mysqli->error.__LINE__);
    $total=$results->num_rows;

    // If one or more rows exist
   if($result->num_rows > 0){

           // Loop through each row of questions, storing it in the $row variable for each loop iteration
            while($row=$result->fetch_assoc()){

            // Print out the question
            echo $row["question_number"].".".$row['text'];
            }

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.