PHP实现留言版

开发环境:Ubuntu11.10 ide:Netbeans

文件结构:

conn.php:负责管理数据库链接;

list.php:显示留言页面;

head.php:头部链接;

add.php:添加留言页;

代码:

//conn.php

<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
$db_host='localhost';
$db_user='root';
$db_pass='1';
$db_name='bbs';
$conn=@mysql_connect($db_host,$db_user,$db_pass) or die("error");
mysql_select_db("bbs",$conn);
mysql_query("set names 'utf8'");
//修正添加空格与换行
function htmtocode($content) {
$content = str_replace("\n", "<br>", str_replace(" ", "&nbsp;", $content));
return $content;
}
?>

//list.php

<?php
include ("conn.php")
?>
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<?
$sql="select * from message ";
$query= mysql_query($sql);

//循环显示留言
while($row= mysql_fetch_array($query))
{
?>
<tr bgcolor="#eff3ff">
<td>标题:<?=$row[title]?> 用户:<?=$row[user]?> </td>
</tr>
<tr bgColor="#ffffff">
<td>内容:<?=htmtocode($row[content])?> </td>
</tr>
<?
}
?>
</table>


//head.php

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<link href="images/css.css" rel="stylesheet" type="text/css">
<b><a href="add.php">添加留言</a> | <a href="list.php">浏览留言</a> | <a href="login.php">登陆</a> </b>
<hr size=1>



//add.php

<?php


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
include ("conn.php");


if($_POST['user'])
{
$sql="insert into `message` (`id`,`user`,`title`,`content`,`lastdate`)".
"values
('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo "发表成功!";
}
include("head.php");
?>

利用javascript验证表单
<script language="javascript">
function CheckPost()
{
if (myform.user.value=="")
{
alert("请填写用户名");
myform.user.focus();
return false;
}
if (myform.title.value.length<5)
{
alert("标题不能少于5个字符");
myform.title.focus();
return false;
}
if (myform.content.value=="")
{
alert("必须要填写留言内容");
myform.content.focus();
return false;
}
}


</script>
<form action="add.php" method="post" name="myform" onsubmit="return CheckPost();">
用户:<input type="text" name="user" size="10"/></br>
标题:<input type="text" name="title"/></br>
内容:<textarea name="content" cols="60" rows="9"></textarea></br>
<input type="submit" name="submit" value="发布留言"/>
</form>


PHP实现留言版

你可能感兴趣的:(PHP)