2

i am new to php and i am trying to build a php quiz, that read questions and answers from the mysql db. so far i have the following code:

require_once("scripts/connect_db.php");
session_start();
$_SESSION['Qid'] = 1;
if (!isset($_POST['answer'])){
    $Qid = $_SESSION['Qid'];
    echo $Qid;
    $stmt = $dbh->prepare("Select * FROM qtable WHERE id = ?");
    $stmt->bindParam(1, $Qid);
    $stmt->execute();
    $result = $stmt->fetchObject(); 
    echo $result->Q . "<br />";
    $stmt = $dbh->prepare("SELECT * FROM atable WHERE Qid = ? ORDER BY rand()");
    $stmt->bindParam(1, $Qid);
    $stmt->execute();
    $result = $stmt->fetchAll();
    echo "<form action=\"test1.php\" method=\"post\">";
    foreach ( $result as $row ) {
        if ( $row['correct'] == 1 ){
            echo "<input type=\"radio\" name=\"answer\" value=\"1\">" .$row['answer']."<br />";
        } else {
            echo "<input type=\"radio\" name=\"answer\" value=\"0\">" .$row['answer']."<br />";
        }
    }   
    echo "<input type=\"submit\" value=\"next\">";
    echo "</form>";     
}
else
{ 
    $Qid = $_SESSION['Qid'] + 1;
    echo $Qid;
    $stmt = $dbh->prepare("Select * FROM qtable WHERE id = ?");
    $stmt->bindParam(1, $Qid);
    $stmt->execute();
    $result = $stmt->fetchObject(); 
    echo $result->Q . "<br />";
    $stmt = $dbh->prepare("SELECT * FROM atable WHERE Qid = ? ORDER BY rand()");
    $stmt->bindParam(1, $Qid);
    $stmt->execute();
    $result = $stmt->fetchAll();
    echo "<form action=\"test1.php\" method=\"post\">";
    foreach ( $result as $row ) {
        if ( $row['correct'] == 1 ){
            echo "<input type=\"radio\" name=\"answer\" value=\"1\">" .$row['answer']."<br />";
        } else {
            echo "<input type=\"radio\" name=\"answer\" value=\"0\">" .$row['answer']."<br />";
        }
    }   
    echo "<input type=\"submit\" value=\"next\">";
    echo "</form>";
}

my idea is to increment $_SESSION['Qid'] by 1 every time i press submit so i can loop through the database, but so far i can only go up to the second row. Can anybody help or point me into the right direction on how to do this with out Jquery or AJAX. Thanks

1
  • 2
    Your problems are twofold (at least). Jim and MattMoehring should team up, because they both describe a part of the problem. Kudos for using prepared statements, though. Most beginners for some reason grab the old api. Commented Jul 14, 2014 at 19:32

3 Answers 3

3

You keep resetting $_SESSION['Qid'] = 1;

Try

require_once("scripts/connect_db.php");
session_start();
//$_SESSION['Qid'] = 1;

if(!isset($_SESSION['Qid']) {
    $_SESSION['Qid'] = 1;
}

then near the end, add the following to save the counter back to SESSION:

$_SESSION['Qid'] = $Qid;
Sign up to request clarification or add additional context in comments.

1 Comment

@GolezTrol He beat me, too. We were probably editing the question while he was typing his answer :)
2

Your Problem lies in the line...

$Qid = $_SESSION['Qid'] + 1;

The issue is that you never set the actual $_SESSION variable to anything other than 1 right after the session_start(); If you want the value to persist after incrementing, you should do:

$_SESSION['Qid'] = $Qid

Before the script ends.

Comments

0

Thanks, i was able to find the answer with your help, here is the code:

session_start();
if (!isset($_POST['answer'])){  
$_SESSION['Qid'] = 1;
echo "im in the if";
$stmt = $dbh->prepare("Select * FROM qtable WHERE id = ?");
$stmt->bindParam(1, $_SESSION['Qid']);
$stmt->execute();
$result = $stmt->fetchObject(); 
echo $result->Q . "<br />";
$stmt = $dbh->prepare("SELECT * FROM atable WHERE Qid = ? ORDER BY rand()");
$stmt->bindParam(1, $_SESSION['Qid']);
$stmt->execute();
$result = $stmt->fetchAll();
echo "<form action=\"test1.php\" method=\"post\">";
foreach ( $result as $row ) {
    if ( $row['correct'] == 1 ){
        echo "<input type=\"radio\" name=\"answer\" value=\"1\">" .$row['answer']."<br />";
    } else {
        echo "<input type=\"radio\" name=\"answer\" value=\"0\">" .$row['answer']."<br />";
    }
}   
echo "<input type=\"submit\" value=\"next\">";
echo "</form>";   

} else {
    $Qid = $_SESSION['Qid'];
    $_SESSION['Qid'] = $Qid + 1;
    echo "im in the else";
    echo $_SESSION['Qid'];
    $stmt = $dbh->prepare("Select * FROM qtable WHERE id = ?");
    $stmt->bindParam(1, $_SESSION['Qid']);
    $stmt->execute();
    $result = $stmt->fetchObject(); 
    echo $result->Q . "<br />";
    $stmt = $dbh->prepare("SELECT * FROM atable WHERE Qid = ? ORDER BY rand()");
    $stmt->bindParam(1, $_SESSION['Qid']);
    $stmt->execute();
    $result = $stmt->fetchAll();
    echo "<form action=\"test1.php\" method=\"post\">";
    foreach ( $result as $row ) {
        if ( $row['correct'] == 1 ){
            echo "<input type=\"radio\" name=\"answer\" value=\"1\">" .$row['answer']."<br />";
        } else {
            echo "<input type=\"radio\" name=\"answer\" value=\"0\">" .$row['answer']."<br />";
        }
    }   
    echo "<input type=\"submit\" value=\"next\">";
    echo "</form>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.