-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
56 lines (46 loc) · 2.04 KB
/
process.php
File metadata and controls
56 lines (46 loc) · 2.04 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
48
49
50
51
52
53
54
55
56
<?php session_start();
if (isset($_POST['save'])) {
$type = $_POST['type'];
$sn_textarea = $_POST['sn'];
mysqli_report(MYSQLI_REPORT_ALL);
try {
if (!preg_match("/^\d+$/", $type)) {
throw new Exception('Выберите тип оборудования из выпадающего списка');
}
if (empty($sn_textarea)) {
throw new Exception('Введите серийные номера');
}
$mysqli = new mysqli('localhost', 'mysql', 'mysql', 'test_task1');
$equipment_type_mask = $mysqli->query("SELECT mask FROM equipment_types WHERE id = '$type'");
$mask = $equipment_type_mask->fetch_assoc()['mask'];
$mask_expr = '/' . preg_replace(['/N/', '/Z/', '/A/', '/a/', '/X/'], ['[0-9]', '[-_@]', '[A-Z]', '[a-z]','[0-9A-Z]'], $mask) . '/';
$sn_arr=explode("\n", $sn_textarea);
$wrong_sn = '';
$has_wrong_sn = false;
foreach ($sn_arr as $sn) {
if (preg_match($mask_expr, $sn)) {
try {
$mysqli->query("INSERT INTO equipments (type, sn) VALUES('$type', '$sn')");
} catch (Exception $e) {
$wrong_sn .= $sn . ', ';
}
} else {
$wrong_sn .= $sn . ', ';
$has_wrong_sn = true;
}
}
if ($has_wrong_sn) {
throw new Exception("Серийные номера '$wrong_sn' не соответствуют маске '$mask' или уже существуют");
} else {
$message = "Все записи успешно сохранены!";
$msg_type = 'success';
}
} catch (Exception $e) {
$message = $e->getMessage();
$msg_type = 'danger';
} finally {
$_SESSION['message'] = $message;
$_SESSION['msg_type'] = $msg_type;
header("location: index.php");
}
}