PHP:从旧表单中获取值

时间:2013-12-21 19:34:15

标签: php mysql forms

我正在写一个考试代码。在表格中我称之为生成问题并存储学生答案的方法。

我的问题是,当我将学生答案存储在数据库中时,上一个问题的答案存储在当前学生答案字段中。然后,即使学生回答是正确的,根据我写的代码也总是错误的。

我觉得问题是我将插入查询放在显示问题的显示功能中,但我不知道如何修复它或放置插入查询的位置。

示例:

Q1:学生回答是4    数据库中的学生答案字段存储为null。

Q2:学生回答是9    数据库中的学生答案字段存储为4。

Q3:学生答案是2.
   数据库中的学生答案字段存储为9。

在我称之为方法(1)的形式中,它调用方法(2)和方法(3)。

方法(2):

function display($sec, $level, $operation, $x, $y, $EvalOrExam, $Answers, $stdAnswer, $result)
 {
$examID;
$sql = "SELECT * FROM question WHERE  Qsec='$sec' and QlawID='$operation' ORDER BY RAND() LIMIT 1";
$result1 = mysql_query($sql);
$data = mysql_fetch_array($result1);
$Qbody = $data['Qtext'] . $x . $data['Qtext2'] . $y . $data['Qtext3'];
echo '<div dir=\'rtl\'>' . $Qbody . '</div>';
echo '<br/>';
return $Qbody;
 }

方法(3):

  function storeExamQ($sec, $level,$EvalOrExam, $Answers, $stdAnswer, $result,$Qbody)
  {
$Answers = $Answers + 1;

    if ($Answers == '1')
    {
        $sql = "SELECT MAX(examID) FROM exam WHERE stdID='" . $_SESSION['id'] . "'";
        $query = mysql_query($sql);
        $row = mysql_fetch_row($query);
        if (!$query) echo "Database Error : " . $sql;
        $query = intval($row['0']);
        $_SESSION['examID'] = $query+1;
    }

        $sql1 = "INSERT INTO `exam`(`examID`,`QID`,`stdID`, `examLevel`, `Qbody` , `Qsec` , `stdAnswer` , `correctAnswer` ) VALUES ('" . $_SESSION['examID'] . "','" . $Answers . "','" . $_SESSION['id'] . "','" . $level . "','" . $Qbody . "','" . $sec . "','" . $stdAnswer . "','" . $result . "')";
        $query1 = mysql_query($sql1);
        if (!$query1) echo "Database Error : " . $sql1;
  }

调用内部方法(1):

  function operations($sec, $levelOfEval, $numOfQ, $EvalOrExam)
 {
if (isset($_POST['result']))
{

    echo '<br/>';
    $stdAnswer = $_POST['stdAnswer'];
    $result = intval($_POST['result']);
    if (!isset($_SESSION['correctAnswers']) AND !isset($_SESSION['wrongAnswers']) AND !isset($_SESSION['Answers']))
    {
        $_SESSION['correctAnswers'] = $correctAnswers = 0;
        $_SESSION['wrongAnswers'] = $wrongAnswers = 0;
        $_SESSION['Answers'] = $Answers = 0;
    }
    else
    {
        $correctAnswers = $_SESSION['correctAnswers'];
        $wrongAnswers = $_SESSION['wrongAnswers'];
        $Answers = $_SESSION['Answers'];
    }

    if ($result != $stdAnswer OR $stdAnswer == "")
    {
        $wrongAnswers++;
        $Answers++;
    }
    else if ($result == $stdAnswer)
    {
        $correctAnswers++;
        $Answers++;
    }


    $_SESSION['correctAnswers'] = $correctAnswers;
    $_SESSION['wrongAnswers'] = $wrongAnswers;
    $_SESSION['Answers'] = $Answers;
}

if ($_SESSION['Answers'] < $numOfQ)
{
    echo '<div dir=\'rtl\'>';
    if (isset($_SESSION['correctAnswers']) AND isset($_SESSION['wrongAnswers']) AND isset($_SESSION['Answers']) AND $EvalOrExam == '0')
    {
        echo $correctAnswers;
        echo '<br/>';
        echo $wrongAnswers;
        echo '<br/>';
    }

    echo $Answers + 1;
    echo '<br/>';
    echo '</div>';
    $operation = rand(1, 4); // 1 add, 2 subtract, 3 multiply, 4 divied

     //IF conditions that generate the values of $x and $y

    $Qbody=displayOperations($sec, $levelOfEval, $operation, $x, $y, $EvalOrExam, $Answers, $stdAnswer, $result);
            if ($EvalOrExam == 1)
    {
storeExamQ($sec, $level,$EvalOrExam, $Answers, $stdAnswer, $result,$Qbody);
}
    echo '<br/>';
    return $result;
}
else
{
    if ($EvalOrExam == 0)
    {
        echo '<br/>';
        echo $correctAnswers;
        echo '<br/>';
        echo $wrongAnswers;
        echo '<br/>';
        /*echo "Answers:";
        echo $Answers;*/
        echo '<br/>';
        $sql = "INSERT INTO `result`(`resultID`,`stdID`,`secID`, `grade`) VALUES (null,'" . $_SESSION['id'] . "','$sec','" . $correctAnswers . "')";
        $query = mysql_query($sql);
        if (!$query) echo "Database Error : " . $sql;
    }

else if ($EvalOrExam == 1)

{

        $sql = "UPDATE `student` SET `activateExam`=0 WHERE `ID`='". $_SESSION['id'] ."'";
        $query = mysql_query($sql);
        if (!$query) echo "Database Error : " . $sql;
        echo mysql_error();
        unset($_SESSION['examID']);
        unset($_SESSION['operationsCount']);
        unset($_SESSION['shapesCount']);
        unset($_SESSION['calculationsCount']);

}

    $_SESSION['finish'] = '1';
    unset($_SESSION['correctAnswers']);
    unset($_SESSION['wrongAnswers']);
    unset($_SESSION['Answers']);
}
}

表格:

  echo '<div>' . $result = evaluation('4', $level,$numOfQ,'1') . '</div>';
  echo '<input type="hidden" name="result" value="' . $result . '">';

方法评估调用同时调用显示和存储的操作。

2 个答案:

答案 0 :(得分:0)

最有可能的是,你的一个SQL语句没有正确形成。

每次

之后
$sql = ...

插入:

echo $sql;

然后直接在数据库中运行该SQL,看看你是否得到了你需要的东西。

另外,请编辑您的问题以反映当前的代码。有很多评论,很难弄清楚你的实际代码是什么。

如果您需要更多帮助,请告诉我。

答案 1 :(得分:0)

非常感谢你们,我非常感谢你的帮助。

通过调用方法

解决了这个问题
storeExamQ($sec, $level,$EvalOrExam, $Answers, $stdAnswer, $result,$Qbody);

在方法(1)后直接在学生回答并设置结果后。