0

I want to make an edit form

This is my controller:

public function edit_data($id_kategori){
    $artikel = $this->mymodel->GetArtikel("where id_kategori = '$id_kategori'");
    $data = array(
        "id_kategori" => $mhs[0]['id_kategori'],
        "nama_lengkap" => $mhs[0]['nama_lengkap'],
        "judul" => $mhs[0]['judul'],
        "nama_kategori" => $mhs[0]['nama_kategori'],
        "isi" => $mhs[0]['isi']
    );
    $this->load->view('form_edit',$data);
    }   
    
public function do_update(){
    $id_kategori= $_POST['id_kategori'];
    $nama       = $_POST['nama_lengkap'];
    $judul      = $_POST['judul'];
    $nama_kategori  = $_POST['nama_kategori'];
    $isi        = $_POST['isi'];
    $data_insert = array(
    'id_kategori' => $id_kategori,
    'nama_lengkap' => $nama,
    'judul' => $judul,
    'nama_kategori' => $nama_kategori,
    'isi' => $isi);
    $where = array('id_kategori' => $id_kategori);
    $res = $this->mymodel->UpdateData('artikel',$data_update,$where);
    if($res>=1){
        $this->session->set_flashdata('pesan','Update Data Sukses');
        redirect('crud/index');
    }
    }

and my model :

public function GetArtikel($id_kategori=''){
        $data = $this->db->query('SELECT a.id_artikel, a.judul, a.tanggal_buat, a.tanggal_update, b.nama_kategori, c.nama_lengkap, c.id_user FROM artikel as a LEFT JOIN kategori as b on a.id_kategori=b.id_kategori LEFT JOIN user as c on a.id_user=c.id_user' .$id_kategori);
        return $data->result_array();
    }
    public function UpdateData($tabelNama,$data,$where){
        $res = $this->db->update($tabelNama,$data,$where);
        return $res;
        }

but when i run to the browser. error :

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id_kategori = '1'' at line 1

How can I resolve it?

3 Answers 3

1

Put a space before you concatenate the WHERE clause:

....on a.id_user=c.id_user' .$id_kategori);

to

....on a.id_user=c.id_user ' .$id_kategori);
Sign up to request clarification or add additional context in comments.

Comments

0

In Controller

public function do_update(){    

    $res = $this->mymodel->UpdateData();

    if($res == TRUE)
    {
        # Success
        $this->session->set_flashdata('pesan','Update Data Sukses');
        redirect('crud/index');
    }
    else
    {
        # Error
        echo "Update Failed";
    }
}

In Model

public function UpdateData(){

    $id_kategori    = $_POST['id_kategori'];
    $nama           = $_POST['nama_lengkap'];
    $judul          = $_POST['judul'];
    $nama_kategori  = $_POST['nama_kategori'];
    $isi            = $_POST['isi'];

    $data = array(
       'nama_lengkap'   => $nama,
       'judul'          => $judul,
       'nama_kategori'  => $nama_kategori,
       'isi'            => $isi
    );

    $this->db->where('id_kategori', $id_kategori);

    if(!$this->db->update('artikel', $data))
    {
        #Error
        return FALSE;
    }
    else
    {
        #Success
        return TRUE;
    }
}

Comments

0
  • In a CodeIgniter application, $_POST should not be directly accessed. $this->input->post() is the method of choice.
  • For a professional-looking, reliable, PSR-12 compliant codebase, method names should be in camelCase, method parameters should implement type hinting, lines of code should not be excessively wide, and methods' opening and closing curly braces should be on lines by themselves.
  • edit_data() is a vague and almost unintuitive endpoint for a controller method that merely presents a form (doesn't actually edit anything). Because this controller might be sensibly called Artikel, it may be enough to call this method loadForm() or something similar.
  • The do_update() method name could be simplified to update() and be just as informative.
  • It is very bad practice to send a whole query clause expression as a string to the model method. You may enjoy the flexible alternative of passing an associative array as a array $where = [] parameter.
  • Use CodeIgniter's active record syntax to build a query with chainable and intuitive methods which help with value and identifier quoting.

Ultimately your script only fails because you smashed your WHERE clause string too close to the end of your query string. It would have been rendered as:

...LEFT JOIN user as c on a.id_user=c.id_userwhere id_kategori =...

but your user table doesn't have a column called id_userwhere.


To put you on a better path, Let me recommend a new Artikel model method which receives an associative array containing WHERE clause conditions.

public function getAll(array $where = []): array
{
    return $this->db
        ->select([
            'a.id_artikel',
            'a.judul',
            'a.tanggal_buat',
            'a.tanggal_update',
            'b.nama_kategori',
            'c.nama_lengkap',
            'c.id_user'
        ])
        ->from('artikel a')
        ->join('kategori b', 'id_kategori', 'left')
        ->join('user c', 'id_user', 'left')
        ->where($where)
        ->get()
        ->result_array();
}

I will mention that I don't understand why you are only accessing the first row of a result set which may contain more than one row. And your provided script is trying to access $mhs which is never declared before it is accessed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.