add.PHP
代码如下:
config.PHP
代码如下:
PHP
define("HOST","localhost");
define("USER","justfan");
define("PWD","justfan");
define("DB","justfanDB");
define("PORT","3360");
?>
define("HOST","localhost");
define("USER","justfan");
define("PWD","justfan");
define("DB","justfanDB");
define("PORT","3360");
?>
DB_class.PHP
代码如下:
PHP
class DB
{
private $host = '';
private $uname = '';
private $pwd = '';
private $port = '';
private $db = '';
public static $instance = null; private function __construct($host,$uname,$pwd,$port,$db)
{
$this->host = $host;
$this->uname = $uname;
$this->port = $port;
$this->pwd = $pwd;
$this->db = $db;MysqL_connect($host,$pwd);
MysqL_select_db($this->db);
}public static function Instance()
{
if(Db::$instance==null){
include 'config.PHP';
return Db::$instance = new DB(HOST,USER,PWD,PORT,DB);
}
else
return Db::$instance;
}public function query($sql)
{
MysqL_query("SET NAMES UTF8");
$query = MysqL_query($sql) or die($sql." error");
if(!$query) return false;
else return $query;
}
public function getAll($sql)
{
$query = $this->query($sql);
if($query)
{
while($ret = MysqL_fetch_assoc($query))
{
$result[] = $ret;
}
}
return $result;
}
}
?>
class DB
{
private $host = '';
private $uname = '';
private $pwd = '';
private $port = '';
private $db = '';
public static $instance = null; private function __construct($host,$uname,$pwd,$port,$db)
{
$this->host = $host;
$this->uname = $uname;
$this->port = $port;
$this->pwd = $pwd;
$this->db = $db;MysqL_connect($host,$pwd);
MysqL_select_db($this->db);
}public static function Instance()
{
if(Db::$instance==null){
include 'config.PHP';
return Db::$instance = new DB(HOST,USER,PWD,PORT,DB);
}
else
return Db::$instance;
}public function query($sql)
{
MysqL_query("SET NAMES UTF8");
$query = MysqL_query($sql) or die($sql." error");
if(!$query) return false;
else return $query;
}
public function getAll($sql)
{
$query = $this->query($sql);
if($query)
{
while($ret = MysqL_fetch_assoc($query))
{
$result[] = $ret;
}
}
return $result;
}
}
?>
doadd.PHP
代码如下:
PHP
include 'DB_class.PHP';
$db = DB::Instance();
include 'DB_class.PHP';
$db = DB::Instance();
$title=$_POST["title"];
$content=$_POST["content"];
$num = uniqid();
$houzui=".html";
$filename=date('Ymd').'/'.$num.$houzui;
$sql="insert into news(title,content,path) values ('{$title}','{$content}','{$filename}')";
$query = $db->query($sql);
$fp=fopen("model.htm","r");
$str=fread($fp,filesize("model.htm"));
$str=str_replace("{title}",$title,$str);
$str=str_replace("{content}",$content,$str);
fclose($fp);
$dir = dirname($filename);
if(!is_dir($dir)){
mkdir($dir);
}
$handle=fopen($filename,"w");
fwrite($handle,$str);
fclose($handle);
model.htm
代码如下: