PHP+MySQL 制作简单的留言本(2)


<?php
$boardname="RUIZHINET留言本";
$host="localhost";
$user="root";
$password="";
$database="ruizhi_messageboard";
$pagenum="10";
$admin_name="admin";
$admin_password="admin";
$conn=mysql_connect($host,$user,$password);
mysql_select_db($database,$conn);
?>


index.php:程序显示页面

复制代码 代码如下:


<?php
if(!$_GET["page"]){
$page=1;
}else{
$page=$_GET["page"];
}
$sql="SELECT id FROM messageboard";
$result=mysql_query($sql,$conn);
$row_num=mysql_num_rows($result);
$page_count=ceil($row_num/$pagenum);
echo "当前共有".$row_num."条留言";
echo "&nbsp;&nbsp;共分".$page_count."页显示";
echo "&nbsp;&nbsp;当前为第".$page."页";
?>
<p>
<?php
if($row_num==0){
echo "暂时没有留言";
}else{
?>
<table>
<?php
$offset=($page-1)*$pagenum;
$sql2="SELECT * FROM messageboard ORDER BY id DESC LIMIT $offset,$pagenum";
$result2=mysql_query($sql2,$conn);
while($row=mysql_fetch_array($result2)){
$offset++;
echo "<tr>";
echo "<td align=left bgcolor=#CCCCFF>";
echo $offset.".<img src='smiles/".base64_decode($row[smiles])."'>";
echo base64_decode($row[title])." - ".base64_decode($row[author])." [".date("Y年m月d日",$row[addtime])."] ";
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
echo "<a href=https://www.jb51.net/del.php?id=".$row["id"].">删除 | </a>";
echo "<a href=https://www.jb51.net/reply.php?id=".$row["id"].">回复</a>";
}
echo "</td></tr>";
echo "<tr><td align=left>".base64_decode($row["content"])."</td></tr>";
if($row["photo"]!="NONE")
{
echo "<tr><td align=left><img src='upfile/".base64_decode($row["photo"])."'></td></tr>";
}
$sql3="SELECT * FROM reply WHERE msgid=$row[id] ORDER BY id DESC";
$result3=mysql_query($sql3,$conn);
while($row3=mysql_fetch_array($result3)){
echo "<tr><td align=left bgcolor=#99CCFF>回复:".date("Y年m月d日",$row3[addtime]);
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
echo "<a href=https://www.jb51.net/delreply.php?id=".$row3["id"].">删除</a>";
}
echo "</td></tr>";
echo "<tr><td align=left>".base64_decode($row3[content])."</td></tr>";
}
}
?>
<?php
if($page_count>1){
$prev_page=$page-1;
$next_page=$page+1;
if($page<=1){
echo "第一页 | ";
}else{
echo "<a href='https://www.jb51.net/index.php?page=1'>第一页</a> | ";
}
if($prev_page<1){
echo "上一页 | ";
}else{
echo "<a href='index.php?page=".$prev_page."'>上一页</a> | ";
}
if($next_page>$page_count){
echo "下一页 | ";
}else{
echo "<a href='index.php?page=".$next_page."'>下一页</a> | ";
}
if($page>=$page_count){
echo "最后一页";
}else{
echo "<a href='index.php?page=".$page_count."'>最后一页</a>";
}
}
?>
</table>
<?php
}
?>
<?php
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
?>
<p><a href="https://www.jb51.net/logout.php">退出管理</a></p>
<?php
}else{
?>
<p><a href="https://www.jb51.net/login.php">登陆管理</a></p>
<?php
}
?>


saveadd.php:保存留言

复制代码 代码如下:


<?php
if(!$_POST["author"] || !$_POST["content"])
{
echo "<meta http-equiv=\"refresh\" content=\"2;url=index.php\">\n";
echo "你没有填写留言姓名或内容,2秒钟返回首页";
exit();
}else{
$imgflag=0;
function random($length)
{
$hash = 'IMG-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
echo (double)microtime() * 1000000;
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
if($_FILES["upfile"]["name"]!=""){
$uploaddir="https://www.jb51.net/upfile/";
$type=array("jpg","gif","bmp","jpeg","png");
if(!in_array(strtolower(fileext($_FILES['upfile']['name'])),$type))
{
echo "<meta http-equiv=\"refresh\" content=\"2;url=index.php\">\n";
$text=implode(",",$type);
echo "您只能上传以下类型文件: ",$text,"<br>";
exit();
}
else
{
$filename=explode(".",$_FILES['upfile']['name']);
do
{
$filename[0]=random(10);
$randname=implode(".",$filename);
$uploadfile=$uploaddir.$randname;
} while(file_exists($uploadfile));
if (move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile)){
echo "上传图片成功";
$imgflag=1;
}
else{
echo "上传图片失败!";
$imgflag=0;
}
}
}
$author=base64_encode($_POST["author"]);
$content=base64_encode(ereg_replace("\r\n","<br>",htmlspecialchars($_POST["content"])));
$smiles=base64_encode($_POST["smiles"]);
if($_POST["title"]){
$title=base64_encode($_POST["title"]);
}else{
$title=base64_encode("无标题");
}
$addtime=time();
if($imgflag==1){
$photo=base64_encode($randname);
}else{
$photo="NONE";
}
//保存数据
$sql="INSERT INTO messageboard(author,title,smiles,content,photo,addtime) VALUES('$author','$title','$smiles','$content','$photo',$addtime)";
mysql_query($sql,$conn);
echo "<meta http-equiv=\"refresh\" content=\"2;url=index.php\">\n";
echo "谢谢您的留言,2秒钟返回首页";
}
?>
savereply.php:保存回复
<?php
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!="")
{
if(!$_POST["content"])
{
echo "<meta http-equiv=\"refresh\" content=\"2;url=index.php\">\n";
echo "你没有填写回复内容,2秒钟返回首页";
exit();
}else{
$msgid=$_POST["msgid"];
$content=base64_encode(ereg_replace("\r\n","<br>",htmlspecialchars($_POST["content"])));
$addtime=time();
//保存数据
$sql="INSERT INTO reply(msgid,content,addtime) VALUES($msgid,'$content',$addtime)";
mysql_query($sql,$conn);
echo "<meta http-equiv=\"refresh\" content=\"2;url=index.php\">\n";
echo "谢谢您的留言,2秒钟返回首页";
}
}
?>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/d323bde64ed9ba88034f4ae05356a5d8.html