1. PHP開發環境與表單 1-1 post.php 1-2 index.php 2. 邏輯判斷及樣板引擎 2-1 index.php 2-2 post.php 2-3 index_tpl.html 3. MySQL的資料存取 3-1 index.php 3-2 post.php 3-3 post_tpl.html 3-4 eznews.sql 4. PHP函數與引入 4-1 post.php 4-2 index.php 4-3 list_tpl.html 4-4 function.php 5. 編輯與刪除資料 5-1 index.php 5-2 list_tpl.html 5-3 post.php 5-4 config.php 5-5 function.php 5-6 post_tpl.html 6. BootStrap各式應用 6-1 post.php 6-2 list_tpl.html 6-3 post_tpl.html 6-4 index.php 7. 置頂、計數器與 join 7-1 post.php 7-2 post_tpl.html 7-3 index.php 7-4 list_tpl.html 7-5 function.php 7-6 eznews.sql 8. 上傳與分頁 8-1 post.php 8-2 index.php 8-3 config.php 8-4 post_tpl.html 8-5 eznews.sql 9. 身份認證機制 9-1 eznews.sql 9-2 post_tpl.html 9-3 list_tpl.html 9-4 config.php 9-5 index.php 9-6 post.php
9-6
post.php
004
require_once
"config.php"
;
005
require_once
"function.php"
;
008
$op
=isset(
$_REQUEST
[
'op'
])?
$_REQUEST
[
'op'
]:
""
;
009
$sn
=isset(
$_REQUEST
[
'sn'
])?
intval
(
$_REQUEST
[
'sn'
]):
""
;
010
$toolbar
=
"<a href='index.php?g2p={$g2p}' class='btn btn-success btn-block'><i class='fa fa-home'></i> 新聞列表</a>"
;
018
header(
"location:post.php"
);
022
$error_msg
=error_msg(
"資料庫連線錯誤!!請檢查資料庫帳號、密碼是否正確!"
);
027
header(
"location:index.php"
);
033
header(
"location:index.php?op=view&sn={$sn}"
);
038
$main
=news_form(
$sn
);
048
show_page(
'post_tpl'
);
055
function
news_form(
$sn
=
""
){
056
if
(!isset(
$_SESSION
[
'uid'
])
or
empty
(
$_SESSION
[
'uid'
])){
057
return
"<div class='alert alert-danger'>請先登入</div>"
;
063
$sql
=
"select * from eznews where sn='$sn'"
;
066
$result
=mysql_query(
$sql
)
or
die
(
"{$sql}<br>"
.mysql_error());
067
$news
=mysql_fetch_assoc(
$result
);
072
$news_title
=
$news
[
'news_title'
];
073
$news_content
=
$news
[
'news_content'
];
075
$author
=
$news
[
'author'
];
076
$post_time
=
$news
[
'post_time'
];
077
$def_cate_sn
=
$news
[
'cate_sn'
];
082
$sql
=
"select * from eznews_files where sn={$sn}"
;
083
$result
=mysql_query(
$sql
)
or
die
(
"{$sql}<br>"
.mysql_error());
084
$files_list
=
"請選擇欲刪除檔案:"
;
085
while
(
$file
=mysql_fetch_assoc(
$result
)){
088
<input type=
'checkbox'
name=
'del_files[{$file['
file_sn
']}]'
value=
'{$file['
file_new_name
']}'
>
089
<a href=
'uploads/{$file['
file_new_name
']}'
target=
'_blank'
>{
$file
[
'file_name'
]}</a>
097
$ip
=
$_SERVER
[
'REMOTE_ADDR'
];
098
$author
=
$_SESSION
[
'uname'
];
099
$post_time
=
date
(
"Y-m-d H:i:s"
);
108
$sql
=
"select * from eznews_cate"
;
109
$result
=mysql_query(
$sql
)
or
die
(
$sql
.
'<br>'
.mysql_error());
111
while
(list(
$cate_sn
,
$cate_title
)=mysql_fetch_row(
$result
)){
112
$selected
=(
$cate_sn
==
$def_cate_sn
)?
"selected"
:
""
;
113
$cate_options
.=
"<option value='$cate_sn' $selected>$cate_title</option>"
;
116
$checked_default
=(isset(
$news
[
'status'
])
and
$news
[
'status'
]==
""
)?
"checked"
:
""
;
117
$checked_top
=(isset(
$news
[
'status'
])
and
$news
[
'status'
]==
"置頂"
)?
"checked"
:
""
;
118
$checked_important
=(isset(
$news
[
'status'
])
and
$news
[
'status'
]==
"高亮"
)?
"checked"
:
""
;
121
<form action=
"post.php"
method=
"post"
role=
"form"
enctype=
"multipart/form-data"
>
124
<div
class
=
"col-md-7"
>
125
<div
class
=
"form-group"
>
126
<input type=
"text"
name=
"news_title"
placeholder=
"請輸入新聞標題"
class
=
"form-control"
value=
"$news_title"
>
130
<div
class
=
"col-md-3"
>
131
<div
class
=
"form-group"
>
133
<label
class
=
"radio-inline"
>
134
<input type=
"radio"
name=
"status"
value=
""
$checked_default
>正常
137
<label
class
=
"radio-inline"
>
138
<input type=
"radio"
name=
"status"
value=
"置頂"
$checked_top
>置頂
141
<label
class
=
"radio-inline"
>
142
<input type=
"radio"
name=
"status"
value=
"高亮"
$checked_important
>高亮
150
<div
class
=
"col-md-2"
>
151
<div
class
=
"form-group"
>
152
<input type=
"text"
name=
"author"
placeholder=
"請輸入發布者"
class
=
"form-control"
value=
"$author"
>
159
<div
class
=
"form-group"
>
160
<textarea id=
"summernote"
name=
"news_content"
style=
"height:300px;"
placeholder=
"請輸入新聞內容"
class
=
"form-control"
>
$news_content
</textarea>
164
<div
class
=
"col-md-3"
>
165
<select name=
"cate_sn"
class
=
"form-control"
>
166
<option value=
""
>請選擇分類</option>
170
<div
class
=
"col-md-3"
>
171
<input type=
"text"
name=
"cate_title"
class
=
"form-control"
placeholder=
"請輸入新分類"
>
173
<div
class
=
"col-md-3"
>
174
<div
class
=
"form-group"
>
175
<input type=
"text"
name=
"post_time"
id=
"datetimepicker"
placeholder=
"請輸入發布日期"
class
=
"form-control"
value=
"$post_time"
>
178
<div
class
=
"col-md-3"
>
180
<input type=
"hidden"
name=
"ip"
value=
"$ip"
>
181
<input type=
"hidden"
name=
"sn"
value=
"$sn"
>
182
<input type=
"hidden"
name=
"op"
value=
"$next_op"
>
183
<a href=
"post.php?op=clear"
class
=
"btn btn-danger"
>清除</a>
184
<input type=
"submit"
value=
"儲存"
class
=
"btn btn-info"
>
189
<div
class
=
"col-md-3"
>
190
<input type=
"file"
name=
"files[]"
multiple>
192
<div
class
=
"col-md-9"
>
203
function
save_news(
$sn
=
""
){
204
if
(!isset(
$_SESSION
[
'uid'
])
or
empty
(
$_SESSION
[
'uid'
])){
209
$op
=isset(
$_POST
[
'op'
])?
$_POST
[
'op'
]:
""
;
210
$password
=isset(
$_POST
[
'password'
])?
$_POST
[
'password'
]:
""
;
211
$news_title
=isset(
$_POST
[
'news_title'
])?check_input(
$_POST
[
'news_title'
]):
""
;
212
$news_content
=isset(
$_POST
[
'news_content'
])?check_input(
$_POST
[
'news_content'
]):
""
;
213
$author
=isset(
$_POST
[
'author'
])?check_input(
$_POST
[
'author'
]):
""
;
214
$ip
=isset(
$_POST
[
'ip'
])?check_input(
$_POST
[
'ip'
]):
""
;
215
$post_time
=isset(
$_POST
[
'post_time'
])?check_input(
$_POST
[
'post_time'
]):
""
;
216
$status
=isset(
$_POST
[
'status'
])?check_input(
$_POST
[
'status'
]):
""
;
217
$cate_title
=isset(
$_POST
[
'cate_title'
])?check_input(
$_POST
[
'cate_title'
]):
""
;
218
$cate_sn
=isset(
$_POST
[
'cate_sn'
])?
intval
(
$_POST
[
'cate_sn'
]):
""
;
223
if
(
empty
(
$cate_sn
)
and
!
empty
(
$cate_title
)){
224
$sql
=
"insert into eznews_cate (cate_title) values('$cate_title')"
;
225
mysql_query(
$sql
)
or
die
(
$sql
.
"<br>"
.mysql_error());
226
$cate_sn
=mysql_insert_id();
227
}
elseif
(!
empty
(
$cate_sn
)
and
!
empty
(
$cate_title
)){
229
$sql
=
"update eznews_cate set cate_title='$cate_title' where cate_sn='$cate_sn'"
;
230
mysql_query(
$sql
)
or
die
(
$sql
.
"<br>"
.mysql_error());
235
$sql
=
"update eznews set cate_sn='$cate_sn', news_title='$news_title', news_content='$news_content', ip='$ip', author='$author', post_time='$post_time', status='$status' where sn='$sn' and uid='{$_SESSION['uid']}'"
;
236
mysql_query(
$sql
)
or
die
(
$sql
.
"<br>"
.mysql_error());
238
$sql
=
"insert into eznews (cate_sn,news_title, news_content, ip, author, uid, post_time,status) values('$cate_sn','$news_title', '$news_content', '$ip', '$author', '{$_SESSION['uid']}','$post_time', '$status')"
;
239
mysql_query(
$sql
)
or
die
(
$sql
.
"<br>"
.mysql_error());
240
$sn
=mysql_insert_id();
244
if
(isset(
$_POST
[
'del_files'
])){
245
foreach
(
$_POST
[
'del_files'
]
as
$file_new_name
) {
247
if
(unlink(
"uploads/{$file_new_name}"
)){
249
$sql
=
"delete from eznews_files where file_new_name='$file_new_name'"
;
250
mysql_query(
$sql
)
or
die
(
$sql
.
"<br>"
.mysql_error());
259
if
(!
is_dir
(
'uploads'
)){
263
foreach
(
$_FILES
[
'files'
][
'tmp_name'
]
as
$i
=>
$tmp_name
){
264
$ext
=
pathinfo
(
$_FILES
[
'files'
][
'name'
][
$i
], PATHINFO_EXTENSION);
265
$new
=
substr
(md5(
$_FILES
[
'files'
][
'name'
][
$i
]), -5);
266
$new_name
=
"uploads/{$sn}_{$new}.{$ext}"
;
268
if
(move_uploaded_file(
$tmp_name
,
$new_name
)){
269
$sql
=
"insert into eznews_files ( sn, file_name, file_size, file_type, file_new_name) values('$sn','{$_FILES['files']['name'][$i]}', '{$_FILES['files']['size'][$i]}}', '{$_FILES['files']['type'][$i]}', '{$sn}_{$new}.{$ext}')"
;
270
mysql_query(
$sql
)
or
die
(
$sql
.
"<br>"
.mysql_error());
278
function
check_input(
$value
){
279
if
(!get_magic_quotes_gpc()){
280
$value
=
addslashes
(
$value
);
1. PHP開發環境與表單 1-1 post.php 1-2 index.php 2. 邏輯判斷及樣板引擎 2-1 index.php 2-2 post.php 2-3 index_tpl.html 3. MySQL的資料存取 3-1 index.php 3-2 post.php 3-3 post_tpl.html 3-4 eznews.sql 4. PHP函數與引入 4-1 post.php 4-2 index.php 4-3 list_tpl.html 4-4 function.php 5. 編輯與刪除資料 5-1 index.php 5-2 list_tpl.html 5-3 post.php 5-4 config.php 5-5 function.php 5-6 post_tpl.html 6. BootStrap各式應用 6-1 post.php 6-2 list_tpl.html 6-3 post_tpl.html 6-4 index.php 7. 置頂、計數器與 join 7-1 post.php 7-2 post_tpl.html 7-3 index.php 7-4 list_tpl.html 7-5 function.php 7-6 eznews.sql 8. 上傳與分頁 8-1 post.php 8-2 index.php 8-3 config.php 8-4 post_tpl.html 8-5 eznews.sql 9. 身份認證機制 9-1 eznews.sql 9-2 post_tpl.html 9-3 list_tpl.html 9-4 config.php 9-5 index.php 9-6 post.php