-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate.php
171 lines (136 loc) · 5.11 KB
/
create.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$name = "";
$name_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate name
$input_did = trim($_POST["did"]);
$input_did_array = explode (",", $input_did);
$route_name_and_outcid = $_POST["route_name"];
$str_arr = explode (",", $route_name_and_outcid);
$route_name_input = $str_arr[0];
$route_cid_input = $str_arr[1];
if(empty($input_did)){
$name_err = "Please enter a valid DID number only.";
} elseif(!filter_var($input_did, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[0-9,]+$/")))){
$name_err = "Please enter a valid DID number only.";
} else{
$did = $input_did;
$route_name = $route_name_input;
$route_cid = $route_cid_input;
$size_of_did = sizeof($input_did_array);
}
// Check input errors before inserting in database
if(empty($name_err) ){
// Prepare an insert statement
if ($size_of_did === 1) {
$sql = "INSERT INTO did_numbers (did,route_name,route_cid) VALUES (?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $param_did, $param_route_name, $param_route_cid);
// Set parameters
$param_did = $did;
$param_route_name = $route_name;
$param_route_cid = $route_cid;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records created successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
else {
for ($i=0; $i < $size_of_did; $i++)
{
$sql = "INSERT INTO did_numbers (did,route_name,route_cid) VALUES (?, ?, ?)";
$input_did = $input_did_array[$i];
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $param_did, $param_route_name, $param_route_cid);
// Set parameters
$param_did = $input_did;
$param_route_name = $route_name;
$param_route_cid = $route_cid;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records created successfully. Redirect to landing page
header("location: index.php");
//exit();
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
}
mysqli_stmt_close($stmt);
}
}
// Close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Record</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
.wrapper{
width: 600px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2 class="mt-5">Create CallerID</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group">
<?php
$sql = "SELECT name, outcid FROM outbound_routes";
$result = $con->query($sql);
?>
<label>Route Name</label>
<select name="route_name" id="route_name">
<option>Select a route:</option>
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$route_cid_cup = $row['outcid'];
echo $route_cid_cup;
?>
<option value="<?php echo $row['name'].",".$row['outcid'];?>"><?php echo $row['name'];?></option>
<?php
//echo $row['name'].$row['outcid'];
}
}
?>
</select>
</div>
<div class="form-group">
<label>DID Number</label>
<input type="text" name="did" class="form-control <?php echo (!empty($name_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $did; ?>">
<span class="invalid-feedback"><?php echo $name_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>