语音播报
新闻列表显示(List.php )
显示详细的新闻内容(View.php )
添加新闻 (news_add.html news_add.php)
//connect.php连接到数据库
<?php //连接到 mysql
$db_host='localhost';
$db_user='root';
$db_pass='';
$conn=mysql_connect($db_host,$db_user,$db_pass)or die(mysql_error()."failed to connect to database!"); //连接数据库;
$db=mysql_select_db('news1',$conn);
?>
//List.php
<?php
include "connect.php";
$page=$_GET["page"]+0;
$page=$page<=0?1:$page;
$page_size=10;
$query="SELECT id from news";
$result=mysql_query($query,$conn);
$total_records=mysql_num_rows($result);
$total_page=ceil($total_records/$page_size);
$page=$page>$total_page?$total_page:$page;
$offset=($page-1)*$page_size;
$query="SELECT * from news ORDER by id desc Limit $offset,$page_size";//第一个参数指定第 一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目
$result=mysql_query($query,$conn);
while($row=mysql_fetch_array($result)){
echo "<p>";
echo "<table >";
echo "<tr>";
echo "<td>";
echo "<a href=View.php?id"].">".$row["title"]."</a>";
echo "</td>";
echo "<td>";
echo "[".$row["author"]."]";
echo "</td>";
echo "<td>";
echo "[".$row["date"]."]";
echo "</td>";
echo "<td>";
echo "<a href=Showedit.php?id"].">修改信息 </a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
$prev_page=$page-1;
$next_page=$page+1;
if ($page<=1){
echo "首页 |";
}
else{
echo "<a href=$_SERVER[PHP_SELF]?page=1>首页 |</a>";
}
if($prev_page<1){
echo "上一页 |";
}
else{
echo "<a href=$_SERVER[PHP_SELF]?page=$prev_page>上一页 |</a>"; }
if ($next_page>$total_page){
echo "下一页 |";
}
else{
echo "a href=$_SERVER[PHP_SELF]?page=$next_page>下一页 |</a>"; }
if ($page>=$$total_page){
echo "最后一页 </p>\n";
}
else{
echo "<a href=$_SERVER[PHP_SELF]?page=$total_page>最后一页 </a>"; }
?>
<a href="news_add.html">发布信息 </a>
</html>
//View.php
<?php
include "connect.php";
$id"];
if($id){
$query="SELECT * from news WHERE id"."=".$id;
$result=mysql_query($query,$conn) or die(mysql_error());
$row=mysql_fetch_array($result);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title><?php echo $_GET["title"]?></title>
</head>
<body>
<?php
echo "<table align='center' >";
$i=0;
while ($i<$rows=mysql_num_rows($result)){
echo "<tr>";
echo "<td align='center'>";
echo "<a href=List.php?id"].">".$row["title"]."</a>"; echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'>";
echo "作者:".$row["author"]."发表时间 ".$row["time"]; echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='left'>";
echo "".$row["content"]."";
echo "</td>";
echo "</tr>";
$i++;
}
echo "</table>";
}
?>
<tr>
<td><center><a href="List.php">返回列表 </a></center></td>
</body>
</html>
//news_add.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻录入 </title>
</head>
<body alink="#FF0000">
<br>
<h1 align="left">新闻录入界面 </h1>
<form action="news_add.php" method="post">
<table >
<tr>
<td>标题 </td>
<td><input type="text" name="title" maxlength="60" size="30"></td>
</tr>
<tr>
<td>作者 </td>
<td><input type="text" name="author" maxlength="30" size="30"></td>
</tr>
<tr>
<td>内容 </td>
<td >
<textarea rows="12" name="content" cols="51"></textarea></td>
<td ></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交 "></td>
</tr>
</table>
</form>
</body>
</html>
//news_add.php
<?php
$conn=mysql_connect('localhost','root','')or die(mysql_error()."failed to connect to database!"); //连接数据库;
$db = mysql_select_db("news1",$conn);
$title=$_POST['title'];
$author=$_POST['author'];
$content=$_POST['content'];
$date=date("y-m-d H:i");
if(!$title||!$author||!$content){
echo "You have not entered all the required details.<br/>";
exit;
}
if(!get_magic_quotes_gpc()){
$title=addslashes($title);
$author=addslashes($author);
$content=addslashes($content);
}
if(mysqli_connect_errno()){
echo"Error: Could not connect to database.Please try again later.";
exit;
}
$query="INSERT into news (title,author,content,date)values('$title','$author','$content','$date')";
$result=mysql_query($query,$conn);
if($result){
echo mysql_affected_rows()." row(s) insert into database.";}
else{
echo "An error has occurred.The item was not added.";}
mysql_close($conn);
echo '<br><center><a href="List.php">返回列表 </a></center>';
?>
</body>
</html>