0

我想将 2 列插入数据库 [artikel & pengarang],当我保存该表单时,它将插入$data到表 artikel 和 $data 到表 pengarang [IDArtikel from Artikel &nama_pengarang]。

这是我的文章 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;

这是 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;

这是我的观点:

<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();
?>

这是我的控制器:

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');
    }
}

这是我的模型

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);
}

pengarang中的表格,可以插入多个数据

4

2 回答 2

1

看看 $this->db->insert_batch();

于 2012-11-26T14:01:50.823 回答
0

在模型中创建新函数在模型中addPengarang添加以下代码,并根据您的要求更改数据数组的值,它将在 Pengarng 表中插入值

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

    $this->db->insert('pengarang', $data);
}
于 2012-11-26T14:45:32.307 回答