5月5 新闻练习题

1)查看新闻页面-----主页面:zhu.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>
<h1>主页面</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>id</td>
<td>title</td>
<td>author</td>
<td>source</td>
<td>content</td>
<td>time</td>
<td>update</td>
<td>delete</td>
</tr>

<?php
$db = new MySQLi("localhost","root","","newssystem");
!mysqli_connect_error() or die("连接失败");
$sql = "select * from news";
$result = $db->query($sql);
$attr = $result->fetch_all();
if($result)
{
	foreach($attr 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?id={$v[0]}'>修改</a>
			  </td>
			  
			  <td>
			  <a href='shanchu.php?id={$v[0]}'>删除</a>
			  </td>
		      
			  </tr>";	
	}	
}
?>
</table>

<div><a href="add.php">添加数据</a></div>

</body>
</html>

2)发布新闻页面-----添加内容:add.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>
<style type="text/css">
.anniu
{
	margin-left:200px;
	float:left;}

</style>
</head>

<body>
<h1 style="margin-left:180px">发布新闻</h1>
<form action="addchuli.php" method="post">
<div>标题:<input type="text" name="title" style="width:400px; height:30px"/></div><br />
<div>作者:<input type="text" name="author" style="width:180px; height:30px" /></div><br />
<div>来源:<input type="text" name="source" style="width:180px; height:30px" /></div><br />
<div>内容:<textarea name="content" cols="80" rows="10"></textarea></div><br />

<div class="anniu"><input type="submit" value="提交" /></div>


<div class="anniu" style="margin-left:20px"><a href="zhu.php"><input type="button" value="查看" />
</a>
</div>

</form>

</body>
</html>

  页面显示效果:

3)发布新闻-----添加出来:addchuli.php

<?php

	//$id = $_POST["id"];
	$title = $_POST["title"];
	$author = $_POST["author"];
	$source = $_POST["source"];
	$content = $_POST["content"];
	$time = date("Y-m-d",time());

	
	$db = new MySQLi("localhost","root","","newssystem");
	$sql = "insert into news values('','{$title}','{$author}','{$source}','{$content}','{$time}')";
	echo $sql;
	$result = $db->query($sql);
	if($result)
	{
		header("location:zhu.php");
	}
	else
	{
		echo "添加失败";	
	}

  

4)删除新闻页面-----删除对象:shanchu.php   

<?php

	$id = $_GET["id"];
	
	$db = new MySQLi("localhost","root","","newssystem");
	$sql = "delete from news where id ='{$id}'";
	$result = $db->query($sql);
	if($result)
	{
		header("location:zhu.php");	
	}
	else
	{
		echo "删除失败";	
	}

  

 修改新闻页面-----修改页面: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>
<style type="text/css">
.anniu
{
	margin-left:200px;
	float:left;}

</style>

</head>

<body>
<h1 style="text-align:center">修改新闻</h1>
<?php
$id = $_GET["id"];
$db =new MySQLi("localhost","root","","newssystem");
$sql = "select * from news where id='{$id}'";
$result = $db->query($sql);
$arr = $result->fetch_row();


?>
<form action="xiugaichuli.php" method="post">
<div>代号:<input type="hidden" name="id" value="<?php echo $arr[0] ?>" /></div><br />

<div>标题:<input type="text" name="title" style="width:400px; height:30px" value="<?php echo $arr[1] ?>"/></div><br />
<div>作者:<input type="text" name="author" style="width:180px; height:30px" value="<?php echo $arr[2] ?>"/></div><br />
<div>来源:<input type="text" name="source" style="width:180px; height:30px" value="<?php echo $arr[3] ?>"/></div><br />
<div>内容:<textarea name="content" cols="80" rows="10" value=""><?php echo $arr[4] ?></textarea></div><br />


<div class="anniu"><input type="submit" value="修改" /></div>

<div class="anniu" style="margin-left:20px"><a href="zhu.php"><input type="button" value="查看" />
</a>
</div>

</form>

</body>
</html>

  

修改时的页面显示:

修改处理新闻页面-----修改处理:xiugaichuli.php

<?php

	$id = $_POST["id"];
	$title = $_POST["title"];
	$author = $_POST["author"];
	$source = $_POST["source"];
	$content = $_POST["content"];
	$time = date("Y-m-d",time());

	$db = new MySQLi("localhost","root","","newssystem");
	$sql = "Update news set title='{$title}',author='{$author}',source='{$source}',content='{$content}',time='{$time}' where id='{$id}'";
	$result = $db->query($sql);
	if($result)
	{
		header("location:zhu.php");	
	}
	else
	{
		echo "修改失败";	
	}

  

以上是对于新闻内容添加,修改,删除的练习题

posted @ 2016-05-06 11:55  Durriya  阅读(137)  评论(0编辑  收藏  举报