Codeigniter 4 Tutorials
- Codeigniter 4
- Application Run
- Application Structure
- Configuration files
- Errors
- Controller
- View
- Model
- How to remove index.php from URL
- Route
- CodeIgniter URLs
- Helper
- Session
- Global Functions and Constants
- Logging Information in CI4
- Error Handling
- Signin / SignUp
- Login / Register
- CRUD
- Crud 1
- Form Validation
- Data Table
- Pagination
- File Upload
- Show Data Using Ajax
- Select2 AJAX Autocomplete Search
- Curl Post
- Rest API
- Weblink
Home » Codeigniter 4 »
CRUD in Codeigniter 4?
Table
File name : index.php
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`city` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;
Route
File name : index.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['users'] = "users/index";
$route['usersCreate']['post'] = "users/store";
$route['usersEdit/(:any)'] = "users/edit/$1";
$route['usersUpdate/(:any)']['put'] = "users/update/$1";
$route['usersDelete/(:any)']['delete'] = "users/delete/$1";
Controller
File name : index.php
<php
use CodeIgniter\Controller;
use App\Models\UserModel;
class User extends Controller
{
public function index()
{
$model = new UserModel();
$data['users_detail'] = $model->orderBy('id', 'DESC')->findAll();
return view('list', $data);
}
public function create()
{
return view('add');
}
public function store()
{
helper(['form', 'url']);
$model = new UserModel();
$data = [
'name' => $this->request->getVar('txtName'),
'email' => $this->request->getVar('txtEmail'),
'password' => $this->request->getVar('txtPassword'),
'city' => $this->request->getVar('txtCity'),
];
$save = $model->insert($data);
return redirect()->to( base_url('User') );
}
public function edit($id = null)
{
$model = new UserModel();
$data['user'] = $model->where('id', $id)->first();
return view('edit', $data);
}
public function update()
{
helper(['form', 'url']);
$model = new UserModel();
$id = $this->request->getVar('id');
$data = [
'name' => $this->request->getVar('txtName'),
'email' => $this->request->getVar('txtEmail'),
'password' => $this->request->getVar('txtPassword'),
'city' => $this->request->getVar('txtCity'),
];
$save = $model->update($id,$data);
return redirect()->to( base_url('users') );
}
public function delete($id = null)
{
$model = new UserModel();
$data['user'] = $model->where('id', $id)->delete();
return redirect()->to( base_url('users') );
}
}
?>
Model
File name : index.php
<php
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $table = 'users';
protected $allowedFields = ['name','email','password','city'];
}
?>
View
File name : list.php
<html>
<head>
<title>Codeigniter 4 CRUD (Create Read Update Delete) Tutorial - Nicesnippets.com</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script
</head>
<body>
<table border="1" align="center">
<tr>
<td colspan="5" align="right"><a href="<?php echo site_url('usersCreate') ?>">Add</a></td>
</tr>
<tr>
<td>Name</td>
<td>Email</td>
<td>Password</td>
<td>City</td>
<td>Action</td>
</tr>
<?php
foreach($users_detail as $value){
?>
<tr>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['email']; ?></td>
<td><?php echo $value['password']; ?></td>
<td><?php echo $value['city']; ?></td>
<td><a href="<?php echo base_url(); ?>/usersEdit/<?php echo $value['id']; ?>">Edit</a> <a href="<?php echo base_url(); ?>/usersDelete/<?php echo $value['id']; ?>">Delete</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
View :- Add
File name : add.php
<form method="post" name="frmAdd" action="<?php echo site_url('usersCreate');?>">
<table align="center">
<tr>
<td colspan="2" align="center">Add Record</td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="txtName"> </td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="txtEmail"> </td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="txtPassword"> </td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="txtCity"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Add" name="btnadd"> </td>
</tr>
</table>
</form>
View :- edit
File name : edit.php
<form method="post" name="frmEdit" action="<?php echo base_url('usersUpdate');?>">
<table align="center">
<tr>
<td colspan="2" align="center">Edit Record</td>
</tr>
<input type="hidden" name="id" class="form-control" id="id" value="<?php echo $user['id'] ?>">
<tr>
<td>Name</td>
<td><input type="text" name="txtName" value="<?php echo $user['name']; ?>"> </td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="txtEmail" value="<?php echo $user['email']; ?>"> </td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="txtPassword" value="<?php echo $user['password']; ?>"> </td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="txtCity" value="<?php echo $user['city']; ?>"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Edit" name="btnEdit"> </td>
</tr>
</table>
</form>
File name : index.php