-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
173 lines (153 loc) · 5.35 KB
/
api.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
172
173
<?php
$conn = new mysqli("meddev.rdsm6y2ok4ji834.rds.bj.baidubce.com","gsj","ranknow123","gsjdb");
if ($conn->connect_error){
die("Connection failed:".$conn->connect_error); //2017-11-23sfeffesefefs
}
if(!isset($_GET['do'])) {
exit(json_encode(['err'=>1, 'errmsg'=>'缺少参数do'])); //TODO jsfesfle
}
$getdo =$_GET['do'];
switch ($getdo){
case 'log in':
case 'login':
if(isset($_GET['username']) && isset($_GET['password'])){
$username= $_GET['username'];
$password= $_GET['password'];
if(empty($username) || empty($password)){ //
$errmsg5 = ['err'=>2, 'errmsg'=>'用户名或密码是空=null'];
echo json_encode($errmsg5);
exit();
}
$ret = login($username,$password);
echo json_encode($ret);
} else{
$errmsg = ['err'=>1, 'errmsg'=>'用户名或密码错误' ];
echo json_encode($errmsg);
}
break;
case 'register':
if(isset($_GET['username']) && isset($_GET['password'])){
$u = $_GET['username'];
$p = $_GET['password'];
if(empty($u)||empty($p)){
$errmsg3 = ['err'=>3, 'errmsg'=>'register_username/password is empty'];
echo json_encode($errmsg3);
exit();
}
$regport = register($u,$p);
echo json_encode($regport);
} else {
$errmsg2 = ['err' => 2, 'errmsg'=>'no username/password' ];
echo json_encode($errmsg2);
}
break;
case 'recordlist':
$ret = recordlist();
echo json_encode($ret);
break;
case 'add_comment':
session_start();
if(!isset($_SESSION['userid'])){
exit(json_encode(['err'=>3,'errmsg'=>'用户未登录']));
}
$userid_add = $_SESSION['userid'];
if(isset($_GET['title']) && isset($_GET['comment'])) {
$title = $_GET['title'];
$comment = $_GET['comment'];
if (empty($title) || empty($comment)) {
$errmsg6 = ['err' => 2, 'errmsg' => '标题或内容缺失'];
echo json_encode($errmsg6);
exit();
}
$add_comment1 = add_comment($title, $comment, $userid_add);
echo json_encode($add_comment1);
}else{
$errmsg7 = ['err'=>2,'errmsg'=>'无标题或内容'];
echo json_encode($errmsg7);
}
break;
default:
echo "{}";
}
function login($username,$password){
global $conn;
$sql = "Select * from login WHERE username='$username' ";
$res = $conn->query($sql);
if ($conn->affected_rows<=0) {
$ret= ['err'=>1, 'errmsg'=>'用户名未找到1'];
//alert($ret);
} else{
$row = $res->fetch_array();
if ($row['password'] == $password) {
$ret = ['err'=>0, 'errmsg'=>'success'];
session_start();
$_SESSION['userid']=$row['userid'];
$_SESSION['username']=$row['username'];
} else{
$ret= ['err'=>1, 'errmsg'=>'密码错误'];
}
}
return $ret;
}
function register($username_reg,$password_reg){
global $conn;
$sql2 = "Select * from login WHERE username='$username_reg' ";
//select 返回的是数据集
$res2 = $conn->query($sql2);
if($conn->affected_rows<=0) {
$sql3 = "INSERT INTO login (username,password)
VALUES ('$username_reg','$password_reg')";
$res3 = $conn->query($sql3);
//echo $res3+'1';
if($res3){
$ret= ['err'=>0, 'errmsg'=>'success'];
// echo $res3+'2';
} else {
$ret = ['err'=>1, 'errmsg'=>'注册失败'];
// echo $res3+'3';
}
} else {
$ret= ['err'=>1, 'errmsg'=>'用户名已存在'];
// echo $res3+'4';
}
return $ret;
}
function add_comment($title,$comment, $userid_add) {
global $conn;
$sql6= "INSERT INTO commentboard (title,comment,userid)
VALUES ('$title','$comment','$userid_add')";
$res6 = $conn-> query($sql6);
if($res6){
$ret = ['err'=>0, 'errmsg'=>'success'];
} else{
$ret = ['err'=>1, 'errmsg'=>'unable to insert into Mysql'];
}
return $ret;
}
function recordlist (){
/*$ret= ['err'=>1, 'errmsg'=>'用户名已存在'];
return $ret;*/
global $conn;
session_start();
if(!isset($_SESSION['userid'])){
return ['err'=>1, 'errmsg'=>'用户没登陆'];
}
$userid4= $_SESSION['userid'];
$sql4 = "SELECT * from commentboard WHERE userid =$userid4";
$res4 = $conn->query($sql4);
$records= [];
if ($conn->affected_rows>0) {
while ($row = $res4->fetch_array()){
$records[]=$row;
/*echo "<table>";
echo "<tr><td>ID: </td><td>".$row["ID"]."</td></tr>";
echo "<tr><td>名字: </td><td>".$row["name"]."</td></tr>";
echo "<tr><td>时间: </td><td>".$row["entrytime"]."</td></tr>";
echo "<tr><td>标题: </td><td>".$row["title"]."</td></tr>";
echo "</table>";
//echo "<br>"; */
}
}
$ret= ['err'=>0, 'data'=>['records'=>$records]];
return $ret;
}