php手机论坛程序,网站开发-php开发手机论坛(8)-编辑帖子
上一篇說了怎么查看帖子,這一篇就來說說怎么把數據庫里面的帖子提取出來編輯.
代碼邏輯是這樣的:
先驗證是否是當前用戶的帖子,是的話就提供編輯按鈕,這個在view.php中實現.
->在數據庫中根據get的a來提取已有帖子數據,加載到模板文件中.
->最后把得到數據更新到數據庫里面,再跳轉到查看帖子
edit.php
include('conn.php');
include('lib.php');
include('ChromePhp.php');
session_start();
// Chromephp::log($_SESSION['uid']);
// Chromephp::log($_SESSION['name']);
//如果會話沒有被設置,查看是否設置了cookie
if(!isset($_SESSION['uid']))
{
if(isset($_COOKIE['uid'])&&isset($_COOKIE['name']))
{
//用cookie給session賦值
$_SESSION['uid']=$_COOKIE['uid'];
$_SESSION['name']=$_COOKIE['name'];
}
else
{
header("Location: login.php");
exit();
}
}
//獲取分類目錄
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
$aid=$_GET['a'];
$sql="select id, name from categories";
$query=mysql_query($sql);
while ($node=mysql_fetch_assoc($query)) {
$category[$node['id']]=$node['name'];
}
//提取已有的帖子內容
$query=mysql_query("select * from articles where id='$aid'");
$article=mysql_fetch_array($query);
ChromePhp::log($article);
include("templates/edit.php");
}
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
//header("Location: index.php") ;
//print_r($category);
$aid=$_GET['a'];
$title=format($_POST['title']);
$content=format($_POST['content']);
$classic=format($_POST['classic']);
//ChromePhp::log($classic);
//更新分類文章數量
$sql="update categories set articles = articles+1 where name = '$classic'";
$query=mysql_query($sql);
//ChromePhp::log($query);
$time=time();
//ChromePhp::log($aid);
//上傳圖片
if ($_FILES['img']['error'] ==0 && ($_FILES['img']['type']=="image/gif"
|| $_FILES['img']['type']=="image/jpeg" || $_FILES['img']['type']=="image/pjpeg"
|| $_FILES['img']['type']=="image/png"
))
{
//判斷錯誤代碼,=0則上傳成功,!=0則上傳失敗
//處理上傳過程
// ChromePhp::log("開始上傳");
$img = $_FILES['img'];
//header("Location: templates/error.php") ;
//exit;
//拼接文件路徑
$path ='upload/'.mk_dir().'/'.randName(). '.' .getExt($img['name']);
// ChromePhp::log($path);
//移動
if(move_uploaded_file($img['tmp_name'],$path)) {
mysql_query("update articles set title='$title', content='$content', category='$classic', edittime='$time', img_path='$path' where id='$aid'");
header("Location: view.php?a=".$aid);
exit();
} else{
ChromePhp::log("上傳失敗");
}
}else
{
mysql_query("update articles set title='$title', content='$content', category='$classic', edittime='$time', img_path='$path' where id='$aid'");
// $url = "http://blog.csdn.net/abandonship";
// echo "
// echo "window.location.href='$url'";
// echo "";
header("Location: view.php?a=".$aid);
exit();
// ChromePhp::log($_FILES['img']['error']);
}
}
?>templates/edit.php
返回
編輯逗貼
enctype="multipart/form-data" data-ajax="false" >'; ?>
標題
>
內容
請選擇分類:
foreach ($category as $id => $name) {
echo '',$name,'
';
}
?>
圖片:
總結
以上是生活随笔為你收集整理的php手机论坛程序,网站开发-php开发手机论坛(8)-编辑帖子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PID控制器开发笔记之三:抗积分饱和PI
- 下一篇: 动态规划算法php,php算法学习之动态