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 wpf中在模版中寻找元素
  • ¥15 MFC平台生成指定圆
  • ¥15 jmeter出现403
  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 怎样删除 右键菜单里的 剪切(T)和复制(C)
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题