dtlygweb2017 2018-11-12 06:24
浏览 77
已采纳

Codeigniter - 带变量的mysql查询

I need help for the query in my model, I want to find the field with variable that i'm input to the controller and as for right now the problem is that the field won't read my variable so here is the code

Controller

public function tampil_soal(){
        $kode_soal = $this->input->post('kode_soal');
        $where = array('kode_soal' => $kode_soal);
        $data['tampilan']= $this->m_model->tampil_soal($kode_soal)->result();
        $this->load->view('soal_tampil',$data);
    }

Model

public function tampil_soal($where){
        return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
    }

View

<form action="<?php echo base_url()?>siswa/tampil_soal" method="post" class="form">
    <input class="input" name="kode_ujian" placeholder="Kode ujian"/>
  <input class="button" type="submit" name="submit" value="Selesai"/>
</form>
  • 写回答

6条回答 默认 最新

  • dongyingtang3803 2018-11-12 08:02
    关注

    View

    <form action="<?php echo base_url()?>siswa/tampil_soal" method="post" class="form">
        <input class="input" name="kode_ujian" placeholder="Kode ujian"/>
      <input class="button" type="submit" name="submit" value="Selesai"/>
    </form>
    

    Controller

    you are using wrong post name "$this->input->post('kode_soal')" change this to $this->input->post('kode_ujian') then

    public function tampil_soal(){
            $kode_soal = $this->input->post('kode_ujian');
            $where = array('kode_soal' => $kode_soal);
            $data['tampilan']= $this->m_model->tampil_soal($kode_soal);
            $this->load->view('test',$data);
        }
    

    Model

    public function tampil_soal($where){
        $qry = $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
            if($qry->num_rows()>0){
                return $qry->result();
            }else{
                return array();
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加