-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartical.php
More file actions
184 lines (164 loc) · 5.08 KB
/
artical.php
File metadata and controls
184 lines (164 loc) · 5.08 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
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
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Created by PhpStorm.
* User: longer
* Date: 2017/5/12
* Time: 15:09
*/
include './utils.php';
$mysql_con = new mysqli('localhost:3306','root','1994woaini');
if(!$mysql_con){
die(toJsonString(-3,"服务器出错",array()));
}
if(!$mysql_con->query("use yuewen")){
die(toJsonString(-8,"服务器出错",array()));
}
$mysql_con->query("set names 'utf8'");
switch ($_POST['method']){
case 'newartical':
uploadArtical();
break;
case 'getarticallist':
getArticalList();
break;
case 'savedraft':
saveDraft();
break;
case 'getdraftlist':
getDraftList();
break;
default:
//die(toJsonString(-9,"缺少必须的字段"));
test();
}
function test(){
global $mysql_con;
$data = array();
$result = $mysql_con->query("select u.uname,u.uicon,a.*
from artical a
INNER JOIN userinfo u
on a.uid = u.uid");
while(($data_fetch=$result->fetch_assoc())!=null){
$data[] = $data_fetch;
}
die(toJsonString(0,'查询成功',$data));
}
function uploadArtical(){
$did = isset($_POST['did'])?$_POST['did']:false;
$uid = isset($_POST['userid'])?$_POST['userid']:false;
if($uid===false){
die(toJsonString(-10,'用户未登录'));
}
if(!isset($_POST['title'])||!isset($_POST['content'])){
die(toJsonString(-10,'缺少必须的字段'));
}
$uid = (int)$uid;
$title = $_POST['title'];
$dscp = isset($_POST['desc'])?$_POST['desc']:null;
$content = $_POST['content'];
$time = time();
$sql_insert =
"insert into artical(uid,atitle,adscp,acontent,atime)
values
('$uid','$title','$dscp','$content','$time')";
global $mysql_con;
$result = $mysql_con->query($sql_insert);
if(!$result){
die(toJsonString(-1,"上传失败",array()));
}else{
echo(toJsonString(0,'上传成功',array()));
if(!$did){
die();
}
$mysql_del_draft = "delete from draft WHERE aid = '$did'";
$del_result = $mysql_con->query($mysql_del_draft);
if(!$del_result){
//产生了垃圾数据。
}
}
}
/*
* params:uid,nouserinfo,
*/
function getArticalList(){
$query_statment = "select u.uname,u.uicon,a.*
from artical a
INNER JOIN userinfo u
on a.uid = u.uid";
global $mysql_con;
$uid = isset($_POST['uid'])?$_POST['uid']:false;
if($uid){
$query_statment = "select u.uname,u.uicon,a.*
from artical a
INNER JOIN userinfo u
on a.uid = u.uid and u.uid = '$uid'";
}
$result = $mysql_con->query($query_statment);
$data = array();
if(!$result){
die(toJsonString(-2,'查询失败'));
}
while(($data_fetch=$result->fetch_assoc())!=null){
$data[] = $data_fetch;
}
echo toJsonString(0,'查询成功',$data);
}
function saveDraft(){
global $mysql_con;
$dtitle = isset($_POST['title'])?$_POST['title']:false;
if(!$dtitle){
die(toJsonString(-10,'缺少标题'));
}
$dcontent = isset($_POST['content'])?$_POST['content']:false;
if(!$dcontent){
die(toJsonString(-10,'缺少文章内容'));
}
$uid = isset($_POST['uid'])?$_POST['uid']:false;
if(!$uid){
die(toJsonString(-10,'缺少用户id'));
}
$ddesc = isset($_POST['desc'])?$_POST['desc']:null;
$aid = isset($_POST['aid'])?$_POST['aid']:false;
$dtime = time();
$mysql_isexist = "select aid from draft WHERE aid = '$aid'";
$mysql_update = "update draft set atitle = '$dtitle',adscp = '$ddesc',atime = '$dtime',acontent = '$dcontent'
WHERE aid = $aid";
$mysql_insert = "insert into draft (uid, atitle, adscp, acontent,atime)
VALUE ('$uid','$dtitle','$ddesc','$dcontent','$dtime')";
$mysql_result = false;
if(!$aid){//第一次保存草稿,直接插入
$mysql_result = $mysql_con->query($mysql_insert);
}else{//已经存在草稿,更新数据。
$mysql_result = $mysql_con->query($mysql_update);
if(!$mysql_result){
die(toJsonString(-2,'服务器错误'));
}else{
if(($mysql_result->num_rows)>0){
$mysql_result = $mysql_con->query($mysql_update);
}
}
}
if(!$mysql_result){
die(toJsonString(-1,'保存草稿失败'));
}else{
die(toJsonString(0,'保存成功'));
}
}
function getDraftList(){
global $mysql_con;
$uid = isset($_POST['uid'])?$_POST['uid']:false;
if(!$uid){
die(toJsonString(-10,'缺少用户id'));
}
$query_statement = "select * from draft WHERE uid = $uid";
$result = $mysql_con->query($query_statement);
if(!$result){
die(toJsonString(-2,'获取数据失败'));
}
$data = array();
while(($data1=$result->fetch_assoc())!=null){
$data[] = $data1;
}
die(toJsonString(0,'获取成功',$data));
}
?>