创建表:
CREATE TABLE 'message'
( 'id' tinyint(1) NOT NULL auto_increment,
'user' varchar(25) NOT NULL,
'title' varchar(50) NOT NULL,
'content' tinytext NOT NULL, `lastdate` date NOT NULL,
PRIMARY KEY ('id') )
ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
连接MySQL数据库:conn.php
<?php
$conn = @mysql_connect('localhost','root','') or die('数据库链接错误');
mysql_select_db('bbs',$conn);
mysql_query("set names 'utf8'");
?>
head.php
<html>
<body>
<h2><a href="add.php">添加留言</a>|<a href="list.php">查看留言</a>|<a href="alter.php">修改留言</a>|<a href="delete.php">删除留言</a></h2>
</body>
</html>
添加留言:add.php
<?php
include("D:Web/BBS/conn.php");
include("D:Web/BBS/head.php");
if(isset($_POST['submit']) && $_POST['submit']) {
$sql="insert into message(ID,user,title,content,lastdate)
value ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo "发布成功!";
}
?>
<html>
<body>
<form action="add.php" method="post">
用户:<input type="text" name="user" required/><br />
标题:<input type="text" name="title" required/><br /><br />
内容:<textarea rows="15" cols="40" name="content"required></textarea><br />
<input type="submit" name="submit" value="发布留言"/>
</form>
</body>
</html>
查看留言:list.php
<?php
include("D:Web/BBS/conn.php");
include("D:Web/BBS/head.php");
?>
<table width=500 border='0' align='center' cellpadding='5' cellspacing='1' bgcolor='#add3ef'>
<?php
$sql="select * from message order by ID desc";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)) {
?>
<tr bgcolor='#eff3ff'>
<td>标题:<?php echo $row['title']; ?>
用户:<?php echo $row['user']; ?></td>
</tr>
<tr bgcolor='#ffffff'>
<td>内容:<?php echo $row['content']; ?>
<a href="alter.php?id=<?php echo $row['ID']; ?>">修改留言</a>
<a href="delete.php?id=<?php echo $row['ID']; ?>">删除留言</a>
</td>
</tr>
<?php
}
?>
</table>
修改留言:alter.php
<?php
include("D:Web/BBS/conn.php");
include("D:Web/BBS/head.php");
if(isset($_POST['submit']) && $_POST['submit']) {
$sql="update message set user='$_POST[user]',title='$_POST[title]',content='$_POST[content]'where ID=".$_POST['id']."";
mysql_query($sql);
echo "修改成功!";
}
if(isset($_GET['id'])){
$sql="select * from message where ID=".$_GET['id']."";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="alter.php" method="post">
<input type='hidden' name="id" value="<?php echo $row['ID'] ?>" />
用户:<input type="text" name="user" value="<?php echo $row['user'] ?>" /><br />
标题:<input type="text" name="title" value="<?php echo $row['title'] ?>" /><br /><br />
内容:<textarea rows="15" cols="40" name="content"><?php echo $row['content'] ?></textarea><br />
<input type="submit" name="submit" value="修改留言"/>
</form>
</body>
</html>
<?php
}
?>
删除留言:delete.php
<?php
include("D:Web/BBS/conn.php");
include("D:Web/BBS/head.php");
if(isset($_GET['id'])){
$sql="delete from message where ID=".$_GET['id']."";
mysql_query($sql);
echo "删除成功!";}
?>