0

我有一个小的多项选择页面,用户可以在其中回答问题,并且所有答案都存储在数据库中,但是由于某种原因,无论我点击什么答案,代码都可以正确执行,它只会显示 3 个可用选项中的第一个选项。假设我有 3 个答案 apple, pear, window 如果我点击第一个苹果,它会将苹果存储在数据库中,但如果我点击 window 或 pear,它将存储苹果而不是 window 或 pear 任何人都可以在这里帮助我是我的控制器功能:

function addanswer() {

    $arrData = array();
    $userid = null;
    if ($this->session->userdata("userid")) {
        $userid = $this->session->userdata("userid");
    }
   if ($this->input->post()) {
        //$arrData["answerid"] = $this->input->post("QID");
        $arrData["questionid"] = $this->input->post("qA");
        if ($this->input->post("qA")) {
            $arrData["answerA"] = $this->input->post("qA");
        }
        if($this->input->post("qB")) {
            $arrData["answerB"] = $this->input->post("qB");
        }
        if($this->input->post("qC")) {
            $arrData["answerC"] = $this->input->post("qB");
        }
        $arrData["userid"] = $userid;
    }
    $viewData['survay_data_id'] = $this->survay->addsurvay($arrData); 
    $this->load->view('survay_view', $viewData);
}

模型

   <?php

class Survay extends CI_Model {

    function dosurvay($question_id = null) {

        $this->db->select('QID, Question, qA, qB, qC');
        $this->db->from('tblquestions');
        if ($question_id) {
            $this->db->where('QID', $question_id);
        }
        $this->db->limit(1);
        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return $query->result();
        } else {
            return false;
        }
    }

function addsurvay($arrData) {

    $this->db->insert('tblanswers', $arrData);

    if ($this->db->affected_rows() > 0) {
        return $this->db->insert_id();
    } else {
        return false;
    }
}

}
?>

看法

     <?php if(isset($survay_data)) : ?>
<form action="http://localhost/Surva/index.php/survaycontroller/addanswer/" name="myform" id="myform" method="post">
   <?php foreach ($survay_data as $survay): ?> 
        <ul>
            <li><h1><?php echo $survay->Question; ?></h1></li> 
            <li><?php echo $survay->qA; ?><input type="checkbox" name="qA" value="<?php echo $survay->qA; ?>"></li>
            <li><?php echo $survay->qB; ?><input type="checkbox" name="qB" value="<?php echo $survay->qB; ?>"></li>
            <li><?php echo $survay->qC; ?><input type="checkbox" name="qC" value="<?php echo $survay->qC; ?>"></li>
            Comments:
            <li><?php echo form_textarea('comment')?></li>
            <li><input type="hidden" name="QID" value="<?php echo $survay->QID; ?>"></li>
            <li><input type="submit" name="btn" value="Answer"></li>
        </ul>
    <?php endforeach; ?>
</form>
<?php endif; ?>
4

1 回答 1

1

在您的 HTML 中,您已分配$survay->qA给所有三个复选框值。

<input type="checkbox" name="qB" value="<?php echo $survay->qA; ?>">

核实。这可能会导致问题。

于 2013-02-28T09:11:14.560 回答