将测试分数提交到数据库

时间:2015-01-19 04:51:51

标签: php mysql

我终于想出了如何创建一个将数据注入数据库表的简单注册表单。我的代码看起来像这样:

<form id="signupform" autocomplete="off" method="post" action="" novalidate>
  <table>
    <tr>
      <td class="label"><label id="llastname" for="lastname">Last Name</label></td>
      <td class="field"><input id="lastname" name="lastname" type="text" value="" maxlength="100"></td>
      <td class="status"></td>
    </tr>
    <tr>
      <td class="label"><label id="lsignupsubmit" for="signupsubmit">Signup</label></td>
      <td class="field" colspan="2"><input id="signupsubmit" name="signup" type="submit" value="Signup"></td>
    </tr>
  </table>

  <?php
 include('config.php');
 $pdo = connect();
try {
  $sql = "INSERT INTO g1_members (firstname VALUES (:firstname)";
  $query = $pdo->prepare($sql);
  $query->bindParam(':lastname', $_POST['lastname'], PDO::PARAM_STR);
  $query->execute();
} catch (PDOException $e) {
echo 'PDOException : '.  $e->getMessage();
}
?>
</form>

现在我想弄清楚如何将测试分数放入不同的数据库表中。

当访问者在mysite / test / gw-intro-1进行测试时,在选择答案并单击“提交”按钮后,他们会转发到mysite / test / results.php,其中下面的代码计算并显示考试成绩:

<?php
 $answer1 = $_POST['q1'];
 $answer2 = $_POST['q2'];
 $answer3 = $_POST['q3'];
 $answer4 = $_POST['q4'];
 $answer5 = $_POST['q5'];
 $answer6 = $_POST['q6'];
 $answer7 = $_POST['q7'];
 $answer8 = $_POST['q8'];
 $answer9 = $_POST['q9'];
 $answer10 = $Ch;

 $totalCorrect = 0;

if ($answer1 == "A") { $totalCorrect++; }
if ($answer2 == "Jupiter") { $totalCorrect++; }
if ($answer3 == "C") { $totalCorrect++; }
if ($answer4 == "D") { $totalCorrect++; }
if ($answer5 == "A") { $totalCorrect++; }
if ($answer6 == "C") { $totalCorrect++; }
if ($answer7 == "C") { $totalCorrect++; }
if ($answer8 == "C") { $totalCorrect++; }
if ($answer9 == "B") { $totalCorrect++; }
if ($answer10) { $totalCorrect++; }

echo ''.$totalCorrect.' out of 10';
?>

所以我试图弄清楚如何将值$ totalCorrect放入数据库表中 - 可能没有表单;它应该在用户单击“提交”按钮时自动提交。

0 个答案:

没有答案