Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)归海一刀
published in(发表于) 2014/2/10 6:53:56
打造简单的PHP&MYSQL留言板_[PHP教程]

打造简单的PHP&MYSQL留言板_[PHP教程]

















数据库结构:(库名:lyb)




表一: admin
字段: id(int11) name(varchvr) password(varchvr)




表二: lo
字段: id(int11) username(varchvr) sex(varchvr) qq(varchvr) email(varchvr) info(text) ip(varchvr) submit_time(datetime)




1、conn.php(连接数据库文件)




mysql_connect("localhost","root","");//连接数据库
mysql_select_db("lyb");//选择数据库
?>




2、header.php(公用头部文件)




http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



银子留言板 Version 1.0






3、footer.php(公用底部文件)







counterFile="conter.xml";
function displayCounter(counterFile){
fp = fopen(counterFile,"rw");
num = fgets(fp,5);
num += 1;
print "

银子留言板 Version 1.0   您是第 "."num"." 位无聊的银
";
exec("rm -rf counterFile");
exec("echo num > counterFile");
}
if(!file_exists(counterFile)){
exec("echo 0 > counterFile");
}
displayCounter(counterFile);
?>








4、index.php(首页)







require_once("conn.php");
require_once("header.php");
session_start();




//分页代码开始
pagesize = 10;//设置每页显示条数
rs = mysql_query("select count(*) from lo");//取得记录总数,计算总页数用
myrow = mysql_fetch_array(rs);
numrows = myrow[0];//计算总记录




pages = intval(numrows/pagesize);
if(numrows%pagesize)pages++;//设置页数
if(isset(_GET['page']))
{
page = intval(_GET['page']);
}
else
{
page = 1;//设为第一页
}
offset = pagesize*(page-1);//计算记录偏移量
//分页代码结束





sql = "select id,username,sex,qq,email,info,ip,DATE_FORMAT(submit_time, '%Y年%m月%d日 %T' ) from lo order by id desc limit offset,pagesize";//用到了DATE-FORMAT格式化日期格式
result = mysql_query(sql);
num = mysql_num_rows(result);




if(num>0){
while(row = mysql_fetch_array(result))
{
//echo print_r(row);
if(row[2]=="男")//这个使性别改成你想要的名称^_^
{
sex = "帅锅";
}
else
{
sex = "美女";
}





?>





第 [] 条留言


留言人:  性别:  留言时间:     IP:  更改 删除


留言内容:




}
}
else
{
echo "
无数据......
";
}
?>






first=1;
prev=page-1;
next=page+1;
last=pages;
if(page==1&&pages>1)
{
echo "首页 | ";
echo "上一页 | ";
echo "下一页 | ";
echo "尾页 | ";
}
elseif(page>=1&&page!=pages&&num>0)
{
echo "首页 | ";
echo "上一页 | ";
echo "下一页 | ";
echo "尾页 | ";
}
elseif(page==pages&&page!=1)
{
echo "首页 | ";
echo "上一页 | ";
echo "下一页 | ";
echo "尾页 | ";
}
elseif(page==pages)
{
echo "首页 | ";
echo "上一页 | ";
echo "下一页 | ";
echo "尾页 | ";
}
else
{
echo "首页 | ";
echo "上一页 | ";
echo "下一页 | ";
echo "尾页 | ";
}
?>
共  页 | 当前第  页 | 共 &nbsp条留言






mysql_close();
?>




require_once("footer.php");
?>







5、post.php(提交留言页面)




require_once("header.php");
?>



提交留言


姓名 : 


性别 : 帅锅  美女


Q Q : (可选填)


Email: (可选填)


留言内容:







银子留言板 Version 1.0








6、input.php(插入留言)







require_once("conn.php");





username = _POST['name'];
sex = _POST['sex'];
qq = _POST['qq'];
email = _POST['email'];
info = _POST['info'];




if (strrpos(username,"<")!==false || strrpos(username,">")!==false||strrpos(username,"@")!==false||strrpos(username,"\"")!==false||strrpos(username,"'")!==false||strrpos(username,"_")!==false)
{
echo "";
exit();
}




if (!ereg("^[0-9]{0,}",qq))//用正则检查QQ格式
{
echo "";
exit();
}




if(email)
{//如果填写了邮箱就用正则检查邮箱格式
if (!ereg("^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+",email))
{
echo "";
exit();
}
}




if(!username)
{
echo "";
exit();
}
elseif(!info)
{
echo "";
exit();
}
else
{
ip = getenv('REMOTE_ADDR');//获取客户端IP地址




sql = "insert into lo (username,sex,qq,email,info,ip,submit_time) values ('username','sex','qq','email','info','ip',NOW())";




result = mysql_query(sql);




mysql_close();




echo "";
}
?>




7、update.php(修改留言页)







session_start();
require_once('conn.php');
require_once('header.php');





if(_SESSION["key"]==1)
{
id = _GET['id'];
sql = "select * from lo where id= ".id;
result = mysql_query(sql);
row = mysql_fetch_array(result);




?>






修改留言


姓名:


留言:




银子留言板 Version 1.0









}
else
{
header('location:index.php');
}
?>




8、updatepost(修改提交页)







require_once('conn.php');




username = _POST['name'];
info = _POST['info'];
id = _GET['id'];
//echo id;




sql = "update lo set username= '".username."',info='".info."' where id=".id;
mysql_query(sql);




echo "";
?>




9、delete.php(删除留言页)







session_start();
require_once('conn.php');




id=_GET['id'];




if(_SESSION["key"]==1)
{
sql = "delete from lo where id=".id;
mysql_query(sql);
echo "";
}
else
{
header('location:index.php');
}
?>




10、admin.php(管理登录页)







require_once('conn.php');
require_once('header.php');
?>






管理员登录


姓名 : &nbsp;


密码 : &nbsp;



银子留言板 Version 1.0






11、adminpost.php(管理验证页)







session_start();
require_once('conn.php');




name = _POST['name'];
password = _POST['password'];




sql = "select * from admin where name='".name."'";
result = mysql_query(sql);
num = mysql_num_rows(result);




if (strrpos(name,"<")!==false || strrpos(name,">")!==false||strrpos(name,"@")!==false||strrpos(name,"\"")!==false||strrpos(name,"'")!==false||strrpos(name,"_")!==false)
{
echo "";
}




if(num)
{//如果用户存在,就检查密码是否正确
rs = mysql_fetch_array(result);
if(rs[2]!=password)
{
echo "";
}
else
{//用户名,密码都正确,注册SESSION变量,然后跳转到首页
_SESSION["key"]=1;
echo "";
}
}
else
{//如果没有这个用户
echo "";
}
?>




12、adminexit.php(退出管理页)







session_start();
_SESSION["key"] = 0;//使SESSION不为1,0为游客,1为管理员




header('location:index.php');
?>




只是些基本的东东,像MD5加密,UBB,表情图片,添加管理员账号,回复留言(-_!!)等我都没加上去,有兴趣的同学可以完善下,不足的地方请指出^_^




管理员账号:wuleying 密码:123456




源文件下载:点击这里下载源文件




来源:blueidea

























If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759