1

I'm trying to insert an array of data into a table in database but an error said Array to string conversion error

This is the post function in my controller, first i post an array of data. The values of the array will be the names, and numbers, they are not id. The id is only kodejdwl. This will be pass to my model

function index_post() {
    $data = array(
        'kodejdwl' => $this->post('kodejdwl'),
        'tahun_akad' => $this->post('kode_tahun_akad'),
        'semester' => $this->post('semester'),
        'mk' => $this->post('mk'),
        'ruangan' => $this->post('ruangan'),
        'nama_dosen' => $this->post('nama_dosen'),
        'namakelas' => $this->post('nama_kelas'),
        'jam_mulai' => $this->post('jam_mulai'),
        'jam_selesai' => $this->post('jam_selesai'),
    );
}

After the data from above code is passed to the model. I created some new variables which are the id of each the name of the value in the array data. e.g if the value of data['mk'] is Website then the id will be 1 and that id will be stored in variable $kodemk and i do it to each value in the data. Then i created new_data which stores array of the id's which i previously made. Then i insert that array into one table in my database. I thought it would be fine but it said Array to string conversion error. What should i do so i could insert that array into the table in my database?

public function insert($data){
    $this->db->select('thn_akad_id');
    $tahunakad_id = $this->db->get_where('tik.thn_akad',array('tahun_akad'=>$data['tahun_akad'],'semester_semester_nm'=>$data['semester']))->result();
    $this->db->flush_cache();

    $this->db->select('kodemk');
    $kode_mk =  $this->db->get_where('tik.matakuliah',array('namamk'=>$data['mk']))->result();
    $this->db->flush_cache();

    $ruangan = $this->db->get_where('tik.ruangan', array('namaruang' => $data['ruangan']), 1)->result();
    $this->db->flush_cache();

    $this->db->select('nip');
    $nip_dosen = $this->db->get_where('tik.staff',array('nama'=>$data['nama_dosen']))->result();
    $this->db->flush_cache();

    $this->db->select('kodeklas');
    $kodeklas = $this->db->get_where('tik.kelas',array('namaklas'=>$data['namakelas']))->result();
    $this->db->flush_cache();

    $this->db->select('kode_jam');
    $kode_mk =  $this->db->get_where('tik.wkt_kuliah',array('jam_mulai'=>$data['jam_mulai'],'jam_selesai'=>$data['jam_selesai']))->result();
    $this->db->flush_cache();

    $new_data = array(
        'kodejdwl' => $data['kodejdwl'],
        'thn_akad_thn_akad_id' => $tahunakad_id,    
        'matakuliah_kodemk' => $kode_mk,
        'ruangan_namaruang' => $ruangan,
        'staff_nip' => $nip_dosen,
        'kelas_kodeklas' => $kodeklas,          
    );

    $insert = $this->db->insert('tik.jadwal_kul', $new_data);
    return $this->db->affected_rows();
}
2
  • tik.jadwal_kul is your table name? Commented Jun 19, 2020 at 5:51
  • yes, it's my table name Commented Jun 19, 2020 at 5:53

2 Answers 2

1

You probably want to use row() instead of result() because it'll contain only one result that you want. If you want to use result() and store multiple values then you'll have to use implode to concatenate them and store it as a string.
I've written a possible solution for your problem; Some things were missing, so I've mentioned them in the comments. See if this helps you.

public function insert($data){

    $this->db->select('thn_akad_id');
    $tahunakad_id = $this->db->get_where('tik.thn_akad',array('tahun_akad'=>$data['tahun_akad'],'semester_semester_nm'=>$data['semester']))->row(); // use row here
    $this->db->flush_cache();

    $this->db->select('kodemk');
    $kode_mk =  $this->db->get_where('tik.matakuliah',array('namamk'=>$data['mk']))->row();
    $this->db->flush_cache();

    // remove your_ruangan_column with your desired column name
    $this->db->select('your_ruangan_column');
    $ruangan = $this->db->get_where('tik.ruangan', array('namaruang' => $data['ruangan']), 1)->row();
    $this->db->flush_cache();

    $this->db->select('nip');
    $nip_dosen = $this->db->get_where('tik.staff',array('nama'=>$data['nama_dosen']))->row();
    $this->db->flush_cache();

    $this->db->select('kodeklas');
    $kodeklas = $this->db->get_where('tik.kelas',array('namaklas'=>$data['namakelas']))->row();
    $this->db->flush_cache();

    // Not sure where this ↓↓ is being used but you can use it the same way as others
    $this->db->select('kode_jam');
    // duplicate variable name here ↓↓ (fix this)
    $kode_mk =  $this->db->get_where('tik.wkt_kuliah',array('jam_mulai'=>$data['jam_mulai'],'jam_selesai'=>$data['jam_selesai']))->row();
    $this->db->flush_cache();

    $new_data = array(
        'kodejdwl' => $data['kodejdwl'],
        'thn_akad_thn_akad_id' => $tahunakad_id->thn_akad_id, // {$tahunakad_id} consists an object with the key {thn_akad_id}-- table_column_name
        'matakuliah_kodemk' => $kode_mk->kodemk, // ...
        'ruangan_namaruang' => $ruangan->your_ruangan_column, // ... 
        'staff_nip' => $nip_dosen->nip, // ...
        'kelas_kodeklas' => $kodeklas->kodeklas // ...         
    );

    $insert = $this->db->insert('tik.jadwal_kul', $new_data);
    return $this->db->affected_rows();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your are making a total of 7 separate trips to the database. Best practice recommends that you always minimize your trips to the database for best performance. The truth is that your task can be performed in a single trip to the database so long as you set up the correct INSERT query with SELECT subqueries.

I don't know what your non-English words are, so I will use generalized terms in my demo (I've tested this successfully in my own CI project). I am also going to reduce the total subqueries to 3 to reduce the redundance in my snippet.

$value1 = $this->db->select('columnA')->where('cond1', $val1)->get_compiled_select('childTableA');
$value2 = $this->db->select('columnB')->where('cond2', $val2)->get_compiled_select('childTableB');
$value3 = $this->db->select('columnC')->where('cond3', $val3)->get_compiled_select('childTableC');

return (int)$this->$db->query(
    "INSERT INTO parentTable
         (column1, column2, column1)
     VALUES (
         ($value1),
         ($value2),
         ($value3)
     )"
);
// to mirror your affected rows return... 1 will be returned on successful insert, or 0 on failure

Granted this isn't using the ActiveRecord technique to form the complete INSERT query, but this is because CI doesn't allow subqueries in the VALUES portion (say, if you were to use the set() method). I am guessing this is because different databases use differing syntax to form these kinds of INSERTs -- I don't know.

The bottom line is, so long as you are fetching a single column value from a single row on each of these sub-SELECTs, this single query will run faster and with far less code bloat than running N number of individual queries. Because all of the variables involved are injected into the sql string using get_compiled_select() the stability/security integrity should be the same.

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.