Cara update data codeigniter 4 untuk pemula

Cara update data codeigniter 4 untuk pemula

Cara update data codeigniter 4 untuk pemula

Pada artikel codeigniter 4 sebelumnya telah kita lewati sebelum saya akan melanjutkan tutorial Cara update data codeigniter 4 untuk pemula ini. Sebaiknya kalian mengikuti tutorial sebelumnya agar semakin mudah memahami tutorial sebelumnya d bawah ini

  1. Cara install composer
  2. Cara install codeigniter 4 dengan composer
  3. Cara insert data dengan codeigniter dan mysql untuk pemula
  4. Cara menampilkan data pada codeigniter 4 untuk pemula

Nah setelah mengikuti tutorial tersebut mari kita lanjutkan ke tutorial Cara update data codeigniter 4 untuk pemula ini.

Modifikasi Controller

<?php

namespace App\Controllers;

use CodeIgniter\Controller;

use App\Models\CrudModel;

class Crud extends BaseController

{

public function __construct(){

}

public function index()

{

$data['title'] = "Form Tambah";

$model = new CrudModel();

$data['posts'] = $model->findAll();

return view('tampil',$data);

}

public function create()

{

return view('crud');

}




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

}

}

public function view_edit($id = null)

{

$model = new CrudModel();

$data['user'] = $model->where('id', $id)->first();

return view('update', $data);

}

public function update($id = null){

$data = $this->request->getVar();

$id = $this->request->getVar('id');

$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'] = "Crud Codeigniter";

echo view('update',$data, [

'validation' => $validation

]);

}

else{

//$CrudModel = new \App\Models\CrudModel();

$session = \Config\Services::session();

try{

$model = new CrudModel();

$user = $model->update($id,$data);

$session->setFlashdata('msg', 'Record Updated successfully');

}

catch(\Exception $e){

$session->setFlashdata('msg', 'Something went wrong');

}

$data['title'] = "Crud Codeigniter";

return redirect()->to( base_url('index.php/crud') );

}

}

}

 

Buat tampilan Form Update

Buat sebuah file baru di folder namaproject/app/views/update.php berinama update.php dan copy code dibawah ini

<!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/update">

<input type="hidden" name="id" id="id" value="<?php echo $user['id'];?>">

<div class="form-group">

<label for="name">Name:</label>

<input type="text" name="username" value="<?php echo $user['username']; ?>" class="form-control" id="username">

</div>

<div class="form-group">

<label for="password">password:</label>

<input type="text" name="password" value="<?php echo $user['password']; ?>" class="form-control" id=" password">

</div>

<div class="form-group">

<label for="email">Email:</label>

<input type="email" name="email" value="<?php echo $user['email']; ?>" class="form-control" id="email">

</div>

<button type="submit" class="btn btn-primary">Simpan</button>

</form>

<a href="<?php echo base_url('index.php/crud') ?>" class="btn btn-sm btn-success">Kembali</a>

</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>

 

Nah demikianlah Cara update data codeigniter 4 untuk pemula ini, semoga bermanfaat, jika ada yang masih belum paham jangan lupa tanyakan dikolom komentar ya

 

Tinggalkan Komentar

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

Need Help? Chat with us