![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?php
/*
* 文个把概要:mysql数据库连接类
*/
class mysql {
private $server; //服务器名
private $user; //数据库用户名
private $password; //数据库密码
private $database; //数据库名
private $link; //mysql连接标识符
private $charset = "utf8"; //数据库编码,默认为utf8
/*==================================================
* 方法:__construct
* 功能:构造函数
* 参数:$server,$user,$password,$database,$charset
* 说明:实例化时自动连接数据库
*===================================================*/
function __construct($server,$user,$password,$database,$charset) {
$this->server = $server;
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->charset = $charset;
$this->connect(); //数据库连接方法
}
/*
*
*/
function connect() {
$this->link = mysql_connect($this->server,$this->user,$this->password) or die($this->error("数据库服器连接出错!"));
mysql_select_db($this->database, $this->link) or die($this->error("数据库连接出错!"));
mysql_query("set names '$this->charset'");
}
/*
*
*/
function query($sql) {
$result = mysql_query($sql, $this->link);
if (!$result) {
$this->error($sql."语句执行失败!");
return false;
}
else {
return $result;
}
}
function fetcharray($result) {
return mysql_fetch_array($result);
}
function fetcharay($result) {
$arr[] = array();
while ($row = mysql_fetch_array($result)) {
$arr[] = $row;
}
mysql_free_result($result);
return $arr;
}
function numrows($result) {
return mysql_num_rows($result);
}
function numfields($result) {
return mysql_num_fields($result);
}
function affectedrows() {
return mysql_affected_rows($this->link);
}
function version() {
return mysql_get_server_info();
}
function insertid() {
return mysql_insert_id($this->link);
}
function close() {
mysql_close($this->link);
}
function error($err_msg = "") {
if ($err_msg == "") {
echo "Errno:".mysql_errno()."<br>";
echo "Error:".mysql_error()."<br>";
}
else {
echo $err_msg;
}
}
function __destruct() {
$this->close();
}
}
================================================
代码
/*
* 文个把概要:mysql数据库连接类
*/
class mysql {
private $server; //服务器名
private $user; //数据库用户名
private $password; //数据库密码
private $database; //数据库名
private $link; //mysql连接标识符
private $charset = "utf8"; //数据库编码,默认为utf8
/*==================================================
* 方法:__construct
* 功能:构造函数
* 参数:$server,$user,$password,$database,$charset
* 说明:实例化时自动连接数据库
*===================================================*/
function __construct($server,$user,$password,$database,$charset) {
$this->server = $server;
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->charset = $charset;
$this->connect(); //数据库连接方法
}
/*
*
*/
function connect() {
$this->link = mysql_connect($this->server,$this->user,$this->password) or die($this->error("数据库服器连接出错!"));
mysql_select_db($this->database, $this->link) or die($this->error("数据库连接出错!"));
mysql_query("set names '$this->charset'");
}
/*
*
*/
function query($sql) {
$result = mysql_query($sql, $this->link);
if (!$result) {
$this->error($sql."语句执行失败!");
return false;
}
else {
return $result;
}
}
function fetcharray($result) {
return mysql_fetch_array($result);
}
function fetcharay($result) {
$arr[] = array();
while ($row = mysql_fetch_array($result)) {
$arr[] = $row;
}
mysql_free_result($result);
return $arr;
}
function numrows($result) {
return mysql_num_rows($result);
}
function numfields($result) {
return mysql_num_fields($result);
}
function affectedrows() {
return mysql_affected_rows($this->link);
}
function version() {
return mysql_get_server_info();
}
function insertid() {
return mysql_insert_id($this->link);
}
function close() {
mysql_close($this->link);
}
function error($err_msg = "") {
if ($err_msg == "") {
echo "Errno:".mysql_errno()."<br>";
echo "Error:".mysql_error()."<br>";
}
else {
echo $err_msg;
}
}
function __destruct() {
$this->close();
}
}
================================================
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?php
require_once ('dbinit.php');
require_once ('function.php');
$action = $_GET['action'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?PHP if ($action=='add') { ?>
<form id="form1" name="form1" method="post" action="?action=save">
<div>
<div id="">增加日志</div>
<input name="title" type="text" id="title" value="" size="25" />
<textarea name="remark" id="remark" cols="45" rows="11"></textarea>
<input name="submit" value="提交" type="submit" />
</div>
</form>
<?php
}
else
if ($_GET['action']=="save") {
$remark = $_POST['remark'];
$title = $_POST['title'];
$time = date("Y-m-d H:i:s");
$sql = "insert into c(title,remark,time) values('$title','$remark', '$time')";
$result = $db_mysql->query($sql);
if ($result==true) {
//echo "<script>alert('插入成功!');window.location.href='index.php'</script>";
forward('插入成功!', 'href', 'index1.php');
}
else {
forward('插入失败!', 'href', 'index1.php');
}
}
else if ($action == "del") {
$id = $_GET['id'];
$sql = "delete from c where id='$id'";
$db_mysql->query($sql);
forward("删除成功!","href","index1.php");
}
else if ($action == "edit") {
$id = $_GET['id'];
$sql = "select * from c where id='$id'";
$result = $db_mysql->query($sql);
$row = $db_mysql->fetcharray($result);
$title = $row['title'];
$remark = $row['remark'];
$time = $row['time'];
?>
<form action="?action=editsubmit" method="post" id="editform" >
<table border="1" style="border-collapse: collapse;" >
<input name="id" type="hidden" id="id" value="<?php echo $id; ?>" />
<tr><th>ID</th><td><?php echo $id; ?></td></tr>
<tr><th>标题</th><td><input name="title" type="text" id="title" value="<?php echo $title ?>" /></td></tr>
<tr><th>内容</th><td><input name="remark" type="text" id="remark" value="<?php echo $remark ?>" /></td></tr>
<tr><th>时间</th><td><?php echo $time ?></td></tr>
</table>
<input name="editsubmit" type="submit" id="editsubmit" value="保存" />
</form>
<?php
}
else if ($action == "editsubmit") {
$id = isset( $_POST['id']) ? $_POST['id'] : "";
$title = isset($_POST['title']) ? $_POST['title'] : "";
$remark = isset($_POST['remark']) ? $_POST['remark'] : "";
$sql = "update c set title = '$title', remark='$remark' where id='$id'";
$result = $db_mysql->query($sql);
if ($result) {
forward('修改成功!','href','index1.php');
}
else {
forward('修改失败','href','index1.php');
}
}
else {
$sql = "select * from c";
$result = $db_mysql->query($sql);
?>
<h1><a href="?action=add">增加</a></h1>
<table style="border-collapse:collapse;" border="1" cellpadding="0" cellspacing="0" >
<tr><th>ID</th><th>标题</th><th>内容</th><th>时间</th><th>删除</th><th>修改</th></tr>
<?php
while ($row = $db_mysql->fetcharray($result)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['remark']; ?></td>
<td><?php echo $row['time']; ?></td>
<td><a href="?action=del&id=<?php echo $row['id'] ?>">删除</a></td>
<td><a href="?action=edit&id=<?php echo $row['id'] ?>">修改</a></td>
</tr>
<?php
}
}
?>
</table>
</body>
</html>
require_once ('dbinit.php');
require_once ('function.php');
$action = $_GET['action'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?PHP if ($action=='add') { ?>
<form id="form1" name="form1" method="post" action="?action=save">
<div>
<div id="">增加日志</div>
<input name="title" type="text" id="title" value="" size="25" />
<textarea name="remark" id="remark" cols="45" rows="11"></textarea>
<input name="submit" value="提交" type="submit" />
</div>
</form>
<?php
}
else
if ($_GET['action']=="save") {
$remark = $_POST['remark'];
$title = $_POST['title'];
$time = date("Y-m-d H:i:s");
$sql = "insert into c(title,remark,time) values('$title','$remark', '$time')";
$result = $db_mysql->query($sql);
if ($result==true) {
//echo "<script>alert('插入成功!');window.location.href='index.php'</script>";
forward('插入成功!', 'href', 'index1.php');
}
else {
forward('插入失败!', 'href', 'index1.php');
}
}
else if ($action == "del") {
$id = $_GET['id'];
$sql = "delete from c where id='$id'";
$db_mysql->query($sql);
forward("删除成功!","href","index1.php");
}
else if ($action == "edit") {
$id = $_GET['id'];
$sql = "select * from c where id='$id'";
$result = $db_mysql->query($sql);
$row = $db_mysql->fetcharray($result);
$title = $row['title'];
$remark = $row['remark'];
$time = $row['time'];
?>
<form action="?action=editsubmit" method="post" id="editform" >
<table border="1" style="border-collapse: collapse;" >
<input name="id" type="hidden" id="id" value="<?php echo $id; ?>" />
<tr><th>ID</th><td><?php echo $id; ?></td></tr>
<tr><th>标题</th><td><input name="title" type="text" id="title" value="<?php echo $title ?>" /></td></tr>
<tr><th>内容</th><td><input name="remark" type="text" id="remark" value="<?php echo $remark ?>" /></td></tr>
<tr><th>时间</th><td><?php echo $time ?></td></tr>
</table>
<input name="editsubmit" type="submit" id="editsubmit" value="保存" />
</form>
<?php
}
else if ($action == "editsubmit") {
$id = isset( $_POST['id']) ? $_POST['id'] : "";
$title = isset($_POST['title']) ? $_POST['title'] : "";
$remark = isset($_POST['remark']) ? $_POST['remark'] : "";
$sql = "update c set title = '$title', remark='$remark' where id='$id'";
$result = $db_mysql->query($sql);
if ($result) {
forward('修改成功!','href','index1.php');
}
else {
forward('修改失败','href','index1.php');
}
}
else {
$sql = "select * from c";
$result = $db_mysql->query($sql);
?>
<h1><a href="?action=add">增加</a></h1>
<table style="border-collapse:collapse;" border="1" cellpadding="0" cellspacing="0" >
<tr><th>ID</th><th>标题</th><th>内容</th><th>时间</th><th>删除</th><th>修改</th></tr>
<?php
while ($row = $db_mysql->fetcharray($result)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['remark']; ?></td>
<td><?php echo $row['time']; ?></td>
<td><a href="?action=del&id=<?php echo $row['id'] ?>">删除</a></td>
<td><a href="?action=edit&id=<?php echo $row['id'] ?>">修改</a></td>
</tr>
<?php
}
}
?>
</table>
</body>
</html>
?>