来自Mysql数据库的PHP POST循环

时间:2014-07-14 19:24:02

标签: php mysql

我是php的新手,我正在尝试构建一个php测验,从mysql db中读取问题和答案。 到目前为止,我有以下代码:

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>";
}

我的想法是每次按下提交时将$ _SESSION ['Qid']增加1,这样我就可以遍历数据库,但到目前为止我只能进入第二行。任何人都可以通过Jquery或AJAX帮助或指出我如何做到这一点的正确方向。 感谢

3 个答案:

答案 0 :(得分:3)

您不断重置$_SESSION['Qid'] = 1;

尝试

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

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

然后接近结尾,添加以下内容将计数器保存回SESSION:

$_SESSION['Qid'] = $Qid;

答案 1 :(得分:2)

你的问题在于......

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

问题是你永远不会在session_start()之后将实际的$ _SESSION变量设置为1以外的任何值。如果希望在递增后值保持不变,则应执行以下操作:

$_SESSION['Qid'] = $Qid

在脚本结束之前。

答案 2 :(得分:0)

谢谢,我能在你的帮助下找到答案,这是代码:

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>";
}