多数据表共用一个页的新闻发布

本文为了简单并能够说明主要内容,一些次要的html内容相对简单。
在网站有多个内容要在某一页显示时可在网页中共用一个显示和提交。
本例中有两个数据表(news,ctm);一个主页(index.php);
一个提交页(index_pub.php;和一个包函页(index_view.php)
一个子页(view_d.php)。
----news,ctm---
increate table news(id int(80) not null auto_increment,title char(100),detail text,primay key(id));
increate table ctm(id int(80) not null auto_increment,title char(100),detail text,primay key(id));

----index_view.php---

1<tr><td><a href="view_d.php?recod=```
2 echo $myrow[id]; 
3```&amp;name=```
4 echo $name; 
5```">$myrow[title]</a></td></tr>
1   
2}   
3}   
4else{   
5echo "这里还没新的内容。";}   

----index.php---

1   
2$id=mysql_connect("localhost","username","password");   
3$db=mysql_select_db("your_db",$id);   
 1<html>
 2<body>
 3<hr color="green" size="0" width="100%"/>
 4<p align="left"><font color="green" size="+3">Yourname Online</font></p>
 5<hr color="green" size="3" width="100%"/>
 6<p align="left"><font size="-1">你现在的位置--&gt;首页</font></p>
 7<hr color="green" size="2" width="100%"/>
 8<table border="0" cellpadding="0" cellspacing="0" width="100">
 9<tr>
10<!-- news -->
11<td align="left" width="50%">
12<table border="0" cellpadding="0" cellspacing="0" width="100">   

$name=news;
include("index_view.php");

1</table>
2</td>
3<!-- ctm -->
4<td align="left" width="50%">
5<table border="0" cellpadding="0" cellspacing="0" width="100">   

$name=ctm;
include("index_view.php");

1</table>
2</td>
3</tr>
4</table>
5<hr color="green" size="0" width="100%"/>
6<p align="center"><font size="-1">Copyrignt 1999…</font></p>
7</body>
8</html>

----index_pub.php---

1   
2$id=mysql_connect("localhost","username","password");   
3$db=mysql_select_db("your_db",$id);   
 1<html>
 2<body>
 3<form action="index_view.php" method="post">
 4<p>请选择数据库:<br/>
 5<select name="db_name" size="1">
 6<option value="news">news</option>
 7<option value="ctm">ctm</option>
 8</select></p>
 9<p>标题:<br/>
10<input name="title" size="20" type="text"/></p>
11<p>内容:<br/>
12<textarea cols="10" name="detail" rows="6"></textarea></p>
13<p><input type="submit" value="submit"/></p>
14</form>   

switch ($db_name){
case news:$name=news;
break;
case ctm:$name=ctm;
break;
}
$query="insert into ".$name."(title,detail) values('$title','$detail');
$result=mysql_query($query,$db);
if ($result){echo "ok";}
else{echo "failed";}

1</body>
2</html>

----view_d.php---

1   
2$id=mysql_connect("localhost","username","password");   
3$db=mysql_select_db("your_db",$id);   
 1<html>
 2<body>
 3<?php   
 4if ($recod){   
 5$query="select * from ".$name." where id=".$recod;   
 6$result=mysql_query($query,$db);   
 7$title=mysql_result($result,0,title);   
 8$detail=mysql_result($result,0,detail);   
 9echo "<p>标题:".$title."";   
10echo "<p>内容:".$detail."</p>";   
11}   
12else{echo "此文件已被删除!";}   
13</body>
14</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus