【问题标题】:How do I use an posted input array in a mysql query?如何在 mysql 查询中使用已发布的输入数组?
【发布时间】:2016-04-01 03:51:20
【问题描述】:

我正在开发一个培训课程应用程序,这对我来说是第一次,所以我遇到了一些障碍。我有一个表格,培训评估,当提交时,它会发送一组 id 和选择的答案到 php 页面进行处理。我的想法是运行一个mysql查询,选择正确答案的数量,然后另一个选择不同问题的数量,然后将两者相除以计算分数。这部分很简单,但我的思绪正在融化,试图找出选择正确答案数量的查询。

这是我的表单的代码...

<form name="assessment" method="post" action="scripts/scoreAssessment.php">
  <ul class="assessment">
    <li class='question'>This is a question? 
      <ul style='list-style:none;'>
        <li><input name='trainingCourseAnswer[]' type='radio' value='A1'/>This is a multiple choice answer.</li>
        <li><input name='trainingCourseAnswer[]' type='radio' value='B1'/>This is a multiple choice answer.</li>
        <li><input name='trainingCourseAnswer[]' type='radio' value='C1'/>This is a multiple choice answer.</li>
        <li><input name='trainingCourseAnswer[]' type='radio' value='D1'/>This is a multiple choice answer.</li>
      </ul>
    </li>
    <li class='question'>This is a question? 
      <ul style='list-style:none;'>
        <li><input name='trainingCourseAnswer[]' type='radio' value='A2'/>This is a multiple choice answer.</li>
        <li><input name='trainingCourseAnswer[]' type='radio' value='B2'/>This is a multiple choice answer.</li>
        <li><input name='trainingCourseAnswer[]' type='radio' value='C2'/>This is a multiple choice answer.</li>
        <li><input name='trainingCourseAnswer[]' type='radio' value='D2'/>This is a multiple choice answer.</li>
      </ul>
    </li>
    <!--continue pattern for total number of questions-->
  </ul>
  <input name='submitAssessment' type='submit' value='Finish Test'/>
</form>

在 scoreAssessment.php 中,我可以使用 PHP 的 substr 函数拆分所选单选输入答案的值,以获得两个变量,$selectedAnswer 和 $answerID。我需要运行以选择所有正确答案的查询如下...

$selectedAnswer=substr($_POST['trainingCourseAnswer'], 0, 1);
$answerID=substr($_POST['trainingCourseAnswer'], 1);
$query="SELECT COUNT(correctAnswer) WHERE answerID='$answerID' AND correctAnswer='$selectedAnswer'"

我遇到的问题是 $answerID 和 $selectedAnswer 都是具有多个值的数组...

谁能提供一些指导?我将不胜感激!

【问题讨论】:

  • "具有多个值的数组" - 由于每个单选按钮具有相同的名称,并且单选按钮的名称确定单选按钮,并且您只能在每个单选按钮中选择一个单选按钮您似乎更有可能得到一个具有一个值的数组。
  • 解决方案非常简单:php.net/for
  • 危险:你很容易受到SQL injection attacks的影响,你需要自己从defend那里得到。
  • 正如@Quentin 所说..您需要制作两个表单和两个提交按钮..或者您需要使用 Ajax 处理一个提交按钮..在这两种情况下您都需要两个表单。跨度>
  • @ErenArdahan — 什么?我没这么说。您不需要多个表格。您需要为每组单选按钮使用不同的名称(作为奖励,这消除了拆分值的需要,因为您不需要在名称中对单选组进行编码)

标签: php mysql arrays forms


【解决方案1】:

首先,您会注意到,使用提供的 HTML,用户只能从 8 个选项中选择一个答案。这不是您想要的:您需要允许每个问题选择一个。您可以通过为无线电控件指定不同的名称来解决此问题:每个问题一个唯一的名称。而且你也不需要数组表示法,因为每个问题你永远不会有超过一个答案:

<form name="assessment" method="post" action="scripts/scoreAssessment.php">
  <ul class="assessment">
    <li class='question'>This is a question? 
      <ul style='list-style:none;'>
        <li><input name='q1' type='radio' value='A'/>This is a multiple choice answer.</li>
        <li><input name='q1' type='radio' value='B'/>This is a multiple choice answer.</li>
        <li><input name='q1' type='radio' value='C'/>This is a multiple choice answer.</li>
        <li><input name='q1' type='radio' value='D'/>This is a multiple choice answer.</li>
      </ul>
    </li>
    <li class='question'>This is a question? 
      <ul style='list-style:none;'>
        <li><input name='q2' type='radio' value='A'/>This is a multiple choice answer.</li>
        <li><input name='q2' type='radio' value='B'/>This is a multiple choice answer.</li>
        <li><input name='q2' type='radio' value='C'/>This is a multiple choice answer.</li>
        <li><input name='q2' type='radio' value='D'/>This is a multiple choice answer.</li>
      </ul>
    </li>
    <!--continue pattern for total number of questions-->
  </ul>
  <input name='submitAssessment' type='submit' value='Finish Test'/>
</form>

因此,您不必将问题编号添加到单选按钮的 ABCD 值中(您可以,但这不是必需的)。此外,我选择了较短的名称(q1、q2),因为较长的名称也会使 URL 变长。

然后在 PHP 中,我建议从数组中的数据库表中读取所有正确答案,并将这些与用户的答案一一进行比较,保持计数。我在这里假设 mysqli_ 函数,但应该很容易将其转换为您喜欢的方法(如 PDO):

// Connection to database...
$con = mysqli_connect("my_host", "my_user", "my_password", "my_db");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Read all correct answers from the database:
$query = "SELECT answerID, correctAnswer FROM answerTable";
$result = mysqli_query($con, $sql);

// Fetch all records into an associative array
$correctAnswers = mysqli_fetch_all($result, MYSQLI_ASSOC);

// Count correct answers provided by user (via POST)
$correctCount = 0;
foreach ($correctAnswers as $correctAnswer) {
    $answerID = $correctAnswer['answerID'];
    if (isset($_POST["q$answerID"])
            && $_POST["q$answerID"] == $correctAnswer['correctAnswer']) {
        $correctCount++;
    }
}
// Score is fraction between 0 and 1. Multiply by 100 for a percentage
$score = $correctCount / count($correctAnswers);

请注意,这样可以避免SQL injection 的漏洞,因为上面的 SQL 语句不是由通过 POST 提交的值定义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 2017-12-11
    • 2016-09-25
    • 2016-07-08
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多