PHP-PDO数据库 [英] Php - PDO Database

查看:46
本文介绍了PHP-PDO数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的功能,用于将单选按钮问题与存储在数据库中的答案一起检查答案.

Below is my function to check radio button questions to the answer with the answer stored in the database.

function chkQuestions($info)
{   
    $query = $this->handle->prepare("SELECT * FROM tbltest");
    $query->execute();
    $rows = $query->fetchAll();

    foreach ($rows as $row)
    {

        $answerID = array($info['answer'.$row['testID']]);


            if ($row['answer'] == $answerID ) 
                {
                      $score = 0;
                      $result = $score +1;
                      return $result;
                } else {
                      $result = "incorrect";
                     return $result;
                }
            //return $answerID;

    }//rows

}//chkQuestions

我的问题是,我编写的这段代码对单个问题表现良好,但我不确定如何使它适用于多个问题

My problem is that this code I did works well for a single question but I not sure how to make it work for multiple questions

有关我的代码的信息.

& info是一个存储所有$ _POST数据的数组.

&info is an array that stores all the $_POST data.

$ row ['answer']; -这是指我在数据库中的答案行.

$row['answer']; - this is referring to my answer row in the database.

Post数据已提交到函数中,看起来像这样

The Post data is submitted into the function and looks like this

<input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option3']?>" />

$ questions是一个包含数据库数据的数组,为每个问题分配一个数据库行的ID,我用来提交给chkquestion函数以将该行的ID与该问题进行匹配.

$questions is an array with database data which assign each question withthe ID of a database row, which i use to submit to the chkquestion function to match the id of the row to that question.

对于如何解决多个问题,我一心一意.

im stuck on how to make this work for multiple questions.

非常感谢

推荐答案

您可以具有answer[1]answer[2]之类的表单字段名称,并且在提交给PHP时,它们将被解析为一个数组.我希望您可以利用这些知识来重新编写代码.现在,它们对我来说是一团糟.

You can have form field names like answer[1], answer[2] and when submitted to PHP, they will be parsed as an array. I hope you can rework your code with this knowledge. Right now they are a mess to me.

name="answer[<?php echo $question['testID'] ?>]"

这篇关于PHP-PDO数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆