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>
5
  • 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 Commented Nov 12, 2018 at 6:27
  • Your code is open to SQL injection related attacks. Please learn to use Prepared Statements Commented Nov 12, 2018 at 6:27
  • Have you check $kode_soal get value in controller? Commented Nov 12, 2018 at 6: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? Commented Nov 12, 2018 at 6:43
  • can you try echo $this->db->last_query(); so we can find your query is correct or something wrong with query? Commented Nov 12, 2018 at 7:50

6 Answers 6

4

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

i'm sorry i don't quite understand what you are saying
you used the wrong input name in your controller and I made it correct in code above use the above code it will work
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');
    $data['tampilan'] = $this->m_model->tampil_soal($kode_soal);
    $this->load->view('test', $data);
}

Model

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

1 Comment

Bad practice: Inserting a variable as a value into the SQL string.
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();
    }

2 Comments

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
Bad practice: Inserting a variable as a value into the SQL string.
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();
    }

3 Comments

You didn't add the $where on the controller
or is there another way get the $where from the model?
is kode_soal field exists in your datatable?
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');    
}

Comments

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

1 Comment

Bad practice: Inserting a variable as a value into the SQL string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.