-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_record.php
More file actions
47 lines (35 loc) · 1.16 KB
/
Copy pathdelete_record.php
File metadata and controls
47 lines (35 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
declare(strict_types=1);
/**
* Deletes one row.
*
* POST only and CSRF protected: the previous version deleted on a plain GET,
* which any link prefetch could trigger.
*/
require __DIR__ . '/lib/bootstrap.php';
use DbManager\Page;
use DbManager\Schema;
[$pdo, $database, $table] = Page::table();
$columns = Schema::columns($pdo, $table);
$primaryKey = Schema::primaryKey($columns);
$listUrl = url('view_table_data.php', ['db' => $database, 'table' => $table]);
if ($primaryKey === []) {
Page::fail(__('data.no_pk'), $listUrl);
}
Page::handle(
static function () use ($pdo, $table, $primaryKey): string {
$submitted = is_array($_POST['key'] ?? null) ? $_POST['key'] : [];
$key = [];
foreach ($primaryKey as $column) {
if (!array_key_exists($column, $submitted)) {
throw new RuntimeException(__('error.row_key_missing'));
}
$key[$column] = (string) $submitted[$column];
}
if (Schema::delete($pdo, $table, $key) === 0) {
throw new RuntimeException(__('error.record_not_found'));
}
return __('data.deleted');
},
$listUrl
);