PHP----练习-----新闻管理----增删改查
2024-08-29 19:01:21
练习-----新闻管理
题目要求如下:
做法:
【1】建数据库
【2】封装类文件--------DBDA.class.php
<?php
class DBDA
{
public $fuwuqi="localhost"; //服务器地址
public $yonghuming="root";//用户名
public $mima="";//密码 public $dbconnect;//连接对象 //操作数据库的方法 //$sql代表需要执行的SQL语句
//$type代表SQL语句的类型,1代表查询,2代表增删改
//$shujukuming代表数据库的名称
//如果是查询,返回二维数组
//如果是增删改,返回true或false function Query($sql,$type=1,$shujukuming="newssystem")
{
//造连接对象
$this->dbconnect = new MySQLi($this->fuwuqi,$this->yonghuming,$this->mima,$shujukuming); //判断是否出错
if(!mysqli_connect_error())
{
//如果连接成功,执行SQL语句
$result = $this->dbconnect->query($sql); //根据语句类型判断
if($type==1)
{
//如果是查询语句,返回二维数组
return $result->fetch_all();
}
else
{
//如果是其他语句,返回true或false
return $result;
} }
else
{
return"连接失败"; } } } ?>
【3】发布新闻页面---------fabuxinwen.php
<!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>
<center><h1>发布新闻</h1> <form action="fabuxinwenchuli.php" method="post"> <div>标题:<input type="text" name="title" style="width:400px"/></div><br /> <div>作者:<input type="text" name="Author" style="width:200px"/></div><br /> <div>来源:<input type="text" name="source" style="width:400px"/></div><br /> <div>内容:<textarea name="content" cols="50" rows="4" ></textarea></div><br />
<br /> <div><input type="submit" name="tijiao" value="提交" />
<a href="chakan.php"><input type="button" value="查看"/></a></div>
</form> </center>
</body>
</html>
【4】发布新闻的处理页面:----------fabuxinwenchuli.php
<?php $title = $_POST["title"];
$Author = $_POST["Author"];
$source = $_POST["source"];
$content = $_POST["content"];
$shijian= date("Y-m-d H:i:s"); include("DBDA.class.php"); $dx=new DBDA(); $sql="insert into news values('','{$title}','{$Author}','{$source}','{$content}','{$shijian}')"; $r = $dx->Query($sql,0); if($r)
{
header("location:fabuxinwen.php");
}
else
{
echo "修改失败!";
} ?>
【4】查看新闻页面----------chakan.php
<!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>
<table width="1000px" cellpadding="1" border="1"ellspacing="1">
<tr>
<td>编号</td>
<td>标题</td>
<td>作者</td>
<td>来源</td>
<td>内容</td>
<td>时间</td>
<td>修改</td>
<td>删除</td>
</tr> <?php
include("DBDA.class.php"); $dx=new DBDA(); $sql="select * from news";
$r = $dx->Query($sql,1);
//$attr=$result->fetch_all();
foreach($r as $v)
{
echo "<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$v[2]}</td>
<td>{$v[3]}</td>
<td>{$v[4]}</td>
<td>{$v[5]}</td>
<td><a href='xiugai.php?newsid={$v[0]}'>修改</a></td>
<td><a href='shanchu.php?newsid={$v[0]}'}>删除</a></td>
</tr>"; } ?>
</table>
</body>
</html>
【5】查看新闻的处理页面----------chakanchuli.php
<?php $newsid = $_POST["newsid"];
$title = $_POST["title"];
$author = $_POST["author"];
$source = $_POST["source"];
$content = $_POST["content"];
$time = $_POST["shijian"]; include("DBDA.class.php"); $dx=new DBDA(); $sql = "update news set title = '{$title}',author = '{$author}',source = '{$source}',content = '{$content}',time= '{$time}' where newsid = '{$newsid}'"; $result = $dx->query($sql);
if($result)
{
header("location:chakan.php");
}
else
{
echo "修改失败";
}
?>
【6】删除处理页面--------shanchu.php
<?php
$newsid = $_GET["newsid"];
var_dump($newsid); include("DBDA.class.php"); $dx=new DBDA(); $sql = "delete from news where newsid='{$newsid}'";
$r = $dx->Query($sql,2);
if($r)
{
header("location:chakan.php");
}
else
{
echo "删除失败!";
}
【7】修改页面---------xiugai.php
<!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>
<!--newsid--><center>
<h1>修改页面</h1>
<?php $newsid = $_GET["newsid"]; include("DBDA.class.php"); $dx=new DBDA(); $sql="select * from news where newsid='{$newsid}'";
$r = $dx->Query($sql); ?> <form action="xiugaichuli.php" method="post">
<input type="hidden" name="newsid" value="<?php echo $r[0][0]; ?>"/> <div>标题:<input type="text" name="title" value="<?php echo $r[0][1];?>"/></div>
<div>作者:<input type="text" name="author" value="<?php echo $r[0][2];?>"/></div>
<div>来源:<input type="text" name="source" value="<?php echo $r[0][3];?>"/></div>
<div>内容:<textarea name="content" cols="100" rows="10"><?php echo $r[0][4];?></textarea></div> <div><input type="submit" value="修改"/></div>
<input type="hidden" name="newsid" value="<?php echo $newsid; ?>" /> </form>
<!--<a href="chakan.php"><input type="button" value="查看"></a>-->
</center>
</body>
</html>
【8】修改的处理页面---------xiugaichuli.php
<?php $newsid = $_POST["newsid"];
$title = $_POST["title"];
$Author = $_POST["author"];
$source = $_POST["source"];
$content = $_POST["content"]; include("DBDA.class.php"); $dx=new DBDA(); $sql="update news set title='{$title}',author = '{$Author}',source ='{$source}',content='{$content}' where newsid = '{$newsid}'";
echo $sql;
$r = $dx->Query($sql,0);
var_dump($r);
if($r)
{
header("location:chakan.php");
}
else
{
echo "修改失败!";
} ?>
最新文章
- dirname 命令
- 报表或BI的价值在哪?
- 排序 order by 的用法
- Android中Path类的lineTo方法和quadTo方法画线的区别
- JDBC相关的类介绍
- Leetcode#146 LRU Cache
- [置顶] 第一天初试linux
- a标签的背景图在ie8下显示问题
- FileSystemWatcher使用方法具体解释
- kali linux 2.0下搭建DVWA渗透测试演练平台
- 软件测试自动化的最新趋势对开源测试管理软件ITEST的启示
- .netcore2.0+pgsql 脚手架
- ImageUtil
- Python操作字典(dict)
- Python os.access() 方法
- ionic入门
- Spring JDBC对象批量操作
- CSS学习之盒子模型
- Microsoft Dynamics CRM 2011 如何导入组织
- Django 模板中使用css, javascript