PHP+MYSQL 简易新闻栏目实例


这里我的例子是利用MySQL数据库的方式制作的,代码如下:

  一:发布新闻文件pub.php和pub_ok.php,代码如下:

  文件pub.php:

<form name="form1" method="post" action="pub_ok.php" onsubmit="javascript:return Juge(this);" >
 <table width="90%" border="0" cellspacing="1" cellpadding="1" align="center">
  <tr> <td> 标题: </td></tr>
  <tr> <td> <input type="text" name="title" size="80"> </td> </tr>
  <tr> <td> 详细内容:</td></tr>
  <tr> <td> <textarea name="detail" style="width=80%;height=160px;">>/textarea<
   </td></tr>
  <tr><td><input type="submit" name="pub" value="发布"></td></tr>
 </table>
 </form>
  文件pub.php:

<?php include "common_news.php"; ?>
  <html>
  <head>
  <title>新闻发布</title>
  </head>
  <body bgcolor=#cccccc><?php
  if ($pub){   $title=htmlspecialchars($title);
  
$detail=htmlspecialchars($detail);
  
$detail= str_replace ( "
","<br>", $detail);   $pub_time=date("Y")."-".date("m")."-".date("d")." ".date("H").":".date("i").":".date("s");
  
$query="insert into news (title,detail,pub_time) values ";
  
$query.="('$title','$detail','$pub_time')";
  
$result=mysql_query($query,$db);
  
echo "<div align='center'>";
  
if ($result){
  
echo "<p>发布成功!<a href='view.php?page=1'>查看</a>、<a href='pub.php'>发布</a></p>";
  
}
  
else{
  
echo mysql_error();
  
echo "<p>发布失败!</p>";
  
}
  
echo "</div>";
  }
  ?>
  </body>
  </html>  说明:上面用了一个包含文件common_news.php,其中$page_size变量为一页最多显示的新闻数,其余的大家应该一看就明白。<?php
  
$id=mysql_connect('localhost','root','cgreen');
  
$db=mysql_select_db('db_news',$id);
  
$page_size=5;
  ?>
  显示新闻条目文件:view.php

 

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3