-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
114 lines (100 loc) · 3.25 KB
/
index.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
<?php
header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin');
// $x = ['蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许', '何', '施', '张'];
// $y = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚'];
// function combineArray($arr1, $arr2)
// {
// $result = array();
// foreach ($arr1 as $item1) {
// foreach ($arr2 as $item2) {
// $result[] = $item1 . $item2;
// }
// }
// return $result;
// }
$servername = "localhost";
$username = "root";
$password = "daichongweb";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, 'test');
// 检测连接
if (!$conn) {
exit("Connection failed: " . mysqli_connect_error());
}
// $data = combineArray($x, $y);
// foreach ($data as $k => $v) {
// $age = (int) $k + 10;
// $home = $v . '村';
// $city = '杭州';
// $address = '杭州滨江';
// $sql = "INSERT INTO `user`(`name`,`age`,`home`,`city`,`address`) VALUE('{$v}','{$age}','{$home}','{$city}','{$address}')";
// $conn->query($sql);
// }
// exit;
$data = $_POST;
if ($data) {
$action = $data['action'];
$page = $data['page'] ?? 1;
$page_size = 10;
$limit = ($page - 1) * $page_size;
if ($action == 'list') {
$name = $data['name'];
$where = "1=1";
if ($name) {
$where .= " and name like '%$name%'";
}
$result = $conn->query("select * from user where {$where} limit $limit,$page_size");
while ($data[] = $result->fetch_assoc()) {}
$total = $conn->query("select count(*) as num from user where {$where}")->fetch_assoc()['num'];
$data = array_values($data);
foreach ($data as $key => $v) {
if (!is_array($v)) {
unset($data[$key]);
}
}
ajaxs(200, 'success', [
'total' => (int) $total,
'data' => array_values($data),
]);
} else if ($action == 'delUser') {
$id = $data['id'];
$result = $conn->query("delete from user where id = {$id}");
$code = 0;
$message = 'error';
if ($result) {
$code = 200;
$message = 'success';
}
ajaxs($code, $message);
} else if ($action == 'createUser') {
$data = $data['data'];
$name = $data['name'];
$city = $data['city'];
$age = $data['age'];
$home = $data['home'];
$date = $data['date'];
$address = $data['address'];
$result = $conn->query("insert into `user`(`name`,`city`,`age`,`home`,`date`,`address`) value('{$name}','{$city}','{$age}','{$home}','{$date}','{$address}')");
$code = 0;
$message = 'error';
if ($result) {
$code = 200;
$message = 'success';
}
ajaxs($code, $message);
}
} else {
ajaxs(0, 'error');
}
function ajaxs(int $code, string $message, array $data = [])
{
echo json_encode([
'code' => $code,
'message' => $message,
'data' => $data,
]);
}