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

本文为了简单并能够说明主要内容,一些次要的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— ?ph
$query=select * from .$name. order by id desc limit 0,5;
$result=mysql_query($query,$db); if ($result){
while($myrow=msyql_fetch_array($result)){ ? trtda
href=view_d.php?recod=?php echo $myrow[id]; ?&name=?php echo $name;
?$myrow[title]/a/td/tr ?php } } else{ echo 这里还没新的内容。;} ?
—-index.php— ?php $id=mysql_connect(localhost,username,password);
$db=mysql_select_db(your_db,$id); ? html body hr size=0 color=green
width=100% p align=leftfont color=green size=+3Yourname Online/font/p hr
size=3 color=green width=100% p align=leftfont
size=-1你现在的位置–首页/font/p hr size=2 color=green width=100% table
width=100 border=0 cellpadding=0 cellspacing=0 tr !– news — td
width=50% align=left table width=100 border=0 cellpadding=0
cellspacing=0 ?php $name=news; include(index_view.php); ? /table /td
!– ctm — td width=50% align=left table width=100 border=0
cellpadding=0 cellspacing=0 ?php $name=ctm; include(index_view.php); ?
/table /td /tr /table hr size=0 width=100% color=green p font
size=-1Copyrignt 1999…/font/P /body /html —-index_pub.php— ?php
$id=mysql_connect(localhost,username,password);
$db=mysql_select_db(your_db,$id); ? html body form
action=index_view.php method=post p请选择数据库:br select
name=db_name size=1 option value=newsnews/option option
value=ctmctm/option /select/p p标题:br input type=text name=title
size=20/p p内容:br textarea rows=6 cols=10 name=detail/textarea/p
pinput type=submit value=submit/p /form ?php 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;} ? /body /html —-view_d.php— ?php
$id=mysql_connect(localhost,username,password);
$db=mysql_select_db(your_db,$id); ? html body ?php if ($recod){
$query=select * from .$name. where id=.$recod;
$result=mysql_query($query,$db); $title=mysql_result($result,0,title);
$detail=mysql_result($result,0,detail); echo p标题:.$title./p; echo
p内容:.$detail./p; } else{echo 此文件已被删除!;} /body /html

——————————-论坛————————– 数据表
create table guest(id int not null auto_increment,primary key(id),title
varchar(50),email varchar(30),homepage varchar(30),name varchar(30),text
text,relay int,r_time date) create table relay(id int not null
auto_increment,primarykey(id),title int,email varchar(30),name
varchar(30),text text)
————————-index.php————————–
STYLETD{FONT-FAMILY:宋体;FONT-SIZE:13pt}BODY{FONT-FAMILY:宋体;FONT-SIZE:13pt}SELECT{FONT-FAMILY:宋体;FONT-SIZE:13pt}A{COLOR:blue;FONT-FAMILY:宋体;FONT-SIZE:13pt;TEXT-DECORATION:none}/STYLE
div p font size=14b网站完全手册/font/p/div ?php $db
=mysql_connect(localhost, ****,****);
mysql_select_db(****,$db); @$result = mysql_query(SELECT * FROM
guest order by id desc,$db); $i=0; while($myrow =
mysql_fetch_row($result)) { $guestbook[$i][0] = $myrow[0];
$guestbook[$i][1] = $myrow[1]; $guestbook[$i][2] =
$myrow[2]; $guestbook[$i][3] = $myrow[4];
$guestbook[$i][4]=$myrow[6]; $guestbook[$i][5]=$myrow[7];
$i++; }//读入数组 mysql_close($db) ; echo
—————————–共[; if ($q!=) { $page = $q; } else {
$page = 0; } $i=count($guestbook); $msgnum=20;// 每页20笔 $start = $page
* $msgnum; $end = $start + $msgnum; if ($end $i) $end=$i;
$totalpage=$i/$msgnum; for ($i=0; $i$totalpage; $i++) { if ($i!=$page) {
echo a href=index.php?q=$i $i/a; } else { echo $i; } } echo
]页—————————–br;printf(p align=righta
href=111.php添加主题/a/p); echo br; echo table width=700 hight=560
border=1 ; echo tr;echo td width=200 bgcolor=aaaaaa border=1
style=border: 1px solid rgb(3,2,1)主题/td; echo td width=200
bgcolor=aaaaaa border=1 style=border: 1px solid rgb(3,2,1)电子邮件/td;
echo td width=80 bgcolor=aaaaaa border=1 style=border: 1px solid
rgb(3,2,1)呢称/td; echo td width=60 bgcolor=aaaaaa border=1
style=border: 1px solid rgb(3,2,1)回复/td; echo td width=200
bgcolor=aaaaaa border=1 style=border: 1px solid
rgb(3,2,1)最后回复时间/td; echo /tr; for ($i=$start; $i$end; $i++) {
$id=$guestbook[$i][0]; echo tr ;echo td width=200 bgcolor=bbbbbb
border=0 style=border: 1px solid rgb(3,2,1); echo a
href=444.php?id=$id;echo $guestbook[$i][1]; echo /a; echo /td; echo
td width=200 bgcolor=bbbbbb border=0 style=border: 1px solid rgb(3,2,1);
echo a href=mailto:.$guestbook[$i][2].;echo
$guestbook[$i][2];echo /a;echo /td; echo td width=80 bgcolor=bbbbbb
border=0 style=border: 1px solid rgb(3,2,1); echo $guestbook[$i][3];
echo /td; echo td width=60 bgcolor=bbbbbb border=0 style=border: 1px
solid rgb(3,2,1); echo $guestbook[$i][4]; echo /td; echo td
width=200 bgcolor=bbbbbb border=0 style=border: 1px solid rgb(3,2,1);
echo $guestbook[$i][5]; echo /td; echo /tr; } echo /table; echo br;
printf(a href=111.php添加主题/a); ?
——————————444.php——————————–
STYLETD{FONT-FAMILY:宋体;FONT-SIZE:13pt}BODY{FONT-FAMILY:宋体;FONT-SIZE:13pt}SELECT{FONT-FAMILY:宋体;FONT-SIZE:13pt}A{COLOR:blue;FONT-FAMILY:宋体;FONT-SIZE:13pt;TEXT-DECORATION:none}/STYLE
?php $db =mysql_connect(localhost, ****,****);
mysql_select_db(****,$db); @$result = mysql_query(SELECT * FROM
guest where id=’$id’,$db); echo table width=700 border=1 ; $myrow =
mysql_fetch_row($result); echo tr higth=20;echo td width=700
bgcolor=888888 border=1 style=border: 1px solid rgb(3,2,1)主题:; echo
$myrow[1]; echo /td; echo /tr; $msg=base64_decode($myrow[5]); $msg
= ereg_replace( ,,$msg);//空格处理 $msg=nl2br($msg);// 换行处理 if
($myrow[3]!=){$myrow[3]=a href=$myrow[3]$myrow[3]/a;} else
{$myrow[3]=我还没在网上安家;} echo tr higth=60; echo td width=700
bgcolor=bbbbbb border=1 style=border: 1px solid rgb(3,2,1); echo
$msg;echo /td;echo /tr; echo tr higth=20;echo td width=700
bgcolor=bbbbee border=1 style=border: 1px solid rgb(3,2,1)主页:; echo
$myrow[3]; echo /td/tr; echo tr higth=20;echo td width=700
bgcolor=888888 border=1 style=border: 1px solid rgb(3,2,1);echo
回复列表; echo /td; echo /tr; $num=$myrow[6]; @$result1 =
mysql_query(SELECT * FROM relay where title=’$id’ order by id
desc,$db); $i=0; while($myrow1 = mysql_fetch_row($result1)) {
$guestbook[$i][0] = $myrow1[1]; $guestbook[$i][1] =
$myrow1[2]; $guestbook[$i][2] = $myrow1[3];
$guestbook[$i][3]=$myrow1[4]; $i++; } mysql_close($db); if ($q!=)
{ $page = $q; } else { $page = 0; } $i=count($guestbook); $msgnum=5;//
每页5笔 $start = $page * $msgnum; $end = $start + $msgnum; if ($end $i)
$end=$i; $totalpage=$i/$msgnum; for ($i=$start; $i$end; $i++) {
$t=$guestbook[$i][0]; echo tr ;echo td width=700 bgcolor=aaaaaa
border=0 style=border: 1px solid rgb(3,2,1); echo Re:; echo
$myrow[1];echo /td;echo /tr; echo tr ; echo td width=700
bgcolor=bbbbbb border=0 style=border: 1px solid rgb(3,2,1); echo
$guestbook[$i][2]; echo /td;echo /tr; echo tr ; echo td width=700
bgcolor=bbbbee border=0 style=border: 1px solid rgb(3,2,1)电子邮件:;
echo a href=mailto:.$guestbook[$i][1].;echo
$guestbook[$i][1];echo /a;echo /td;echo /tr;
$msg=base64_decode($guestbook[$i][3]); $msg = ereg_replace(
,,$msg); $msg=nl2br($msg); echo tr ;echo td width=700 bgcolor=bbbbbb
border=0 style=border: 1px solid rgb(3,2,1); echo $msg; echo /td;echo
/tr; } echo /table; echo br; echo br;echo
——————————-共[; for ($i=0; $i$totalpage; $i++) { if
($i!=$page) { echo a href=444.php?q=$i&id=$id $i/a; } else { echo$i; }
}echo ]页———————————;echo br; echo a
href=333.php?t=$id&num=$num回复/a;echo br; echo a
href=index.php浏览主题/a; ?
——————————–333.php—————————–
html head ?php if (!$t){ if ($text==&$email==&$name==){echo *必填;echo
a
href=333.php?t=$title&name=$name&email=$email&text=$text&num=$num返回/a;
} else {if
(eregi(^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$,$email)){
$text = base64_encode($text);//检验邮件地址是否合法 $db
=mysql_connect(localhost, ****,****); $time=date(y-m-d);
mysql_select_db(****,$db); $sql=insert into relay
values(”,’$title’,’$email’,’$name’,’$text’); $result =
mysql_query($sql); $num=$num+1; $sql=update guest set
relay=’$num’,r_time=’$time’ where id=’$title’;
$result=mysql_query($sql); mysql_close(); echo META HTTP-EQUIV=REFRESH
CONTENT=’1; URL=444.php?id=$title’; } else {echo 邮件地址无效;echo a
href=333.php?t=$title&name=$name&text=$text&num=$num返回/a;} }} else
{?FORM ACTION=333.php METHOD=post table border=0 cellpadding=2 width=395
tr td width=20%input type=hidden name=title value=? echo $t; ?/td/tr tr
td nowrapfont color=004080呢称:*/font/td td width=10%input type=text
name=name size=10 value=? echo $name; ?/td /tr tr td nowrapfont
color=004080邮件:*/font/td td width=20%input type=text name=email
size=20 value=? echo $email; ?/td /tr tr td width=20%input type=hidden
name=num value=? echo $num; ?/td /tr tr td nowrapvalign=topfont
color=004080内容:*/font/td td width=80% colspan=3textarea rows=5
name=text cols=50 size=255? echo $text; ?/textarea/td /tr tr td
width=100% colspan=4 input type=submit value=送出 input type=reset
value=擦掉 /td /tr /table /FORM?} ? /head body /html
———————————– HTML HEAD ?php if
($title!=&$email!=&$name!=){ if
(eregi(^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$,$email)){
$text = base64_encode($text);//检验邮件地址是否合法 $db
=mysql_connect(localhost, ****,****); $time=date(y-m-d);
mysql_select_db(****,$db); $sql=insert into guest
values(”,’$title’,’$email’,’$homepage’,’$name’,’$text’,”,’$time’);
$result = mysql_query($sql);mysql_close(); echo META
HTTP-EQUIV=REFRESH CONTENT=’1; URL=index.php’; } else { echo
邮件地址无效; echo a
href=111.php?title=$title&&homepage=$homepage&name=$name&text=$text返回/a;}}
else {echo *必填; echo a
href=111.php?title=$title&email=$email&homepage=$homepage&name=$name&text=$text返回/a;}
//mysql_close($db) //printf(留言成功a href=222.php按此返回/a); ? /HEAD
BODY /BODY /HTML
——————————–111.php————————— html
head /head body FORM ACTION= METHOD=post table border=0 cellpadding=2
width=395 tr td nowrapfont color=004080标题:*/font/td td width=20%input
type=text name=title size=30 value=? echo $title; ?/td/trtr td
nowrapfont color=004080邮件:*/font/td td width=20%input type=text
name=email size=30 value=? echo $email; ?/td /tr tr td nowrapfont
color=004080主页:/font/td td width=25%input type=text name=homepage
size=30 value=? echo $homepage; ?/td /tr tr td nowrapfont
color=004080呢称:*/font/td td width=10%input type=text name=name
size=10 value=? echo $name; ?/td /tr tr td nowrapvalign=topfont
color=004080内容:/font/td td width=80% colspan=3textarea rows=5
name=text cols=50 size=255 ? echo $text; ?/textarea/td /tr tr td
width=100% colspan=4 input type=submit value=送出 input type=reset
value=擦掉 /td /tr /table /FORM /body /html

本系统可以录入标题、内容、图片、相关软件,显示时会根据是否有有图片和相关软件来判断是否显示该项内容,打开页面显示的是最新的一片文章,点击目录会显示之前的文章。
建立channelimages和channelsoft目录,并把权限设为777。
*********************************
news表结构
********************************* #
Host: localhost Database : yourdb #
——————————————————– # # Table
structure for table ‘yourtable’ # CREATE TABLE news( id int(11) DEFAULT
‘0’ NOT NULL auto_increment, kind varchar(16), title varchar(60),
content text, pic varchar(30), timer datetime DEFAULT ‘0000-00-00
00:00:00’ NOT NULL, soft varchar(30), PRIMARY KEY (id), KEY timer
(timer) );
*********************************
新闻录入文件:insert.htm
********************************* html
head title新闻录入/title meta content=text/html; charset=gb2312 /head
body bgcolor=#FFFFFF form name=form1 enctype=multipart/form-data
action=insert.php method=post table width=81% border=0 cellspacing=2
cellpadding=0 align=center trtd width=11% align=right
bgcolor=#CC0000font color=#FFFFFF标题:/font/td td
bgcolor=#E7E7E7input type=text name=title size=60 /td /tr trtd
align=right bgcolor=#CC0000font color=#FFFFFF类别:/font/td td
bgcolor=#E7E7E7input type=radio name=kind value=类型1 类型1input
type=radio name=kind value=类型2 类型2 input type=radio name=kind
value=类型3 类型3 input type=radio name=kind value=类型4 类型4/td /tr
trtd width=11% align=right bgcolor=#CC0000font
color=#FFFFFF内容:/font/td td bgcolor=#E7E7E7textarea name=content
cols=60 rows=10/textarea /td /tr trtd width=11% align=right
bgcolor=#CC0000font color=#FFFFFF图片:/font/td td
bgcolor=#E7E7E7input type=file name=pic size=60 /td /tr trtd width=11%
align=right bgcolor=#CC0000font color=#FFFFFF软件:/font/td td
bgcolor=#E7E7E7input type=file name=soft size=60 /td /tr trtd width=11%
align=right bgcolor=#CC0000/td td bgcolor=#E7E7E7div align=centerinput
type=submit name=Submit value=提 交 /div /td /tr /table /form
/body/html*********************************
end of insert.htm
*********************************
*********************************
insert.php
********************************* ?
$day=date(md); If($pic != none){ $picname=$day.$pic_name;
copy($pic,channelimages/$picname); unlink($pic); } If($soft!= none){
copy($soft,channelsoft/$soft_name); unlink($soft); } $dbh
=mysql_connect(‘localhost’,’uesrname’,’passward’);
mysql_select_db(‘yourtable’);$dat=date(Y-m-d h:i:s); $query=insert
into news(title,kind,content,pic,timer,soft)
values(‘$title’,’$kind’,’$content’,’$picname’,now(),’$soft_name’); $res
= mysql_query($query,$dbh);$err = mysql_error(); if($err){echo
$err;exit();} echo p/pp ; echo bodyh2录入成功/h2/body/p; ?
********************************* end
of insert.php
*********************************
*********************************
新闻显示文件:news.php
********************************* html
head title显示类型1/title meta 目录类型 content=文本/html; 字符集=gb2312
/head body bgcolor=#FFFFFF ?
$db=mysql_connect(localhost,username,passward);
mysql_select_db(yourtable,$db); $result=mysql_query(select id,title
from news where kind=’类型1′ order by timer desc); for($a=0;$a10;$a++)
{if(!($ahrow=mysql_fetch_row($result)))break;
$id=mysql_result($result,$a,id);
$title=mysql_result($result,$a,title); echo a
href=news.php?id=.$id..$title./abr; } if (!isset($id))
$id=mysql_result($result1,0,id); $result=mysql_query(select * from
news where id=$id order by timer desc);
$rows=mysql_fetch_row($result); echo brcenterfont color=ff0000; print
$rows[2]; echo /fontbr$rows[5]/center; if (strlen($rows[4])0) echo
pimg src=channelimages/$rows[4] align=right; echo
str_replace(/r,br,str_replace( ,,$rows[3])); if
(strlen($rows[6])0) echo /pp align=righta
href=channelsoft/$rows[6]点击下载/a/p; ? /body /html
********************************* end
of news.php
*********************************

发表评论

电子邮件地址不会被公开。 必填项已用*标注