How To Update A Single Field In Codeigniter
Information is the bloodline of near applications. The information needs to be stored in such a manner that it tin be further analyzed to provide insights and facilitate business decisions. The information is usually stored in the database. Among the main concerns when interacting with the database is security, ease of access and database vendor specific implementations of Structured Query Language (SQL).
Active tape is a pattern pattern that makes it easy to interact with the database in ease, secure and eloquent way.
The agile record has the following advantages
- Insert, update and delete records with simple method bondage of active record
- Submits the user input in a secure way using parameters
- Allows you to work with multiple database engines such as MySQL, SQL Server, etc. without rewriting the application code
- CodeIgniter uses drivers specific for each database engine in the background.
In this tutorial, you will learn:
- How to use Active Tape: Example
- CodeIgniter Database Configuration
- CodeIgniter Insert Agile Record
- CodeIgniter Select Active Tape
- CodeIgniter Update Active Record
- CodeIgniter Delete Active Record
How to use Active Record: Example
In this tutorial, we will talk over the tutorial database. Nosotros will have ii tables, one with orders the other with details.
This tutorial assumes yous have MySQL database installed up and running.
Run the post-obit scripts to create tutorial database:
CREATE SCHEMA ci_active_record; USE ci_active_record; CREATE TABLE `order_details` ( `id` int(11) NOT NULL AUTO_INCREMENT, `order_id` int(xi) DEFAULT Goose egg, `detail` varchar(245) DEFAULT NULL, `quantity` int(eleven) DEFAULT '0', `price` decimal(ten,2) DEFAULT '0.00', `sub_total` decimal(10,two) DEFAULT '0.00', PRIMARY Fundamental (`id`) ) ENGINE=InnoDB AUTO_INCREMENT = 1; CREATE TABLE `orders` ( `id` int(11) Not NULL AUTO_INCREMENT, `appointment` timestamp NULL DEFAULT NULL, `customer_name` varchar(245) DEFAULT Null, `customer_address` varchar(245) DEFAULT Cypher, Chief Primal (`id`) ) ENGINE=InnoDB AUTO_INCREMENT = 1;
The to a higher place code creates a database named ci_active_record and creates two tables namely orders and order_details. The relationship between the two tables is defined by the cavalcade id in orders and order_id in order_details table.
CodeIgniter Database Configuration
Nosotros will now configure our application to be able to communicate with this database.
Open database configuration file located in awarding/config/database.php
locate the following lines in the configuration file
'hostname' => 'localhost', 'username' => '', 'password' => '', 'database' => '',
Update the above lawmaking to the following
'hostname' => 'localhost', 'username' => 'root', 'password' => 'letmein', 'database' => 'ci_active_record',
Note: you will need to replace the username and password to the ones that match your configuration of MySQL.
In addition to the database configuration details, nosotros besides need to tell CodeIgniter to load the database library when information technology loads
Pace i) Open the post-obit file awarding/config/autoload.php
Step 2) Locate the $autoload array key libraries and load the database library as shown below
$autoload['libraries'] = array('database');
Here,
- The above code loads the database library when the application starts
CodeIgniter Insert Agile Record
For testing purposes, we will create a controller and defined routes that we will be using to interact with our awarding via active tape.
Create a new file application/controllers/ActiveRecordController.php
Add the post-obit code to ActiveRecordController.php
<?php defined('BASEPATH') OR leave('No direct script access immune'); class ActiveRecordController extends CI_Controller { public part store_order(){ $data = [ 'date' => '2018-12-19', 'customer_name' => 'Joe Thomas', 'customer_address' => 'United states' ]; $this->db->insert('orders', $information); echo 'order has successfully been created'; } }
Hither,
- $data = […] defines an array variable data that uses database table names equally array keys and assigns values to them
- $this->db->insert('orders', $data); calls the insert method of the database library, passes in the table proper noun orders and the assortment variable $data equally the parameter. This line generates the SQL INSERT statement using the array keys as the field names and the assortment values as the values to exist inserted into the database.
Now that we have successfully created the controller method for active record, we will at present need to create a route that nosotros will telephone call to execute the controller method.
Now open routes.php in awarding/config/routes.php
add the following line to the routes
$road['ar/insert'] = 'activerecordcontroller/store_order';
Here,
- We define a road ar/insert that calls the store_order of the ActiveRecordController.
Permit's now start the web server to exam our method.
Run the following command to first the born server for PHP
cd C:\Sites\ci-app php -S localhost:3000
HERE,
- The above command browser to the command line and start the built-in server at port 3000.
Load the following URL into your browser
http://localhost:3000/ar/insert
You lot will get the following results
lodge has successfully been created
Open the MySQL tutorial database and cheque the orders table
You volition able to newly created row every bit shown in the image below
CodeIgniter Select Active Record
In this section, nosotros will meet how to read the records that we have in the database and brandish them in the web browser as an unordered list
Add the following method to the ActiveRecordController
public role alphabetize() { $query = $this->db->get('orders'); repeat "<h3>Orders List</h3>"; echo "<ul>"; foreach ($query->result() equally $row) { echo "<li>$row->customer_name</li>"; } repeat "</ul>"; }
HERE,
- $query = $this->db->go('orders'); runs the select query against the orders tabular array selecting all the fields
- repeat "<h4>Orders List</h4>"; displays a HTML heading of size 3
- echo "<ul>"; prints the opening tag for un-ordered HTML list
- foreach ($query->consequence() as $row) {…} used the for loop to loop through the results returned from the database. echo "<li>$row->customer_name</li>"; prints the customer_name from the database
Before you load the following URL, you can load a couple more records to the database.
Lets at present define a route for the SELECT query
Open up application/config/routes.php table
Add the following route
$route['ar'] = 'activerecordcontroller';
Here,
- The route ar points to the index method of ActiveRecordController class. This is past default that's why nosotros didn't specify the index method as you did for the route that inserts records
Assuming the spider web server is already running, load the following URL
http://localhost:3000/ar
You should exist able to see results which is very much like to the following in your web browser
CodeIgniter Update Active Record
In this department, we volition tal about how to employ the active record to update the database. Allow'southward say we want to update the customer name Joe Thomas to Joe.
Add together the following method to ActiveRecordController class
public office update_order() { $data = [ 'customer_name' => 'Joe', ]; $this->db->where('id', 1); $this->db->update('orders', $data); echo 'order has successfully been updated'; }
Hither,
- $data = […] defines the fields and values that we wish to update in the database table
- $this->db->where('id', i); sets the where clause of the update query
- $this->db->update('orders', $information); generates the SQL update query and executes it against our database.
The to a higher place lawmaking will produce the following SQL argument
UPDATE orders Prepare customer_name = 'Joe' WHERE id = 1;
Permit's at present update the routes.php awarding/config/routes.php
Add together the following road
$route['ar/update'] = 'activerecordcontroller/update_order';
Save the changes
Load the post-obit URL in the spider web browser
Permit'southward now display the database records and see if the changes have been affected.
Every bit yous tin run across from the above-given Image, the start record has been updated from Joe Thomas to Joe.
CodeIgniter Delete Active Record
Nosotros volition now delete a record from the database. Nosotros will delete the record with the id of three.
Add the post-obit method to the ActiveRecordController
public part delete_order() { $this->db->where('id', 3); $this->db->delete('orders'); echo 'lodge has successfully been deleted'; }
Hither,
- $this->db->where('id', 1); sets the where clause
- $this->db->delete('orders'); deletes the database row in orders tabular array based on the criteria set up using the where clause.
To execute the above code, load the following URL in our web browser
http://localhost:3000/ar/delete
Summary
In this tutorial, you have learned how to work with an active record to insert, update, delete and select records from the database. We worked with static values to create records, update and delete. In the next tutorial, we volition create a user interface that the user can apply to create records dynamically in the database.
Source: https://www.guru99.com/codeigniter-active-record.html
Posted by: perezboading73.blogspot.com
0 Response to "How To Update A Single Field In Codeigniter"
Post a Comment