bandungeuy
bandungeuy

Reputation: 512

Codeigniter Multi Insert Query

I couldn't find any proper example how to execute multiple insert query in codeigniter. I already tried make a function in models, containing more than 1 insert query. What I get always first query success, and others not executed at all.

I've already try use transaction: $this->db->trans_start(); In my case below: $db->trans_start(); and off course $db->trans_complete();

But that didn't help. Always only first insert query that executed. What should I do to execute all insert/update/delete queries in one fuction?

Example:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Deposit_model extends CI_Model
{
     function __construct()
     {
          // Call the Model constructor
          parent::__construct();

     }




     public function inputdeh() 
     {
            $this->load->helper('url');
            $CI =& get_instance();
            $db = $CI->load->database( 'local', TRUE );

            $id_member = $this->input->post('id_member');
            $jumlah = $this->input->post('jumlah');
            $ket1 = $this->input->post('ket');
            $ket = 'WEB/'.$ket1;


        $saldoawal = $db->query("select saldo from member where id_member='$id_member'");
        $saldoawa['saldo'] = $saldoawal->row('saldo');
        $saldoaw = $saldoawa['saldo']; 

        $saldoak = $saldoaw + $jumlah; 
        //$_POST[jumlah]=number_format($_POST['jumlah'],".");
        //$saldoak = number_format($saldoak);
        //$dep = number_format(floatval($jumlah));

        $result = $db->query("select nama from member where id_member='$id_member'");
        $namar['nama'] = $result->row('nama');
        $nama = $namar['nama'];

        $resulthp = $db->query("select hp from member_hp where id_member='$id_member' LIMIT 1 OFFSET 0");
        $hpr['hp'] = $resulthp->row('hp');
        $nohp = $hpr['hp'];

        //$now = 'NOW()';
        $user = 'adminweb';


        $data = array(
        'id_member' => $this->input->post('id_member'),
        'nama' => $nama,
        'jml' => $this->input->post('jumlah'),
        'saldo_awal' => $saldoaw,
        'saldo_akhir' => $saldoak,
        'kode_trx' => '1',
        'status' => '1',
        'ket' => $ket,
        'user_input' => $user,
        );
        $db->set('tgl_transaksi', 'NOW()', FALSE);
        $db->set('tgl_input', 'NOW()', FALSE);

        return $db->insert('transaksi', $data);

        $logsaldodata = array(
        'id_member' => $this->input->post('id_member'),
        'saldo' => $saldoaw,
        'act' => $this->input->post('jumlah'),
        'ket' => 'Deposit Tambah~True#',
        );
        $db->set('tgl', 'NOW()', FALSE);
        $db->set('ref', 'last_insert_id()', FALSE);
        return $db->insert('log_saldo', $logsaldodata);
}
}

Upvotes: 0

Views: 545

Answers (1)

Damien Pirsy
Damien Pirsy

Reputation: 25435

Of course, you're returning before executing the rest of the code!

$db->set('tgl_input', 'NOW()', FALSE);
return $db->insert('transaksi', $data); // <-- remove the keyword here
$logsaldodata = array(

Remove that intermediate return statement, just leave $db->insert('transaksi', $data). return stops the function and returns the result, the rest of the function code won't be executed.

Upvotes: 1

Related Questions