biyuan 发表于 2006-8-27 21:11:16

【原创】奉献给新手—PHP+ACCESS一页文章管理程序

花了一天的时间,写了一个PHP操作ACCESS的演示整站示例程序(不包括分页和二级分类):

<?php
session_start();
//==========================
//作者:biyuan
//网址:http://bbs.51js.com
//时间:2006.07
//联系:QQ6010707
//==========================
?>
<html>
<head>
<title>碧原网络PHP+ACCESS文章管理系统</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<style type="text/css">
body {
        margin:0px;
        text-align:center;
        font-size:12px;
        font-family:verdana;
}
td {
        font-size:12px;
        height:28px;
        padding:4px;
        font-family:verdana;
}
A:link,
A:visited,
A:active {
        color: #005a17; text-decoration: none;
}
A:hover {
        color: #ff5a00; text-decoration: underline;
}
</style>
</head>
<body>
<?php
//==================================
//连接数据库
//==================================       
@$conn = new com("ADODB.Connection");
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("article.mdb"));

//==================================
//文章导航
//==================================
function article_menu(){
        global $conn;
        @$sql = "Select * From List";
        @$rs  = $conn->Execute($sql);
        while(!$rs->Eof){
                echo "|&nbsp;<a href=\"?classid=" . $rs->Fields->value . "\">" . $rs->Fields->value . "</a>&nbsp;";
                $rs->MoveNext();
        }
        echo "|&nbsp;<a href=\"?admin=login\">后台管理</a>\r\n";
        $rs->Close();
        if(@$_SESSION["ARTICLEADMIN"] == "BY_PHP_ARTICLE"){
                echo "<br />管理选项->&nbsp;|&nbsp;<a href=\"?admin=class\">分类管理</a>&nbsp;|&nbsp;<a href=\"?admin=insert\">发表文章</a>&nbsp;|&nbsp;<a href=\"?admin=list\">文章列表</a>&nbsp;|&nbsp;<a href=\"?admin=logout\">退出登陆</a>";
        }
}
//==================================
//文章列表:article_list(文章分类ID)
//==================================
function article_list($str){
        global $conn;
        @$sql = "Select id,by_title,by_name,by_time,by_count From Article Where by_class = " . $str . " Order By id desc";
        @$rs  = $conn->Execute($sql);
        if(!$rs->Eof){
                echo "<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"#cccccc\">\r\n"
                         . "        <tr bgcolor=\"#F3F3F3\" align=\"center\">\r\n"
                         . "                <td>文章标题</td><td>文章作者</td><td>发表时间</td><td>浏览次数</td>\r\n"
                         . "        </tr>\r\n";

                while(!$rs->Eof){
                        echo "        <tr bgcolor=\"#FFFFFF\">\r\n"
                                 . "                <td><a href=\"?artid=" . $rs->Fields->value . "\">" . $rs->Fields->value . "</a></td><td>" . $rs->Fields->value . "</td><td>" . date("Y-m-d H:i:s",$rs->Fields->value) . "</td><td>" . $rs->Fields->value . "</td>\r\n"
                                 . "        </tr>\r\n";
                        $rs->MoveNext();
                }
                echo "</table>\r\n";
        }
        else {
                echo "<br />该分类不存在或暂时没有任何文章!";
        }
        $rs->Close();
}

//==================================
//文章详细:article_look(文章ID)
//==================================
function article_look($str){
        global $conn;
        @$sql = "Select * From Article Where id = " . $str;
        @$rs  = $conn->Execute($sql);
        @$usql= "Update Article Set by_count = (by_count + 1) Where id = " . $str;
        $conn->Execute($usql);
        if(!$rs->Eof){
                @$body = $rs->Fields->value;
                $body  = str_replace("\\","",$body);
                echo "<table border=\"0\" width=\"60%\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"#cccccc\">\r\n"
                         . "        <tr bgcolor=\"#F3F3F3\" align=\"center\">\r\n"
                         . "                <td style=\"font-size:14px;font-weight:bold\">" . $rs->Fields->value . "</td>\r\n"
                         . "        </tr>\r\n"
                         . "        <tr bgcolor=\"#F3F3F3\" align=\"center\">\r\n"
                         . "                <td>文章作者:" . $rs->Fields->value . "&nbsp;|&nbsp;发表时间:" . date("Y-m-d H:i:s",$rs->Fields->value) . "&nbsp;|&nbsp;浏览次数:" . $rs->Fields->value . "</td>\r\n"
                         . "        </tr>\r\n"
                         . "        <tr bgcolor=\"#FFFFFF\">\r\n"
                         . "                <td>" . $body . "</td>\r\n"
                         . "        </tr>\r\n"
                         . "        <tr bgcolor=\"#F3F3F3\">\r\n"
                         . "                <td align=\"right\">【 <a href=\"javascript:history.back();\">返 回</a> 】&nbsp;&nbsp;【 <a href=\"javascript:print();\">打 印</a> 】&nbsp;&nbsp;【 <a href=\"javascript:close();\">关 闭</a> 】</td>\r\n"
                         . "        </tr>\r\n"
                         . "        </table>\r\n"
                         . "        <script language=\"JavaScript\">document.title += \"--" . $rs->Fields->value . "\";</script>\r\n";
        }
        else {
                echo "<br />该文章不存在或已被管理员删除!";
        }
        $rs->Close();
}

//==================================
//后台:分类管理
//==================================
function admin_class(){
        global $conn;
        @$sql = "Select * From List";
        @$rs  = $conn->Execute($sql);
        echo "<form action=\"\" method=\"post\" name=\"frm\">\r\n";
        while(!$rs->Eof){
                echo "类别:<input type=\"text\" value=\"" . $rs->Fields->value . "\" name=\"class_" . $rs->Fields->value . "\" />&nbsp;编号:<input type=\"text\" value=\"" . $rs->Fields->value . "\" name=\"class_cid_" . $rs->Fields->value . "\" />&nbsp;<input type=\"button\" value=\"修改\" onclick=\"frm.action='?admin=class&class=edit&id=" . $rs->Fields->value . "';submit();\" />&nbsp;<input type=\"button\" value=\"删除\" onclick=\"frm.action='?admin=class&class=del&id=" . $rs->Fields->value . "';submit();\" /><br />\r\n";
                $rs->MoveNext();
        }
        echo "类别:<input type=\"text\" name=\"class_name\" />&nbsp;编号:<input type=\"text\" name=\"class_cid\" />&nbsp;<input type=\"button\" value=\"增加\" onclick=\"frm.action='?admin=class&class=add';submit();\" />&nbsp;<input type=\"button\" value=\"闲置\" disabled />\r\n"
                 . "</form>\r\n";

        //增加一个类别
        if(@$_GET["class"] == "add"){
                if(@$_POST["class_name"] == "" || @$_POST["class_cid"] == ""){
                        echo "<br />填写不完整!";
                        echo "<script language=\"JavaScript\">setTimeout(\"history.back();\", 2000);</script>";
                        exit();
                }
                else {
                        @$sql = "Insert into List(by_list,by_cid) Values('" . $_POST["class_name"] . "' , '" . $_POST["class_cid"] . "')";
                        $conn->Execute($sql);
                        echo "<br />添加成功!";
                        echo "<script language=\"JavaScript\">setTimeout(\"window.location.href='?admin=class';\", 2000);</script>";
                }
        }

        //修改一个类别
        if(@$_GET["class"] == "edit"){
                if(@$_POST["class_" . $_GET["id"]] == "" || @$_POST["class_cid_" . $_GET["id"]] == ""){
                        echo "<br />填写不完整!";
                        echo "<script language=\"JavaScript\">setTimeout(\"history.back();\", 2000);</script>";
                        exit();
                }
                else {
                        @$sql = "Update List Set by_list = '" . $_POST["class_" . $_GET["id"]] . "' , by_cid = '" . $_POST["class_cid_" . $_GET["id"]] . "' Where id = " . $_GET["id"];
                        $conn->Execute($sql);
                        echo "<br />修改成功!";
                        echo "<script language=\"JavaScript\">setTimeout(\"window.location.href='?admin=class';\", 2000);</script>";
                }
        }

        //删除一个类别
        if(@$_GET["class"] == "del"){
                @$sql = "Delete From List Where id = " . $_GET["id"];
                $conn->Execute($sql);
                echo "<br />删除成功!";
                echo "<script language=\"JavaScript\">setTimeout(\"window.location.href='?admin=class';\", 100);</script>";
        }
}

//==================================
//后台:发表文章(支持HTML)
//==================================
function admin_insert(){
        global $conn;
        if(@$_GET["innert"] == "add"){
                if(@$_POST["title"] == "" || @$_POST["name"] == "" || @$_POST["class"] == "" || @$_POST["body"] == ""){
                        echo "<br />填写不完整!";
                        echo "<script language=\"JavaScript\">setTimeout(\"history.back();\", 2000);</script>";
                        exit();
                }
                else {
                        @$body = $_POST["body"];
                        $body = str_replace("'", "''", $body);
                        $body = str_replace("\r\n", "<br />", $body);
                        @$sql = "Insert into article(by_title,by_name,by_time,by_count,by_body,by_class) Values('" . $_POST["title"] . "' , '" . $_POST["name"] . "' , #" . date("Y-m-d G:i:s") . "# , 0 , '" . $body . "' , '" . $_POST["class"] . "')";
                        $conn->Execute($sql);
                        echo "<br />添加成功!";
                        echo "<script language=\"JavaScript\">setTimeout(\"window.location.href='?admin=insert';\", 2000);</script>";
                }
        }
        else {
                echo "<div style=\"width:590px;text-align:left\">\r\n"
                         . "<form action=\"?admin=insert&innert=add\" method=\"post\">\r\n"
                         . "文章标题:<input type=\"text\" name=\"title\" /><br />\r\n"
                         . "文章作者:<input type=\"text\" name=\"name\" /><br />\r\n"
                         . "文章类别:<select name=\"class\">\r\n";
                @$sql = "Select * From List";
                @$rs  = $conn->Execute($sql);
                while(!$rs->Eof){
                        echo "<option value=\"" . $rs->Fields->value . "\">" . $rs->Fields->value . "</option>\r\n";
                        $rs->MoveNext();
                }
                echo "</select><br />\r\n"
                         . "文章正文:<textarea cols=\"70\" rows=\"14\" name=\"body\"></textarea><br />\r\n"
                         . "<center><input type=\"submit\" value=\"提交\" />&nbsp;<input type=\"reset\" value=\"重置\" /></center>\r\n"
                         . "</form>\r\n"
                         . "</div>\r\n";
        }
}

//==================================
//后台:文章列表
//==================================
function admin_list(){
        global $conn;
        @$sql = "Select id,by_title,by_name,by_time,by_count,by_class From Article Order By id desc";
        @$rs  = $conn->Execute($sql);
        if(!$rs->Eof){
                echo "<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"#cccccc\">\r\n"
                         . "        <tr bgcolor=\"#F3F3F3\" align=\"center\">\r\n"
                         . "                <td>文章标题</td><td>文章分类<td>文章作者</td><td>发表时间</td><td>浏览次数</td><td>操作</td>\r\n"
                         . "        </tr>\r\n";
                while(!$rs->Eof){
                        @$sql = "Select * From List Where by_cid = " . $rs->Fields->value;
                        @$crs = $conn->Execute($sql);
                        @$cid = $crs->Fields->value;
                        echo "        <tr bgcolor=\"#FFFFFF\">\r\n"
                                 . "                <td><a href=\"?artid=" . $rs->Fields->value . "\">" . $rs->Fields->value . "</a></td><td>" . $cid . "</td><td>" . $rs->Fields->value . "</td><td>" . $rs->Fields->value . "</td><td>" . $rs->Fields->value . "</td><td><a href=\"?admin=list&list=edit&id=" . $rs->Fields->value . "\">编辑</a>&nbsp;|&nbsp;<a href=\"?admin=list&list=del&id=" . $rs->Fields->value . "\">删除</a></td>\r\n"
                                 . "        </tr>\r\n";
                        $crs->Close();
                        $rs->MoveNext();
                }
                echo "</table>\r\n";
        }
        else {
                echo "<br />该分类不存在或暂时没有任何文章!";
        }
        $rs->Close();

        //修改文章
        if(@$_GET["list"] == "edit"){
                if(@$_GET["edit"] == "true"){
                        if(@$_POST["title"] == "" || @$_POST["name"] == "" || @$_POST["class"] == "" || @$_POST["body"] == ""){
                                echo "<br />填写不完整!";
                                echo "<script language=\"JavaScript\">setTimeout(\"history.back();\", 2000);</script>";
                                exit();
                        }
                        else {
                                @$body = $_POST["body"];
                                $body = str_replace("'", "''", $body);
                                $body = str_replace("\r\n", "<br />", $body);
                                @$sql = "Update article Set by_title = '" . $_POST["title"] . "' , by_name = '" . $_POST["name"] . "' , by_count = " . $_POST["count"] . " , by_body = '" . $body . "' , by_class = '" . $_POST["class"] . "' Where id = " . $_GET["id"];
                                $conn->Execute($sql);
                                echo "<br />修改成功!";
                                echo "<script language=\"JavaScript\">setTimeout(\"window.location.href='?admin=list';\", 2000);</script>";
                        }
                }
                else {
                        @$sql = "Select * From Article Where id = " . $_GET["id"];
                        @$rs  = $conn->Execute($sql);
                        echo "<div style=\"width:590px;text-align:left\">\r\n"
                                 . "<form action=\"?admin=list&list=edit&edit=true&id=" . $rs->fields->value . "\" method=\"post\">\r\n"
                                 . "文章标题:<input type=\"text\" name=\"title\" value=\"" . $rs->fields->value . "\" /><br />\r\n"
                                 . "文章作者:<input type=\"text\" name=\"name\" value=\"" . $rs->fields->value . "\" /><br />\r\n"
                                 . "浏览次数:<input type=\"text\" name=\"count\" value=\"" . $rs->fields->value . "\" /><br />\r\n"
                                 . "文章类别:<select name=\"class\" id=\"class\">\r\n";
                        @$sql = "Select * From List";
                        @$crs  = $conn->Execute($sql);
                        while(!$crs->Eof){
                                echo "<option value=\"" . $crs->Fields->value . "\">" . $crs->Fields->value . "</option>\r\n";
                                $crs->MoveNext();
                        }
                        $crs->close();
                        @$body = $rs->Fields->value;
                        $body  = str_replace("<br />","\r\n",$body);
                        echo "</select><br />\r\n"
                                 . "<script language=\"JavaScript\">\r\n"
                                 . "document.getElementById(\"class\").selectedIndex = " . $rs->fields->value . " - 1;\r\n"
                                 . "</script>\r\n"
                                 . "文章正文:<textarea cols=\"70\" rows=\"14\" name=\"body\">" . $body . "</textarea><br />\r\n"
                                 . "<center><input type=\"submit\" value=\"提交\" />&nbsp;<input type=\"reset\" value=\"重置\" /></center>\r\n"
                                 . "</form>\r\n"
                                 . "</div>\r\n";
                }
        }

        //删除文章
        if(@$_GET["list"] == "del"){
                @$sql = "Delete From Article Where id = " . $_GET["id"];
                $conn->Execute($sql);
                echo "<br />删除成功!";
                echo "<script language=\"JavaScript\">setTimeout(\"window.location.href='?admin=list';\", 100);</script>";
        }               
}       

//==================================
//权限验证
//==================================
function admin_session(){
        if(@$_SESSION["ARTICLEADMIN"] != "BY_PHP_ARTICLE"){
                echo "<br />请先登陆!";
                echo "<script language=\"JavaScript\">setTimeout(\"window.location.href='?admin=login';\", 2000);</script>";
                exit();
        }
}

//==================================
//后台登陆
//==================================
function admin_login(){
        if(@$_SESSION["ARTICLEADMIN"] == "BY_PHP_ARTICLE"){
                echo "<script language=\"JavaScript\">window.location.href='?admin=admin';</script>";
        }
        else{
                echo "<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"#cccccc\">\r\n"
                         . "        <tr bgcolor=\"#F3F3F3\" align=\"center\">\r\n"
                         . "                <td><strong>管理员登陆</strong></td>\r\n"
                         . "        </tr>\r\n"
                         . "        <tr bgcolor=\"#FFFFFF\">\r\n"
                         . "                <form method=\"post\" action=\"?admin=check\">\r\n"
                         . "                <td><input type=\"password\" name=\"adminPass\" /><input type=\"submit\" value=\"确定\" /></td>\r\n"
                         . "                </form>\r\n"
                         . "        </tr>\r\n"
                         . "</table>\r\n";
        }
}

//==================================
//登陆验证
//==================================
function admin_check(){
        if(trim($_POST["adminPass"]) != "admin"){
                echo "<br />错误的密码!";
                echo "<script language=\"JavaScript\">setTimeout(\"history.back();\", 2000);</script>";
        }
        else {
                echo "<br />登陆成功!";
                @$_SESSION["ARTICLEADMIN"] = "BY_PHP_ARTICLE";
                echo "<script language=\"JavaScript\">setTimeout(\"window.location.href='?admin=admin';\", 2000);</script>";
        }
}

//==================================
//URL参数列表
//==================================
function page_load(){
        @$classid = $_GET["classid"];
                if($classid != ""){
                        if(!is_numeric($classid)){
                                echo "<br />错误的参数!";
                                return false;
                        }
                        else {
                                article_list($classid);
                        }
                }

        @$artid = $_GET["artid"];
                if($artid != ""){
                        if(!is_numeric($artid)){
                                echo "<br />错误的参数!";
                                return false;
                        }
                        else {
                                article_look($artid);
                        }
                }
        @$admin = $_GET["admin"];
                if($admin == "login"){
                        admin_login();
                }
                if($admin == "check"){
                        admin_check();
                }
                if($admin == "admin"){
                        admin_session();
                        echo "<br />登陆成功!请选择功能菜单进行操作.";
                }
                if($admin == "logout"){
                        session_destroy();
                        echo "<br />已成功退出!";
                        echo "<script language=\"JavaScript\">setTimeout(\"window.location.href='?admin=login';\", 2000);</script>";
                }
                if($admin == "class"){
                        admin_session();
                        admin_class();
                }
                if($admin == "insert"){
                        admin_session();
                        admin_insert();
                }
                if($admin == "list"){
                        admin_session();
                        admin_list();
                }

        if($classid == "" && $artid == "" && $admin == ""){
                echo "<script language=\"JavaScript\">window.location.href='?classid=1';</script>";
        }
}

?>
<div style="width:100%;text-align:left;background:#cccccc;line-height:20px;">
        <br /><br /><br />文章分类->&nbsp;<?=article_menu();?>
</div>
<?=page_load();?>
<?php
$conn->Close();
?>
</body>
</html>

Javr 发表于 2006-8-27 21:14:55

顶一下~~~

野草 发表于 2006-9-3 23:28:13

代码好像很少的样子哦!

Rimifon 发表于 2006-9-10 16:36:17

哇,单文件。

Rimifon 发表于 2006-9-10 17:13:06

http://www.fengyun.org/Test/Article/?classid=2
有点错误啊:Warning: date() expects parameter 2 to be long, object given in E:\www\Rimifon\Test\Article\index.php on line 77
今天洗澡碰见一牛人,太牛了 无忧脚本  11

Warning: date() expects parameter 2 to be long, object given in E:\www\Rimifon\Test\Article\index.php on line 77
小故事 无忧脚本  6

Warning: date() expects parameter 2 to be long, object given in E:\www\Rimifon\Test\Article\index.php on line 77

biyuan 发表于 2006-9-11 05:45:51

查找:
. date("Y-m-d H:i:s",$rs->Fields->value) .
替换成:
. $rs->Fields->value .

Rimifon 发表于 2006-9-12 23:05:05

向老B学习PHP,也玩一个PHP+Access单文件文章管理系统看看

运行环境:Windows平台 + PHP5

第一次访问自动建立Access数据库,实现了分页、数据库压缩等。

所有查询语句全部采用了Access的“存储过程”。(使用了一个自己用Javascript编写的数据库操作组件:D没想到Javascript还可以用于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 = new COM("script: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");
function SqlVar($Arg, $Need=1)
{
        $Arg = Str_Replace("\\\\", "\\", HtmlSpecialChars($_POST[$Arg]));
        $Arg = Str_Replace("\\'", "'", $Arg);
        If($Need)$Arg = Str_Replace("'", "''", $Arg);
        return Str_Replace("\\&quot;","&quot;", $Arg);
}
//=====================
//查询文章分类
//=====================
function ShowFenLei()
{
        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;
}
//======================
//第一次执行建立数据库
//======================
function InitDb()
{
        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(" ", "&nbsp;", 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["Page"]);
                        For($C=0; $C<$Ds->length; $C++)
                        {?><tr>
        <td><?=$Ds->{$C}->ID?></td>
        <td><a href="?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>
</tr><?php
                        }?>
<tr><td colspan="5">共 <?=$Ds->PageCount?> 页,当前为第 <?=$Ds->AbsolutePage?> 页。
<a href="?FenLei=<?=$_GET["FenLei"]*1?>">首页</a> <a href="?FenLei=<?=$_GET["FenLei"]*1?>&Page=<?=$Ds->AbsolutePage-1?>">上页</a>
<a href="?FenLei=<?=$_GET["FenLei"]*1?>&Page=<?=$Ds->AbsolutePage+1?>">下页</a> <a href="?FenLei=<?=$_GET["FenLei"]*1?>&Page=<?=$Ds->PageCount?>">末页</a></td>
</tr></table>
<?php
                }
}
?>
<!-- Your Page Footer Here -->
</body></html>

[ 本帖由 Rimifon 最后编辑于 2006-9-15 00:41 ]

biyuan 发表于 2006-9-13 02:21:28

Parse error: syntax error, unexpected '{' in E:\Vhost\www\by_article\test.php on line 15

不错,还写了分页,代码也简洁多了~
不过我这里好象运行不了:)

Rimifon 发表于 2006-9-13 09:25:06

我是在php5环境下做的。难道你那个php版本不支持try{}catch语句?或者是保存编码不是ANSI格式?

另外,你代码里面那么多@符号我看不出有什么作用啊,不知能否解释一下。

[ 本帖由 Rimifon 最后编辑于 2006-9-13 14:07 ]

Rimifon 发表于 2006-9-13 12:14:05

找到has encountered an Access Violation at 7C96002E的原因了,Access中Sql语句不允许超过1024字节。超过就会发生系统级别的错误,不能用try捕捉到。

biyuan 发表于 2006-9-13 19:07:57

"@"的作用是忽略php中的警告错误提示。
当然如果在php.ini中关闭了此项,可以不用"@"

Rimifon 发表于 2006-9-14 08:57:17

当时我也是这么想的,主要是看到那个“@$sql = "Select * From List";”有点疑惑,呵呵,难道对$sql进行字符串赋值也怕出错?

昨天在php4下测试了一下代码,果然有你说的那个问题。另外,php 4并不支持“new COM("script:http://www.zope.org/Members/Rimifon/DbHelper.sct#DataHelper");”这样的方式创建控件。PHP5功能还是强大多了。:D

zfh 发表于 2007-3-10 22:35:12

好,下一个来看看。

langziyangyang 发表于 2007-8-4 17:55:08

呵呵这个不错啊

我想问一下php 高手 php 链接access,mysql,mssql 之后有没有本质上的区别,比如操作数据的函数会不会有变化

mailangel123 发表于 2007-8-9 10:32:26

好久都没来了,我没用过php+access现在只用php+mysql,

今天看了一个觉得,php+access和asp+access几乎相同啊,
asp:  conn.open()      php:conn->open()
asp:  do while not rs.eof       php: while(!$rs->eof)
刚学这个不久,个人意见,不过觉得php很多地方都优于asp

solidluck 发表于 2007-8-24 04:14:59

为什么我用com类会导致超时呢?郁闷

is286 发表于 2010-5-12 08:42:34

Class 'COM' not found

PHP 5

fhuoyun 发表于 2010-5-12 09:45:26

用php还去拆腾access  :L
页: [1]
查看完整版本: 【原创】奉献给新手—PHP+ACCESS一页文章管理程序