您的位置:无忧脚本 » Rimifon的个人空间 » 日志
PHP+Access单文件文章管理系统
运行环境:Windows平台 + PHP5
第一次访问自动建立Access数据库,实现了分页、数据库压缩等。
所有查询语句全部采用了Access的“存储过程”。(使用了一个自己用Javascrīpt编写的数据库操作组件:D没想到Javascrīpt还可以用于PHP的服务器端脚本吧?)
演示:http://www.fengyun.org/Test/PHP/Article.php
Code:
第一次访问自动建立Access数据库,实现了分页、数据库压缩等。
所有查询语句全部采用了Access的“存储过程”。(使用了一个自己用Javascrīpt编写的数据库操作组件:D没想到Javascrīpt还可以用于PHP的服务器端脚本吧?)
演示:http://www.fengyun.org/Test/PHP/Article.php
<?php
Session_Start();
//====================
//作者:Rimifon
//网址:http://bbs.51js.com
//时间:2006.09
//联系:QQ5029111
//====================
$AdminPass="admin";
$DbPath=RealPath(".")."\\".SubStr(Base64_Encode(MD5($_ENV["COMPUTERNAME"])),0,8).".mdb";
//=====================
//初始化数据库操作对象
//=====================
Try
{
$DB= newCOM("scrīpt:http://www.zope.org/Members/Rimifon/DbHelper.sct#DataHelper");
}
Catch(Exception $Err)
{
Die("您的服务器没有权限从HTTP获取组件,请改为本地获取方式。行数:".$Err->GetLine());
}
$DB->ConnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source=".$DbPath;
if(!File_Exists($DbPath))InitDb();
//=====================
//初始化变量
//=====================
$MoKuai=$_GET["MoKuai"];
$IsAdmin=$_SESSION["Access"]=="Admin";
If($_GET["MoKuai"] =="Login"&&$IsAdmin)
{
Session_Unset("Access");
$IsAdmin=0;
}
//=====================
//后台处理
//=====================
If($_SERVER["REQUEST_METHOD"] =="POST")
{
switch($MoKuai)
{
case"Login": # 处理登录
if($_POST["AdminPass"] ==$AdminPass)
{
$_SESSION["Access"] ="Admin";
Header("Location:?MoKuai=FenLei");
}
else
{
$Info="密码错误";
}
break;
case"FenLei": # 分类维护
If(!$IsAdmin)
{
Header("Location:?");
Die();
}
switch($_POST["CaoZuo"])
{
case"添加分类":
$DB->NonQuery("TianJiaFenLei '".SqlVar("MingCheng")."',".(SqlVar("PaiXu")*1));
$Info="添加了一个分类";
break;
case"删除":
$DB->NonQuery("ShanChuFenLei ".(SqlVar("ID")*1));
$Info="删除了该分类和该分类下的文章";
break;
default:
$DB->NonQuery("XiuGaiFenLei ".(SqlVar("ID")*1).",'".SqlVar("MingCheng")."',".(SqlVar("PaiXu")*1));
$Info="分类修改成功";
break;
}
break;
case"BianJi": # 文章编辑
$IsEdit=SqlVar("ID") *1;
$Rs=$DB->RecordSet();
$Rs->Open("select 1");
$Rs->Close();
$Cmd=$Rs->ActiveCommand;
$Cmd->CommandType=4;
$Cmd->CommandText=$IsEdit?"XiuGaiWenZhang":"TianJiaWenZhang";
if($IsEdit)$Cmd->Parameters->Append($Cmd->CreateParameter("@ID",3,1,10,$IsEdit));
$Cmd->Parameters->Append($Cmd->CreateParameter("@BiaoTi",200,1,254,SqlVar("BiaoTi",0)));
$Cmd->Parameters->Append($Cmd->CreateParameter("@FenLei",3,1,10,SqlVar("FenLei")*1));
$Cmd->Parameters->Append($Cmd->CreateParameter("@ZuoZhe",200,1,50,SqlVar("ZuoZhe",0)));
$Cmd->Parameters->Append($Cmd->CreateParameter("@NeiRong",203,1, -1,SqlVar("NeiRong",0)));
$Cmd->Execute();
$Rs->ActiveConnection->Close();
Header("Location:?MoKuai=LieBiao");
Die();
break;
case"LieBiao":
For($C=0;$C<Count($_POST["ID"]);$C++)$DB->NonQuery("ShanChuWenZhang ".($_POST["ID"][$C]*1));
$Info="完成".Count($_POST["ID"])."条数据的删除操作";
break;
default:
break;
}
}
$FenLeiArr=$DB->DataSet("MoKuaiLieBiao");
functionSqlVar($Arg,$Need=1)
{
$Arg=Str_Replace("\\\\","\\",HtmlSpecialChars($_POST[$Arg]));
$Arg=Str_Replace("\\'","'",$Arg);
If($Need)$Arg=Str_Replace("'","''",$Arg);
returnStr_Replace("\\"",""",$Arg);
}
//=====================
//查询文章分类
//=====================
functionShowFenLei()
{
Global$FenLeiArr;
For($C=0;$C<$FenLeiArr->length;$C++)
{
$FenLei=$FenLei."<a href=\"?FenLei=".$FenLeiArr->{$C}->ID.
"\">".$FenLeiArr->{$C}->MingCheng."</a>\r\n";
If($C<$FenLeiArr->length-1)$FenLei=$FenLei."| ";
}
return$FenLei;
}
//======================
//第一次执行建立数据库
//======================
functionInitDb()
{
Global$DB,$Info;
$DB->CreateAccess();
$DB->NonQuery("create table MoKuai(ID counter primary key, MingCheng varchar(50), PaiXu int)");
$DB->NonQuery("create table WenZhang(ID counter, BiaoTi varchar(254),
FenLei int,NeiRong memo,ZuoZhe varchar(50), LiuLan int not null default 0,
ShiJian DateTime not null default now,
ConsTraint FK_FenLei Foreign Key (FenLei) References MoKuai On Delete CasCade)");
$DB->NonQuery("create procedure MoKuaiLieBiao as
(select ID,MingCheng,PaiXu from MoKuai order by PaiXu)");
$DB->NonQuery("create procedure TianJiaFenLei(@MingCheng varchar(50),@PaiXu int) as
(insert into MoKuai(MingCheng, PaiXu) Values(@MingCheng, @PaiXu))");
$DB->NonQuery("create procedure ShanChuFenLei(@BH int) as
(delete from MoKuai where ID=@BH)");
$DB->NonQuery("create procedure XiuGaiFenLei(@BH int,@MC varchar(50),@PX int) as
(update MoKuai set MingCheng=@MC,PaiXu=@PX where ID=@BH)");
$DB->NonQuery("create procedure TianJiaWenZhang(@BT varchar(254),@FL int,@ZZ varchar(50),@NR memo) as
(insert into WenZhang(BiaoTi,FenLei,ZuoZhe,NeiRong) Values(@BT,@FL,@ZZ,@NR))");
$DB->NonQuery("create procedure XiuGaiWenZhang(@BH int,@BT varchar(254),@FL int,@ZZ varchar(50),@NR memo) as
(update WenZhang set BiaoTi=@BT,FenLei=@FL,ZuoZhe=@ZZ,NeiRong=@NR where ID=@BH)");
$DB->NonQuery("create procedure WenZhangLieBiao as (select A.ID,A.BiaoTi,B.MingCheng,A.ZuoZhe,A.LiuLan,A.ShiJian
from WenZhang A,MoKuai B where A.FenLei=B.ID order by A.ShiJian desc)");
$DB->NonQuery("create procedure ShanChuWenZhang (@BH int) as
(delete from WenZhang where ID=@BH)");
$DB->NonQuery("create procedure DuQuWenZhang(@BH int) as (select A.ID,A.BiaoTi,A.FenLei,B.MingCheng,A.ZuoZhe,
A.LiuLan,A.NeiRong,A.ShiJian from WenZhang A,MoKuai B where A.ID=@BH and B.ID=A.FenLei)");
$DB->NonQuery("create procedure FangWenZengJia(@BH int) as
(update WenZhang set LiuLan=LiuLan+1 where ID=@BH)");
$DB->NonQuery("create procedure FenLeiLieBiao(@BH int) as (select A.ID,A.BiaoTi,B.MingCheng,A.ZuoZhe,A.LiuLan,A.ShiJian
from WenZhang A,MoKuai B where A.FenLei=B.ID and B.ID=@BH order by A.ShiJian desc)");
$Info="成功建立了数据库";
}?><html><head><title>風雲工作室文章系统(PHP+Access)</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
</head><body>
<!-- Your Page Header Here -->
<?php
If($IsAdmin&&$_GET["MoKuai"]!="")
{
Echo <<<HTML
<div><b>管理选项:</b>
<a href="?MoKuai=FenLei">分类维护</a>
| <a href="?MoKuai=LieBiao">文章列表</a>
| <a href="?MoKuai=BianJi">文章录入</a>
| <a href="?MoKuai=YaSuo">数据压缩</a>
| <a href="?">返回首页</a>
| <a href="?MoKuai=Login">退出登录</a></div>
HTML;}
else
{
$ShowFenLei=ShowFenLei();
$LoginParam=$IsAdmin?"FenLei":"Login";
Echo <<<HTML
<div><b>导 航 栏:</b>$ShowFenLei
| <a href="?">全部文章</a>
| <a href="?MoKuai=$LoginParam">后台管理</a></div>
HTML;}?><hr>
<div align="center"><font color="red"><?=$Info?></font></div>
<?php
switch($MoKuai)
{
//======================
//登录页面
//======================
case"Login":
Echo <<<HTML
<div align="center">
<form method="post">请输入管理密码:
<input type="password" name="AdminPass">
<input type="submit" value="登录">
</form>
</div>
HTML; break;
//======================
//文章分类管理
//======================
case"FenLei":
Echo <<<HTML
<table><tr><th>名称</th><th>排序</th><th>操作</th></tr>
<tr><form method="post"><input type="hidden" name="ID" value="0">
<td><input size=50 name="MingCheng"></td>
<td><input size=3 name="PaiXu"></td>
<td><input type=submit value="添加分类" name="CaoZuo"></td>
</form></tr>
HTML;For($C=0;$C<$FenLeiArr->length;$C++)
{?><tr><form method="post"><input type="hidden" name="ID" value="<?=$FenLeiArr->{$C}->ID?>">
<td><input size=50 value="<?=$FenLeiArr->{$C}->MingCheng?>" name="MingCheng"></td>
<td><input size=3 value="<?=$FenLeiArr->{$C}->PaiXu?>" name="PaiXu"></td>
<td><input type=submit value="修改" name="CaoZuo">
<input type=submit value="删除" name="CaoZuo"></td>
</form></tr><?}?></table>
<? break;
//======================
//文章列表管理
//======================
case"LieBiao":?>
<table border><caption>文章列表</caption>
<tr><th>编号</th><th>标题</th><th>分类</th><th>作者</th><th>浏览</th><th>操作</th></tr>
<form method="post"><?php
$Ds=$DB->DataSet("WenZhangLieBiao",25,$_GET["Page"]);
For($C=0;$C<$Ds->length;$C++)
{?><tr>
<td><?=$Ds->{$C}->ID?></td>
<td><a href="?MoKuai=BianJi&ID=<?=$Ds->{$C}->ID?>"><?=$Ds->{$C}->BiaoTi?$Ds->{$C}->BiaoTi:"无标题"?></a></td>
<td><?=$Ds->{$C}->MingCheng?></td>
<td><?=$Ds->{$C}->ZuoZhe?></td>
<td><?=$Ds->{$C}->LiuLan?></td>
<td><input type="checkbox" name="ID[]" value="<?=$Ds->{$C}->ID?>"></td>
</tr><?}?>
<tr><td colspan="5">共<?=$Ds->PageCount?>页,当前为第<?=$Ds->AbsolutePage?>页。
<a href="?MoKuai=LieBiao">首页</a> <a href="?MoKuai=LieBiao&Page=<?=$Ds->AbsolutePage-1?>">上页</a>
<a href="?MoKuai=LieBiao&Page=<?=$Ds->AbsolutePage+1?>">下页</a> <a href="?MoKuai=LieBiao&Page=<?=$Ds->PageCount?>">末页</a></td>
<td><input type="submit" value="删除"></td></tr>
</form></table>
<? break;
//======================
//文章编辑界面
//======================
case"BianJi":
$GetID=$_GET["ID"]*1;
If($GetID)
{
$Ds=$DB->DataSet("DuQuWenZhang $GetID");
$ZuoZhe=$Ds->{0}->ZuoZhe;
$BiaoTi=$Ds->{0}->BiaoTi;
$NeiRong=$Ds->{0}->NeiRong;
$FenLei=$Ds->{0}->FenLei;
}
If($FenLeiArr->length)
{
Echo <<<HTML
<table border><caption>文章编辑界面</caption>
<form method="post"><input type="hidden" name="ID" value="$GetID">
<tr><th>标题</th><td><input name="BiaoTi" size="50" value="$BiaoTi"></td></tr>
<tr><th>分类</th><td><select name="FenLei">
HTML;For($C=0;$C<$FenLeiArr->length;$C++)
{
Echo"<option value=\x22".$FenLeiArr->{$C}->ID."\x22".($FenLei==$FenLeiArr->{$C}->ID?" selected":"").">".$FenLeiArr->{$C}->MingCheng."</option>";
}
Echo <<<HTML
</select></td></tr>
<tr><th>作者</th><td><input name="ZuoZhe" value="$ZuoZhe"></td></tr>
<tr><th>内容</th><td><textarea name="NeiRong" cols=50 rows=13>$NeiRong</textarea></td></tr>
<tr><th colspan="2"><input type="submit" value="提交"></th></tr>
</form></table>
HTML; }
Else
{
Echo"目前没有分类,请先添加分类";
}
break;
case"YaSuo":
If($IsAdmin)
{
$Info=$DB->CompactAccess();
Echo$Info?$Info:"数据库压缩成功";
}
break;
//======================
//默认页面
//======================
default:
$GetID=$_GET["ID"]*1;
If($GetID)
{
$DB->NonQuery("FangWenZengJia $GetID");
$Ds=$DB->DataSet("DuQuWenZhang $GetID");
If(!$Ds->length) Echo"文章不存在";
Else
{?>
<table border cellpadding="20"><tr><td>
<a href="?FenLei=<?=$Ds->{0}->FenLei?>"><?=$Ds->{0}->MingCheng?></a>
-<?=$Ds->{0}->BiaoTi?><h3 align="center"><?=$Ds->{0}->BiaoTi?></h3>
<div align="center">浏览次数:<?=$Ds->{0}->LiuLan?></div>
<p><?=Str_Replace(" "," ",Str_Replace("\r\n","<br>\r\n",$Ds->{0}->NeiRong))?></p>
<div align="right">作者:<?=$Ds->{0}->ZuoZhe?><br>发表时间:<?=$Ds->{0}->ShiJian?></div>
</td></tr></table>
<?}
}
Else
{
$GetFenLei=$_GET["FenLei"]*1;?>
<table border><caption>文章列表</caption>
<tr><th>编号</th><th>标题</th><th>分类</th><th>作者</th><th>浏览</th></tr>
<?php
$Ds=$DB->DataSet($GetFenLei?"FenLeiLieBiao ".($GetFenLei*1):"WenZhangLieBiao",25,$_GET[
TAG:
