What is CRUD (Create, Read, Update, Delete)?
The term CRUD is strongly connected with managing digital data. To be more precise, CRUD refers to an acronym of four fundamental operators of persistent database applications:
- Create (create a dataset)
- Read or Retrieve (read datasets)
- Update (update datasets)
- Delete or Destroy (destroy datasets)
Simply put, the term CRUD sums up the functions that users need in order to create and manage data. Whether your task involves managing databases or using applications, there are many data management processes based on CRUD, and often these operations are specifically tailored to both the user as well as the system in question. Following this, operations are indispensable access tools that enable experts to inspect database problems. For users, CRUD means creating an account (create), which can be used (read), adjusted (updated), or deleted at any time. Depending on the language environment, CRUD operations are executed quite differently, as the following table shows:
CRUD Operations | SQL | RESTful HTTP | XQuery |
Create | INSERT | POST, PUT | insert |
Read | SELECT | GET, HEAD | copy/modify/return |
Update | UPDATE | PUT, PATCH | replace, rename |
Delete | DELETE | DELETE | delete |
- Free website protection with SSL Wildcard included
- Free private registration for greater privacy
- Free 2 GB email account
CRUD framework: access layer for databases
CRUD grids or CRUD frameworks refer to individual data objects that can be visualised with the help of graphical user interfaces and changed through the said CRUD operations. Normally, these user interfaces involve HTML interfaces. A CRUD framework requires multiple transactional steps so that data isn’t saved merely upon it being entered and instead first requires users to press the ‘Save’ or ‘Continue’ button in order to carry out such steps. CRUD framework operations don’t need to be executed within strict intervals, meaning that datasets are available to users during this period of time. Multi-users systems especially profit from this set-up, as many different individuals are able to simultaneously read out the same data set.
In order to implement these operations, so-called persistence layers are used, which are usually contained in the form of modules in the framework. These itemise the relational, tabular dataset representations and present these on an object-oriented layer. Through optimised access to the used database, CRUD frameworks facilitate both the development as well as the use of applications. The CRUD system offers many different frameworks and these are based on a variety of different languages and platforms. Some examples have been laid out for you here in the following table:
Language or platform | Framework |
Java | JDBC (The Java Database Connectivity), Hibernate, JBoss Seam, Isis |
PHP | Yii, CakePHP, Zikula, Symfony, TYPO3 Flow |
Perl | Catalyst, Gantry |
Python | Django, SQLAlchemy, web2py |
Groovy | Grails |
.NET | NHibernate, ADO.NET/Entity Framework |
Ruby | Ruby on Rails |
JavaScript | Backbone.js, AngularJS |
How to develop a CRUD PHP grid for your database
In the following paragraphs, we’ll show you how to create a Bootstrap interface for the widespread database system, MySQL. This type of interface enables access to the dataset via CRUD operations. Before starting, the create operator should already be set up. In order to be able to manipulate the database table, the server-side script language PHP is used together with the extension, PHP Data Objects.
1. The first step involves creating a simple database table, which can then later be manipulated per CRUD access at a later point during this tutorial. To this end, import the following example table into your MySQL database:
CREATE TABLE `customers` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 100 ) NOT NULL ,
`email` VARCHAR( 100 ) NOT NULL ,
`mobile` VARCHAR( 100 ) NOT NULL
) ENGINE = INNODB;
2. The next step involves adjusting the connection set-up and release to the database. Create a PHP file with the name, database.php, and ad the following script with the class ‘database’ in order to manage the database connection:
<?php
class Database
{
private static $dbName = 'database name';
private static $dbHost = 'localhost';
private static $dbUsername = 'user name';
private static $dbUserPassword = 'password';
private static $cont = zero;
public function __construct() {
die('Init-function not allowed');
}
public static function connect() {
// Connection only allowed during access time
if ( null == self::$cont )
{
try
{
self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect()
{
self::$cont =zero;
}
}
3. The code generates the HTML form where the individual specifications regarding names, cell phones, and e-mail addresses can be made. An additional PHP variable is also integrated. This is responsible for generating error messages in combination with the following code (this is entered into the create.php before it’s entered in the HTML code) provided that the affected entry field remains free:
<?php
require 'database.php';
if ( !empty($_POST)) {
// record validation error
$nameError = zero;
$emailError = zero;
$mobileError = zerol;
// record entry values
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
// validate entry
$valid = true;
if (empty($name)) {
$nameError = 'Please provide a name';
$valid = false;
}
if (empty($email)) {
$emailError = 'Please provide an e-mail address';
$valid = false;
} else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$emailError = 'Please provide a valid e-mail address';
$valid = false;
}
if (empty($mobile)) {
$mobileError = 'Please provide a phone number';
$valid = false;
}
// Daten eingeben
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO customers (name,email,mobile) values(?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($name,$email,$mobile));
Database::disconnect();
header("Location: index.php");
}
}
?>
At this point, a create.php page has been set-up. This is called up by pressing the create button and enables user information to be entered. The script makes sure all of the entered data as well as the validation errors are recorded, that error messages appear when the wrong entry is given, and that the data is forwarded to a declared database. You can find out more on how to create and use the other CRUD operations, read, update, and delete by reading a tutorial on the matter. Here, you can find out more about the create operation and how to create a Bootstrap grid.