Repository navigation
Expand file tree
/
Copy pathinsert.php
More file actions
110 lines (96 loc) · 4.68 KB
/
Copy pathinsert.php
File metadata and controls
110 lines (96 loc) · 4.68 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
$server = 'sql.rde.hull.ac.uk';
$connectionInfo = array("Database"=>"rde_554754");
$conn = sqlsrv_connect($server,$connectionInfo);
//define empty starter variables
$useridErr = $firstnameErr = $surnameErr = $userTypeErr = $locationErr = "";
$userid = $firstname = $surname = $userType = $location = "";
if (empty($_POST["userID"])) {
$useridErr = "User ID is Required and cannot be longer than 6 numbers";
} else {
$userid = entry_data($_POST["userID"]);
if (!preg_match("/^((?!(0))[0-9]{6})$/", $userid)) {
$useridErr = "Only Numbers Allowed";
}
if (strlen($userid) != 6)
{
$useridErr = "The ID cannot exceed 6 numbers";
}
if (($userid) == 0)
{
$useridErr = "Cannot be 0";
}
}
if (empty($_POST["firstname"])) {
$firstnameErr = "First Name is Required";
}
else {
$firstname = entry_data($_POST["firstname"]);
if (!preg_match("/^[a-zA-Z ]*$/", $firstname)) {
$firstnameErr = "Only letters and White space allowed";
}
if (strlen($firstname) >= 19) {
$firstnameErr = "First Name can't be longer than 20 characters";
}
}
if (empty($_POST["surname"]))
{
$surnameErr = "Surname is Required";
}
else {
$surname = entry_data($_POST["surname"]);
if (!preg_match("/^[a-zA-Z ]*$/", $surname)) {
$surnameErr = "Surname is Required";
}
if (strlen($surname) >= 19) {
$surnameErr = "Surname can't be longer than 20 characters";
}
}
if (empty($_POST["userType"]))
{
$userTypeErr = "A User Type selection is Required";
}
else {
$userType = entry_data($_POST["userType"]);
}
if (empty($_POST["location"]))
{
$locationErr = "A Location selection is Required";
}
else {
$location = entry_data($_POST["location"]);
}
function entry_data($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//define Existing checker to make sure the ID exists first
$ExistingUser = "SELECT UserID FROM locationTable WHERE UserID LIKE '$userid'";
$fetchExistingUser = sqlsrv_query($conn, $ExistingUser);
$ExistingUserID = sqlsrv_fetch_array($fetchExistingUser);
if(($useridErr =="") && ($firstnameErr =="") && ($surnameErr =="") && $userTypeErr == "" && $locationErr =="")
{
if ($ExistingUserID['UserID'] == $userid)
{
$deniedmessage = "Denied, User Exists";
echo "<script type='text/javascript'>alert('$deniedmessage');</script>";
}
else {
$confirmmessage = "Success, User created";
echo "<script type='text/javascript'>alert('$confirmmessage');</script>";
$insert_query = "INSERT INTO locationTable (UserID, FirstName, Surname, UserType, currentLocation, currentDate)
VALUES (?, ?, ?, ?, ?, SYSDATETIME())";
$params = array($userid, $firstname, $surname, $userType, $location);
}
$result = sqlsrv_query($conn,$insert_query, $params);
}
else {
$errormessage = "Error in entry, check your entry and try again";
echo "<script type='text/javascript'>alert('$errormessage');</script>";
}
sqlsrv_close($conn);
header("refresh:0 url=index.php");
die();
?>