您的位置:首页 > 编程语言 > PHP开发

php面向对象操作mysql数据库,简单的新闻管理系统的编写

2015-03-21 21:51 966 查看

一.新闻主页面(main_screen.php)

代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>新闻查询</title>
</head>
<body bgcolor='green'>
<h1 align='center' ><font color='red'>?新闻管理?</font></h1>
<hr align='center' size='1' color='yellow'></hr>
<h2 align='center' ><a href="main_new.php">查询新闻</a></h2>
<h3 align='center' ><a href="add1.php">添加新闻</a></h3>
<h4 align='center' ><a href="delete1.php">删除新闻</a></h4>
</body>
</html>
二.新闻查询部分的设计
1.查询页面的设计(main_new.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>新闻查询</title>
</head>
<body  bgcolor='6CD3C1'>
<form action='news1.php' method='post'>
<font color="red"><b>查询新闻的关键字</b></br></font>
<input type='text' name="new"></input>
<input type="hidden" value="search" name="type" ></input>
<input type="submit" value="查询"></input>
</br></br>
<a href="main_screen.php">
<table border="2">
<tr>
<th>返回主页面</th>
</tr>
</table>
</a>
</form>
</body>
</html>
2.查询过程(news1.php)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body  bgcolor='6CD3C1'>
<?php
require_once 'news2.php';
if(isset($_REQUEST['new']))
{
$title=$_REQUEST['new'];
} else
{
die("操作失败news1.php".$title->error);
}

if($title==null)
{
die(" <font color='red' ><b>您的输入不可以为空,<a href='main_new.php'>请您重新输入!</a></b></font>");
}

$sql="select title,content,author,time_s from news_record where title like '%".$title."%' ";
$mysqli=new m_s();
$res=$mysqli->execute_dql($sql);
if($res->num_rows>0)
{
while ($row=$res->fetch_assoc())
{
echo "新闻标题:".$row["title"].'</br>';
echo "新闻内容:".$row['content'].'</br>';
echo "新闻撰稿者:".$row["author"].'</br>';
echo "新闻时间:".$row["time_s"].'</br>';
}
echo "</br>请您重新<a href='main_new.php'><font color='red'>输入查询!</font></a>";
}
else
{
echo "sorry,您查询的内容不存在,请您重新<a href='main_new.php'><font color='red'>输入查询!</font></a>";
}
$res->free();
?>
</body>
</html>
三.新闻添加部分的设计
1.添加主页面设计(add1.php)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>添加新闻</title>
</head>
<body bgcolor='6CD3C1'>
<form action='add2.php' method='post'>
<font color="red"><b>请输入新闻标题</b></font>
<input type='text' name="title"></input></br></br>
<font color="red"><b>请输入新闻内容</b></font>
<input type='text' name="content"></input></br></br>
<font color="red"><b>请输入新闻作者</b></font>
<input type='text' name="author"></input></br></br>
<font color="red"><b>请输入新闻时间(x-x-x x-x-x)</b></font>
<input type='text' name="time"></input></br></br>
<input type="hidden" value="search" name="type" ></input>
<input type="submit" value="开始添加"></input>
</form>
<a href="main_screen.php">
<table border="2">
<tr>
<th>返回主页面</th>
</tr>
</table>
</a>
</body>
</html>
2.添加过程的设计(add2.php)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body bgcolor='6CD3C1'>
<?php
require_once 'news2.php';
if(isset($_REQUEST['title'])&&isset($_REQUEST['content'])&&isset($_REQUEST['author'])&&isset($_REQUEST['time']))
{
$title=$_REQUEST['title'];
$content=$_REQUEST['content'];
$author=$_REQUEST['author'];
$time=$_REQUEST['time'];
}
else
{
die ("操作失败news1.php".$title->error);
}
if($title==null||$content==null||$author==null||$time==null)
{
die(" <font color='red' ><b>您的输入不可以为空,<a href='add1.php'>请您重新输入!</a></b></font>");
}
$sql="insert into news_record values ('null','".$title."','".$content."','".$author."','".$time."') ";
$mysqli=new m_s();
$res=$mysqli->execute_dml($sql);
if($res==0)
{
die ("您的操作有误,请您检查!".$res->error);
}
else
{
if($res>0)
{
echo "您的操作成功";
}
else
{
echo "您的操作,没有影响到行数";
}
}

echo "</br><font color='red' ><b><a href='main_new.php'>请您重新添加!</a></b></font>";
?>
</body>
</html>
三.删除新闻的部分
1.(delete1.php)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body  bgcolor='6CD3C1'>
<?php
require_once 'news2.php';
$mysqli=new m_s();
$sql1="select * from news_record";
$res1=$mysqli->execute_dql($sql1);
if($row=$res1->num_rows>0)
{
while($a=$res1->fetch_row())
{
echo "编号=>>".$a[0]."</br>"."标题=>>".$a[1]."</br>"."内容=>>".$a[2]."</br>"."小编=>>".$a[3]."</br>"."时间=>>".$a[4]."</br>";
echo "<a href='delete3.php'>删除</a>";
echo "<hr color='yellow'></hr>";
}
}
else {
echo "您的操作有误!";

}
?>
<a href="main_screen.php">
<table border="2">
<tr>
<th>返回主页面</th>
</tr>
</table>
</a>
</body>
</html>

2.(delete3.php)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>新闻查询</title>
</head>
<body  bgcolor='6CD3C1'>
<form action='delete2.php' method='post'>
请确认要删除的新闻编号:</br><input type='text' name='de_te'>
</input>
<input type='submit' value='确认删除'></input>
</form>
</body>
</html>

3.(delete2.php)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body  bgcolor='6CD3C1'>
<?php
require_once 'news2.php';
if(isset($_REQUEST['de_te']))
{
$de_te=$_REQUEST['de_te'];
} else
{
die("操作失败news1.php".$title->error);
}
$mysqli=new m_s();
$sql="delete from news_record where id='$de_te'";
$res=$mysqli->execute_dql($sql);
if($res==0)
{
die ("您的操作有误,请您检查!".$res->error);
}
else
{
if($res>0)
{
echo "删除数据成功";
}
else
{
echo "您的操作,没有影响到行数";
}
}

echo "</br><font color='red' ><b><a href='main_new.php'>请您重新返回删除!</a></b></font>";

?>
<a href="main_screen.php">
<table border="2">
<tr>
<th>返回主页面</th>
</tr>
</table>
</a>
</body>
</html>

四.工具类的设计(news.php)

<?php
class m_s
{
private $mysqli;
private static $host="localhost:3306";
private static $user="root";
private static $password="";
private static $db="news";
function __construct()
{
$this->mysqli=new mysqli(self::$host,self::$user,self::$password,self::$db);
if($this->mysqli->connect_error)
{
die("连接失败".$this->mysqli->conecct_error);
}
$this->mysqli->query("set names utf8");
}

function execute_dql($sql)
{
$res=$this->mysqli->query($sql) or
die("操作失败news2.1.php".$this->mysqli->error);
return $res;
}
function execute_dml($sql)
{
$res=$this->mysqli->query($sql)or
die("操作失败news2.2.php".$this->mysqli->error);
if(!$res)
{
return 0;
}
else
{
if($this->mysqli->affected_rows>0)
{

return 1;
}
else
{

return 2;
}
}

}
}

?>

五.数据库的设计

create database news;

create table news_record(id int primary key auto_increment,title varchar(128) not null,content varchar(256) not null,author varchar(20) not null,time_s datetime not null)character set utf8;

insert into news_record values('1','nba','骑士决战勇士','小鑫','2015-8-28 19-33-45');
insert into news_record values('null','抗日','悉数歼敌','小鑫','1939-2-28 13-43-45');
insert into news_record values('null','宿舍','都考上心仪的大学','鑫鑫','1993-8-28 19-33-45');
insert into news_record values('null','学校','学费涨价了','鑫鑫','1963-9-28 19-33-45');
insert into news_record values('null','帅哥','帅哥不顶事','鑫鑫','2001-10-28 20-33-45');


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: