我有一个小的多选页面,用户可以回答问题并且所有答案都存储在数据库中但是由于某种原因代码剂量正确执行无论答案我点击它只显示3个可用选项中的第一个选项。让我说我有3个答案苹果,梨,窗口,如果我点击第一个苹果它将苹果存储在数据库中,但如果我点击窗口或梨它将存储苹果而不是窗口或梨可以任何人帮助我在这里这里是我的控制器功能:
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; ?>
答案 0 :(得分:1)
在您的HTML中,您已为所有三个复选框值分配了$survay->qA
。
<input type="checkbox" name="qB" value="<?php echo $survay->qA; ?>">
检查一下。这可能会导致问题。