0

I want insert 2 column to database [artikel & pengarang], when I save that form it will be insert $data from to table artikel and $data to table pengarang [IDArtikel from Artikel &nama_pengarang].

This is my artikel SQL:

CREATE TABLE `artikel`(
`IDArtikel` INT(11)NOT NULL AUTO_INCREMENT,
`IDJurnal` INT(11)NOT NULL,
`IDKategori` INT(11)NOT NULL,
`judul` VARCHAR(255)NOT NULL,
`abstract` text NOT NULL,
`nama_file` VARCHAR(255)NOT NULL,
`dilihat` INT(50)NOT NULL,
`didownload` INT(50)NOT NULL,
`created_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` VARCHAR(255)NOT NULL,
`updated_time` datetime NOT NULL,
`updated_by` VARCHAR(255)NOT NULL,
PRIMARY KEY(`IDArtikel`))ENGINE = INNODB DEFAULT CHARSET = utf8;

and this is pengarang SQL:

CREATE TABLE `pengarang`(
`IDPengarang` INT(11)NOT NULL AUTO_INCREMENT,
`IDArtikel` INT(11)NOT NULL,
`nama_pengarang` VARCHAR(255)NOT NULL,
PRIMARY KEY(`IDPengarang`))ENGINE = INNODB DEFAULT CHARSET = utf8;

this is my view:

<h3><?= $title; ?></h3><?php echo form_open("admin/artikel/buat/"); ?>
<table width="95%">
    <tr>
        <td><b>Pilih Referensi Jurnal</b></td>
        <td>
            <input type="hidden" name="IDJurnal" id="IDJurnal" value="<?php echo $IDJurnal; ?>" />
            <input type="text" name="volume" id="volume" value="<?php echo $volume; ?>" readonly="readonly"  class="sedang" />
        <?php echo anchor_popup('admin/artikel/popup', 'Referensi Jurnal', array('class' => 'button')); ?>
    </td>
</tr>
<tr>
    <td><b>Kategori</b></td>
    <td>
        <?php
            echo form_dropdown('IDKategori', $kategori) . "";
        ?>
        </td>
    </tr>
    <tr>
        <td width="125"><strong>Judul</strong></td>
        <td><input type="text" name="judul" class="panjang"></td>
    </tr>
    <tr>
        <td width="125"><strong>Pengarang</strong></td>
        <td>
            <input type="text" name="pengarang" class="panjang">
        </td>
    </tr>
    <tr>
        <td><b>Abstract</b></td>
        <td>
        <?php
            $data = array('name' => 'abstract');
            echo $this->ckeditor->editor($data['name']);
        ?>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <input type="submit" class="button" value="Simpan">
            <input type="button" class="button" value="Batal" onClick="javascript: history.go(-1)" />
        </td>
    </tr>
<?php
            echo form_close();
?>

this is my controller:

function buat() {
    if ($this->input->post('judul')) {
        $this->MArtikel->addArtikel();
        $this->session->set_flashdata('message', 'Artikel telah di buat !');
        redirect('admin/artikel/index', 'refresh');
    } else {
        // konfigurasi ckfinder dengan ckeditor
        $this->load->library('ckeditor');
        $this->load->library('ckfinder');
        $this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
        $this->ckeditor->config['toolbar'] = 'Full';
        $this->ckeditor->config['language'] = 'en';
        $this->ckfinder->SetupCKEditor($this->ckeditor, '../../../asset/ckfinder/');

        $data['title'] = "Tambah Artikel";
        $data['main'] = 'admin/artikel/artikel_buat';
        $data['jurnal'] = $this->MJurnal->getJurnalDropDown();
        $data['kategori'] = $this->MKategori->getKategoriDropDown();
        $this->load->vars($data);
        $this->load->view('dashboard/template');
    }
}

and this my model

function addArtikel() {
    $now = date("Y-m-d H:i:s");
    $data = array(
        'IDJurnal' => $this->input->post('IDJurnal'),
        'IDKategori' => $this->input->post('IDKategori'),
        'judul' => $this->input->post('judul'),
        'abstract' => $this->input->post('abstract'),
        'created_time' => $now,
        'created_by' => $_SESSION['username']
    );

    $this->db->insert('artikel', $data);
}

form in pengarang, can insert multiple data

5
  • is it not working. Tell us whats the problem Commented Nov 26, 2012 at 13:58
  • you can use $this->db->insert_batch($array); have a look here: ellislab.com/codeigniter/user-guide/database/… Commented Nov 26, 2012 at 13:58
  • codeigniter insert multiple table in one function Commented Nov 26, 2012 at 13:59
  • when i used $this->db->insert_batch($array); it will be just update 1 table in my database Commented Nov 26, 2012 at 14:01
  • Why not create a model for pengarang add the addArtickle method to it and then call it in your controller? Commented Nov 26, 2012 at 14:16

2 Answers 2

1

Take a look at $this->db->insert_batch();

Sign up to request clarification or add additional context in comments.

4 Comments

can we used $this->db->insert_batch('mytable1','mytable2' $data); ??
no. you can use $this->db->insert_batch('mytable1', array $data1) and then $this->insert_batch('mytable2', array $data2)
what it would be like this in my model $data1 = array( ); $data2 = array( ); $this->db->insert_batch('artikel', $data1); $this->db->insert_batch('pengarang', $data1);
Yes where $data1 = array(array('field1' => 'value1', 'field2' => 'value2'), array('field1' => 'value1', 'field2' => 'value2')); The method will take each array from $data1 and will make a $this->db->insert()
0

create new function in model addPengarang add the following code in your model, and change the values of data array as per your requirement it will insert values in Pengarng table

function addPengarang() { 
   $data = array(
        'IDPengarang' => 'value for IDPegarang',
        'IDArtikel' => 10,
        'nama_pengarang' => 'value for nama pengarang'
    );

    $this->db->insert('pengarang', $data);
}

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.