-
Notifications
You must be signed in to change notification settings - Fork 2
/
operate.php
160 lines (126 loc) · 4.62 KB
/
operate.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
<?php
include_once('config.php');
/*
* Load Data
*/
if (count($_GET) > 0) {
if ($_GET['type'] === 'load') {
$item = $_GET['item'] ? :'';
$orderby = $_GET['orderby'] ? :'id';
session_start();
$result = mysqli_query($conn, "SELECT * FROM phonesdirectory WHERE name LIKE '%$item%' OR phone LIKE '%$item%' WHERE `user_id` ".$_SESSION['user_id']." ORDER BY $orderby ASC");
$output = "";
while ($row = mysqli_fetch_array($result)) {
echo "<tr data-id(".$row['id'].")>
<td>" . $row['id'] . " <input type='checkbox' class='input-check' data-id=".$row['id']." name='checked' id='check' ></td>
<td>" . $row["name"] . "</td>
<td>" . $row['phone'] . "</td>
<td class='text-center'>
<button class='btn btn-sm btn-primary' data-id='" . $row['id'] . "' data-toggle='modal' id='edit'><i class='fas fa-user-edit'></i></button>
<button class='btn btn-sm btn-danger' data-id='" . $row['id'] . "' data-toggle='modal' id='delete'><i class='fas fa-user-times'></i></button>
</td></tr>";
}
}
}
/*
* Update Data
*/
if (count($_POST) > 0) {
if ($_POST['type'] == "update") {
$id = $_POST['id'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$query = "UPDATE `phonesdirectory` SET `name`='$name',`phone`='$phone' WHERE id=$id";
mysqli_query($conn, $query);
echo 'success';
mysqli_close($conn);
}
}
/*
* Create Data
*/
if (count($_POST) > 0) {
if ($_POST['type'] == "create") {
$name = $_POST['name'];
$phone = $_POST['phone'];
$query = "INSERT INTO `phonesdirectory`( `name`, `phone`)
VALUES ('$name','$phone')";
if (mysqli_query($conn, $query)) {
echo json_encode(array(
"statusCode" => 200
));
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
}
/*
* Delete Data
*/
if (count($_POST) > 0) {
if ($_POST['type'] == "delete") {
$id = $_POST['id'];
$query = "DELETE FROM `phonesdirectory` WHERE id=$id ";
if (mysqli_query($conn, $query)) {
echo $id;
} else {
echo "Error:: " . $query . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
}
/*
* Delete Checked Rows
*/
if (count($_POST) > 0) {
if ($_POST['type'] == "deleteChecked") {
$ids = $_POST['ids'];
foreach($ids as $id){
$query = "DELETE FROM `phonesdirectory` WHERE id=$id ";
mysqli_query($conn, $query) or die(mysqli_error($conn));
}
echo $id;
mysqli_close($conn);
}
}
if (count($_GET) > 0) {
if ($_GET['type'] == "edit") {
$id = $_GET['id'];
$query = "SELECT * FROM `phonesdirectory` WHERE id=$id ";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
echo '<form id="data_form">
<div class="form-group">
<label for="recipient-name" class="col-form-label" >Name</label>
<input type="text" required="" class="form-control" id="ename" value="'.$row['name'].'">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label" >Phone</label>
<input type="number" required="" min="100000000" max="100000000000" value="'.$row['phone'].'" class="form-control" id="ephone">
</div>
</form>';
mysqli_close($conn);
}
}
// /*
// * Search Data
// */
// if (count($_GET) > 0) {
// if ($_GET['type'] =="search") {
// $item = $_GET['item'];
// $query = "SELECT * FROM phonesdirectory WHERE name LIKE '%$item%' OR phone LIKE '%$item%'";
// $result = mysqli_query($conn, $query);
// while ($row = mysqli_fetch_array($result)) {
// echo "<tr>
// <td>" . $row['id'] . "</td><td>" . $row["name"] . "</td>
// <td>" . $row['phone'] . "</td>
// <td class='text-center'>
// <button class='btn btn-sm btn-primary' data-id='" . $row['id'] . "' data-toggle='modal' id='edit'>Edit</button>
// <button class='btn btn-sm btn-danger' data-id='" . $row['id'] . "' data-toggle='modal' id='delete'>Delete</button>
// </td></tr>";
// }
// mysqli_close($conn);
// }
// }
?>