PHP Programming Tutorial 1 - php codeigniter4 configuration in xampp or wamp
PHP Programming Tutorial 2 - CodeIgniter 4 Application Structure
PHP Programming Tutorial 3 - What is MVC in Codeigniter 4
PHP Programming Tutorial 4 - Codeigniter 4 Environment Variables
PHP Programming Tutorial 5 - Controllers in CodeIgniter 4
PHP Programming Tutorial 6 - Views in CodeIgniter 4
PHP Programming Tutorial 7 - Models in CodeIgniter 4
PHP Programming Tutorial 8 - Remove index.php from URL in CodeIgniter 4
PHP Programming Tutorial 9 - Include header and footer in CodeIgniter 4
PHP Programming Tutorial 10 - Session in CodeIgniter 4
PHP Programming Tutorial 11 - URI Routing in CodeIgniter 4
PHP Programming Tutorial 12 - Highlight Menu using URI Segment in CodeIgniter 4
PHP Programming Tutorial 13 - Connect to Database in CodeIgniter 4
PHP Programming Tutorial 14 - Insert data in Database using CodeIgniter 4
PHP Programming Tutorial 15 - Retrieve data from Database using CodeIgniter 4
PHP Programming Tutorial 16 - Update data in Database using CodeIgniter 4
PHP Programming Tutorial 17 - Delete data in Database using CodeIgniter 4
PHP Programming Tutorial 18 - Generate PDF in CodeIgniter 4
PHP Programming Tutorial 19 - Send Email using PHPMailer in CodeIgniter 4
PHP Programming Tutorial 20 - Upload a File in CodeIgniter 4
🔰 PHP Programming Tutorial 14 - Insert data in Database using CodeIgniter 4 🔰
In CodeIgniter we use, insert() function to insert data into database. Basic syntax: Here, we are using 3 files for insert data into MySQL:
Overview
insert('table_name', 'array_of_object');
Controller: User.php /app/Controllers/User.php Model: UserModel.php /app/Models/UserModel.php View: add.php /app/Views/add.php
Database Table user_info
CREATE TABLE `user_info` ( `id` INT NOT NULL AUTO_INCREMENT , `first_name` VARCHAR(255) NOT NULL , `last_name` VARCHAR(255) NOT NULL , `email` VARCHAR(255) NOT NULL , PRIMARY KEY (`id`) ) ENGINE = InnoDB;
/app/Config/Routes.php
$routes->add('user/add', 'User::add'); $routes->post('user/save', 'User::save');
/app/Views/add.php
<!DOCTYPE html> <html> <head> <title>Add user</title> </head> <body> <form method="post" action="<?php echo base_url('user/save'); ?>"> <table width="600" border="1" cellspacing="5" cellpadding="5"> <tr> <td width="230">First Name</td> <td width="329"> <input type="text" name="txtFirstName" /> </td> </tr> <tr> <td>Last Name</td> <td> <input type="text" name="txtLastName" /> </td> </tr> <tr> <td>Email ID</td> <td> <input type="email" name="txtEmail" /> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="btnAddUser" value="Add user" /> </td> </tr> </table> </form> </body> </html>
/app/Controllers/User.php
<?php namespace App\Controllers; use App\Models\UserModel; class User extends BaseController { public function __construct() { $db = db_connect(); $this->userModel = new UserModel($db); } public function add() { echo view('add'); } public function save() { $first_name = $this->request->getPost('txtLastName'); $last_name = $this->request->getPost('txtLastName'); $email = $this->request->getPost('txtEmail'); $data = [ 'first_name' => $first_name, 'last_name' => $last_name, 'email' => $email, ]; $result = $this->userModel->add($data); if($result) { echo "New user is registered successfully."; } else { echo "Something went wrong"; } } }
/app/Models/UserModel.php
<?php namespace App\Models; use CodeIgniter\Model; use CodeIgniter\Database\ConnectionInterface; class UserModel extends Model { protected $db; public function __construct(ConnectionInterface &$db) { $this->db =& $db; } function add($data) { return $this->db ->table('user_info') ->insert($data); } }
😉Subscribe and like for more videos:
https://www.youtube.com/@chiragstutorial
💛Don't forget to, 💘Follow, 💝Like, 💖Share 💙&, Comment
0 Comments
Leave a message