MYSQL-TOOLS

Comparison of graphical tools for MySQL

Home Blog MySQL CRUD MySQL in PHP

CRUD MySQL in PHP

Печать PDF
Рейтинг пользователей: / 0
ХудшийЛучший 

CRUD — (create read update delete) abbreviation naming four basic functions for working with databases - create, read, update and delete.

Examples

OperationMySQL statment HTTP operation
Create INSERT POST
Read SELECT GET
Update UPDATE PUT or PATCH
Delete DELETE DELETE

 

The classic application to work with the database is usually called CRUD - on the first letter of standard operations, Create, read, update and delete.

It's like the notes in music - any application consists of these basic elements. In our case it is a PHP web page that displays a list of records in the database as a list of (R), allows you to add new records in the MySQL database (C), edit them (U) and delete (D) from database.

With the help of this php application class is implemented through a few stitches.

Here they are:

 

<?

include 'safemysql.class.php';
$db = new SafeMysql();
$table = "test"

if(
$_SERVER['REQUEST_METHOD']=='POST') {
if (isset(
$_POST['delete'])) {
$db->query("DELETE FROM ?n WHERE id=?i",$table,$_POST['delete']);
} elseif (
$_POST['id']) { 
$db->query("UPDATE ?n SET name=?s WHERE id=?i",$table,$_POST['name'],$_POST['id']);
} else { 
$db->query("INSERT INTO ?n SET name=?s",$table,$_POST['name']);

header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);  
exit;  
}  
if (!isset(
$_GET['id'])) {
$LIST = $db->getAll("SELECT * FROM ?n",$table);
include
'list.php'
} else {
if (
$_GET['id']) {
$row = $db->getRow("SELECT * FROM ?n WHERE id=?i",$table,$_GET['id']);
foreach (
$row as $k => $v) $row[$k]=htmlspecialchars($v); 
} else { 
$row['name']=''
$row['id']=0

include
'form.php'
}

 

Template files:
list.php
<a href="/?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="/?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>


form.php
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="/?">Return to the list</a>
</form>
<? if ($row['id']):?>
<div align=right>
<form method="POST">
<input type="hidden" name="delete" value="<?=$row['id']?>">
<input type="submit" value="Удалить"><br>
</form>
</div>
<?endif?>