Gambar 1. Insert Database -CRUD 2 -Codeigniter 3 |
Pengaturan Awal:
1. Database /MySQL (menggunakan database tutorial sebelumnya)2. Config Database
3. Autoload Database
dapat melihat halaman sebelumya:
https://komputasistat.blogspot.co.id/2016/08/select-database-crud-1-codeigniter-3.html
Langkah Selanjutnya:
1. Atur baseurl karena kita akan menggunaan fungsi baseurl
sourcecode (...\application\config\config.php)
$config['base_url'] = 'http://localhost:8012/latihanci/MVC/1/';
2. Pada kali ini helper form juga digunakan. Ketika menggunakan helper lebih dari satu kita dapat menggunakan array
source code: Login.php (...\application\controllers\InsertLogin.php)
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class InsertLogin extends CI_Controller { function __construct(){ parent::__construct(); $this->load->model('data_insert'); $this->load->helper(array('url','form')); } public function index() { if($this->input->post('submit')){ $this->data_insert->tambah(); $this->load->view('telahterinsert'); }else $this->load->view('data_insert_form'); } }
3.
source code: data_insert.php (...\application\models\data_insert.php)
<?php class data_insert extends CI_Model { function tambah() { $role = $this->input->post('role'); $id_user = $this->input->post('id_user'); $username = $this->input->post('username'); $password = $this->input->post('password'); $data = array ( 'role' => $role, 'id_user' => $id_user, 'username'=> $username, 'password' => $password ); $this->db->insert('user',$data); } }
4.
source code: data_insert_form.php (...\application\views\data_insert_form.php)
<html> <head> <title>Form Tambah Data Login</title> <base href="<?php echo base_url(); ?>" /> </head> <body> <h3>Tambah Data Login</h3> <?php echo form_open('insertlogin'); ?> <table> <tr> <td> role </td> <td> <?php echo form_input('role'); ?> </td> </tr> <tr> <td> id_user </td> <td> <?php echo form_input('id_user'); ?> </td> </tr> <tr> <td> username </td> <td> <?php echo form_input('username'); ?> </td> </tr> <tr> <td> password </td> <td> <?php echo form_input('password'); ?> </td> </tr> <tr> <td> </td> <td> <?php echo form_submit('submit', 'Tambah'); ?> </td> </tr> </table> <?php echo form_close(); ?> </form> </body> </html>
5. bila berhasil ter insert source code telahterinsert.php akan dipanggil
source code: telahterinsert.php (...\application\views\telahterinsert.php)
<!DOCTYPE html> <html> <head> </head> <body> <center><h1>Telah TerInsert</h1></center> </body> </html>
6. Data yang sudah ters-insert dapat dilihat menggunakan phpmyadmin/ gunakan tutorial sebelumnya
https://komputasistat.blogspot.co.id/2016/08/select-database-crud-1-codeigniter-3.html
contoh:
<?php echo form_input('role','','class="test"'); ?>
<style>
.test{
display: block;
margin: 0;
width: 100%;
font-family: sans-serif;
font-size: 18px;
appearance: none;
box-shadow: none;
border-radius: none;
}
</style>
Trimakasih...
0 komentar:
Post a Comment