-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_device.php
64 lines (48 loc) · 1.25 KB
/
create_device.php
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
57
58
59
60
61
62
63
64
<?php require('inc/checklogin.php'); ?>
<?php require('inc/connect.php'); ?>
<?php require('inc/function.php');?>
<?php
admin_only();
$page_title = "Add User";
?>
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
header('Location: http://localhost/dashboard/');
die();
}
$errors = array();
if (empty($_POST['name'])) {
$errors[] = 'Name is required.';
}
elseif (strlen($_POST['name']) < 3) {
$errors[] ='Name is too short.';
}
if (empty($_POST['mac_address'])) {
$errors[] = 'mac_address is required.';
}
elseif (strlen($_POST['mac_address']) != 17) {
$errors[] = 'Mac Address is invalid.';
}
if (empty($_POST['user_id'])) {
$errors[] = 'User Id is required.';
}
elseif ($_POST['user_id'] < 1) {
$errors[] = 'User Id invalid.';
}
elseif (!get_user_by_id($_POST['user_id'])) {
$errors[] = 'User Id invalid.';
}
foreach ($errors as $error) {
echo '<li>' . $error. '</li>';
}
if (!empty($errors)) {
echo 'Please correct the errors';
die();
}
$name = addslashes($_POST['name']);
$mac_add = $_POST['mac_address'];
$user_id = $_POST['user_id'];
$insert = $conn->query("INSERT INTO devices (name, mac_add, user_id) VALUES('$name', '$mac_add', '$user_id')");
if (!$insert)
die('database error');
header('Location: http://localhost/dashboard/');