Cara insert data dengan codeigniter dan mysql untuk pemula

Cara insert data dengan codeigniter dan mysql untuk pemula

Cara insert data dengan codeigniter dan mysql untuk pemula

Pada tutorial Cara insert data dengan codeigniter dan mysql untuk pemula, pertama tama yang harus kita lakukan adalah membuat database terlebih dahulu

Buat Database

Buat Tabel

Kolom table

Atau copy code di bawah ini

CREATE TABLE `codeigniter4`.`users` 

( `id` INT NOT NULL AUTO_INCREMENT , 

`username` VARCHAR(50) NOT NULL , 

`password` VARCHAR(50) NOT NULL , 

`email` VARCHAR(50) NOT NULL , 

PRIMARY KEY (`id`)) 

ENGINE = InnoDB;

 

Buat view Form Input

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Crud Codeigniter 4 <?=$title?></title>

<!-- Bootstrap CSS -->
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="col-lg-4">

<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Form Tambah</h3>
</div>
<div class="panel-body">
<?= \Config\Services::validation()->listErrors(); ?>
<?= \Config\Services::session()->getFlashdata('msg'); ?>
<form method="post" action="<?= base_url() ?>/index.php/crud/save">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="username" class="form-control" id="username">
</div>
<div class="form-group">
<label for="age">password:</label>
<input type="password" name="password" class="form-control" id=" password">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" name="email" class="form-control" id="email">
</div>
<button type="submit" class="btn btn-primary">Simpan</button>
</form>
</div>
</div>
</div>


<!-- jQuery -->
<script src="//code.jquery.com/jquery.js"></script>
<!-- Bootstrap JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</body>
</html>

 

Buat controller

<?php

namespace App\Controllers;

class Crud extends BaseController
{
public function __construct(){
}

public function index()
{
$data['title'] = "Form Tambah";
return view('crud',$data);
}



public function save(){
$data = $this->request->getVar();
$validation = \Config\Services::validation();
$validation->setRules([
'username' => 'required|string',
'password' => 'required|string',
'email' => 'required|valid_email'
]);
$res = $validation->withRequest($this->request)
->run();
if(!$res){

$data['title'] = "Validasi Input";
echo view('crud',$data, [
'validation' => $validation
]);
}
else{
$crudModel = new \App\Models\CrudModel();
$session = \Config\Services::session();
try{
$crudModel->insert($data);

$session->setFlashdata('msg', 'Record Inserted successfully');
}
catch(\Exception $e){
$session->setFlashdata('msg', 'Something went wrong');
}
$data['title'] = "Form Tambah";
echo view('crud',$data);
}
}

}

 

Buat Model

<?php namespace App\Models;

use CodeIgniter\Model;

class CrudModel extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';

protected $returnType = 'array';
protected $useSoftDeletes = true;

protected $allowedFields = ['username', 'password', 'email'];

protected $useTimestamps = false;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';

protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
}

 

Buat yang masih bingung bagaimana mulai belajar codeigniter 4 lihat tutorial Cara install codigniter di link ini

Tinggalkan Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Need Help? Chat with us