網站程式設計-PHP
  
  
    
	
	- 
	update.php
	主要工作有,建立資料夾、增加資料表、增加欄位
	增加資料表必須放在 增加欄位的流程前面執行才可以
	輪播圖要上傳圖片,所以必須有資料夾放圖片、資料表放圖片的資訊
	
<?php
require_once 'head.php';
#整理傳入變數
$op = isset($_REQUEST['op'])?$_REQUEST['op']:"";
$sn = isset($_REQUEST['sn'])?intval($_REQUEST['sn']):"";
#程式流程
switch($op){
  #顯示單筆
  case "op_show":
    die($op);
  break;
  #更新
  default:
    $op="op_list";
    $msg=op_list();
    if($msg)
    {
      redirect_header("index.php",3000,"更新系統成功!!");
    } else
    {
      redirect_header("index.php",3000,"更新系統失敗!!");
    }
  break;
}
#將變數送至樣板引擎
#op
$smarty->assign("op", $op);
/*
$WEB['theme_name'] = "admin";
WEB['title'] = "網站名稱";
$WEB['file_name'] = basename ($_SERVER['PHP_SELF']);
*/
#變數在head.php
$smarty->assign("WEB", $WEB);
#程式結尾
$smarty->display('theme.html');
#函數
########################################
# 更新主程式
########################################
function op_list()
{
  global $mysqli;
  #檢查資料夾
  mk_dir(WEB_PATH."/uploads");
  mk_dir(WEB_PATH."/uploads/slider");
  //-------- 資料表 ------
  #檢查資料表(show_kind)
  if(!chk_isTable("show_kind")) go_update1();
  #檢查資料表(show_file)
  if(!chk_isTable("show_files")) go_update2();
  //-------- 資料表 end------
  //-------- 欄位 ------
  //if(!chk_isColumn("sn1","show_kind")) go_update3();
  //-------- 欄位 end------
  return true;
}
########################################
# 建立資料表 show_kind
########################################
function go_update1()
{
    global $mysqli;
    $sql="
      CREATE TABLE `show_kind` (
        `sn` smallint(5) unsigned not null auto_increment comment 'sn',
        `ofsn` smallint(5) unsigned not null default 0 comment '父類別',
        `kind` varchar(255) not null default 'nav_home' comment '分類',
        `title` varchar(255) not null  default '' comment '標題',
        `sort` smallint(5) unsigned not null default 0 comment '排序',
        `enable` enum('1','0') not null default '1' comment '狀態',
        `url` varchar(255) not null default '' comment '網址',
        `target` enum('0','1') not null default '0' comment '外連',
        `col_sn` int(10) unsigned not null default 0 comment 'col_sn',
        `content` text not null default '' comment '內容',
        PRIMARY KEY (`sn`)
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    ";
    $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    return true;
}
########################################
# 建立資料表show_files
########################################
function go_update2()
{
    global $mysqli;
    $sql="
      CREATE TABLE `show_files` (
        `sn` smallint(5) unsigned not null auto_increment comment 'sn',
        `col_name` varchar(255) not null default '' comment '欄位名稱',
        `col_sn` smallint(5) unsigned not null default 0 comment '欄位編號',
        `sort` smallint(5) unsigned not null default 0 comment '排序',
        `kind` enum('img','file') not null default 'img' comment '檔案種類',
        `file_name` varchar(255) not null default '' comment '檔案名稱',
        `file_type` varchar(255) not null default '' comment '檔案類型',
        `file_size` int(10) unsigned not null default 0 comment '檔案大小',
        `description` text not null default '' comment '檔案說明',
        `counter` mediumint(8) unsigned not null default 0 comment '下載人次',
        `sub_dir` varchar(255) not null default '' comment '檔案子路徑',
        PRIMARY KEY (`sn`)
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    ";//die($sql);
    $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    return true;
}
########################################
# 在資料表show_kind 增加一個 sn1 欄位
########################################
// function go_update3()
// {
//     global $mysqli;
//     $sql="ALTER TABLE `show_kind` ADD `sn1` smallint(5) unsigned NOT NULL default 0 comment 'sn1'";//die($sql);
//     $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
//     return true;
// }
########################################
# 檢查某欄位是否存在(欄名,資料表)
########################################
function chk_isColumn($col_name="",$tbl_name="")
{
    global $mysqli;
    if(!$col_name and $tbl_name)return;
    //SHOW COLUMNS FROM `show_kind` LIKE 'sn1'
    $sql    = "SHOW COLUMNS FROM `{$tbl_name}` LIKE '{$col_name}'";
    $result = $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    if ($result->num_rows)return true; //欄位存在
    return false;//欄位不存在
}
########################################
# 檢查資料表是否存在(資料表)
########################################
function chk_isTable($tbl_name="")
{
    global $mysqli;
    if(!$tbl_name)return;
    $sql    = "SHOW TABLES LIKE '{$tbl_name}'";//die($sql);
    $result = $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    if ($result->num_rows)return true; //欄位存在
    return false;//欄位不存在
}
	 
	 
	- function.php 增加一個建立資料夾的函數 
	
#####################################################################################
#  建立目錄
#####################################################################################
if(!function_exists("mk_dir")){
function mk_dir($dir = "")
{
  #若無目錄名稱秀出警告訊息
  if (empty($dir)) {
      return;
  }
  #若目錄不存在的話建立目錄
  if (!is_dir($dir)) {
    umask(000);
    //若建立失敗秀出警告訊息
    mkdir($dir, 0777);
  }
}
}
	
 
	 
	- 後台的導航增加選單
	
        <li><a href='update.php'>網站更新</a></li>
        <li><a href='../../mysql/adminer.php' target='_blank'>資料庫管理</a></li>