0

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>
Happy Dog
  • 87
  • 1
  • 8
  • You are passing `$where` as an array, while you are not using the key's value for using it as a column in `Where` condition – Madhur Bhaiya Nov 12 '18 at 06:27
  • Your code is open to [SQL injection](https://stackoverflow.com/q/332365/2469308) related attacks. Please learn to use [Prepared Statements](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Madhur Bhaiya Nov 12 '18 at 06:27
  • Have you check $kode_soal get value in controller? – Sachin Nov 12 '18 at 06:33
  • @Sachin yes i'm already make the view input of the `$kode_soal` and make sure that it gets the value to the controller but the problem is probably in the model? @MadhurBhaiya what do you mean that is open to SQL injection? is like get easily hacked or something? – Happy Dog Nov 12 '18 at 06:43
  • can you try echo $this->db->last_query(); so we can find your query is correct or something wrong with query? – Sachin Nov 12 '18 at 07:50

6 Answers6

5

just try this:

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


// model
public function tampil_soal($where){
    $this->db->where('kode_soal',$where);
    return $this->db->get('soal')->result();
    // use the return $this->db->get('soal')->row();
    // if your query return one record
}
Khalifa Nikzad
  • 1,213
  • 1
  • 6
  • 17
0

I think you have to use result() function with query

public function tampil_soal($where){
        return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()")->result();
    }
Poonam Navapara
  • 186
  • 1
  • 9
  • isn't the `->result()` is for the controller? PS:I have tried it still not working, and it says `Call to a member function result() on a non-object` – Happy Dog Nov 12 '18 at 06:44
0

try this query: //controller

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

//model

public function tampil_soal($where){
      return $this->db->where('kode_soal',$where)->get('soal')->result();
    }
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
0

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();
        }
    }
Faisal Mk
  • 71
  • 5
0

Model

public function tampil_soal($where){
    $condition = $where;
    // Your Condition...
    $this->db->where($condition);
    // Order By ...
    $this->db->order_by('RAND()');
    // Return Data from Table ...
    return $this->db->get('soal');    
}
Darryl Ceguerra
  • 181
  • 1
  • 7
0

change this

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

to this

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

hope it'll helps