Heim > Backend-Entwicklung > PHP-Tutorial > PHP+MySQL 制作简单的留言本_PHP教程

PHP+MySQL 制作简单的留言本_PHP教程

WBOY
Freigeben: 2016-07-21 15:42:50
Original
780 Leute haben es durchsucht

 

留言显示页面:比上一个例子增加了分页和留言回复的功能

主要代码:
install/index.php:程序安装页面

复制代码 代码如下:

if($_GET["action"]!=1)
{
?>






































MySQL主机名:
MySQL用户名:
MySQL密码:
数据库名称:
留言本每页记录数:
管理员账号:
管理员密码:
留言本标题:


}else{
@set_time_limit(1000);
$lockfile = "install.lock";
$host="localhost";
$user="root";
$password="";
$database="ruizhi_messageboard";
$pagenum=10;
$admin_name="admin";
$admin_password="123456";
$boardname="RUIZHINET留言本";
if($_POST["host"]!=""){
$host=$_POST["host"];
}
if($_POST["user"]!=""){
$user=$_POST["user"];
}
if($_POST["password"]!=""){
$password=$_POST["password"];
}
if($_POST["database"]!=""){
$database=$_POST["database"];
}
if($_POST["pagenum"]!=""){
$pagenum=$_POST["pagenum"];
}
if($_POST["admin_name"]!=""){
$admin_name=$_POST["admin_name"];
}
if($_POST["admin_password"]!=""){
$admin_password=$_POST["admin_password"];
}
if($_POST["boardname"]!=""){
$boardname=$_POST["boardname"];
}
if(file_exists($lockfile)){
exit("已经安装过了,如果要重新安装请先删除install/install.lock");
}
$conn=mysql_connect($host,$user,$password);
if($conn){
$sql_drop_database="DROP DATABASE IF EXISTS `".$database."`";
$sql_create_database="CREATE DATABASE `".$database."`";
$sql_create_table_messageboard="CREATE TABLE `messageboard` (
`id` int(11) NOT NULL auto_increment,
`author` varchar(15) NOT NULL,
`title` varchar(30) NOT NULL,
`smiles` varchar(30) NOT NULL,
`content` mediumtext,
`photo` varchar(30) NOT NULL,
`addtime` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1";
$sql_create_table_reply="CREATE TABLE `reply` (
`id` int(11) NOT NULL auto_increment,
`msgid` int(11) NOT NULL,
`content` mediumtext,
`addtime` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1";
if(mysql_query($sql_drop_database,$conn)){
if(mysql_query($sql_create_database,$conn)){
mysql_select_db($database,$conn);
if(mysql_query($sql_create_table_messageboard,$conn) && mysql_query($sql_create_table_reply,$conn)){
$config_file="../config.php";
$config_strings="$config_strings.="\$boardname=\"".$boardname."\";\n";
$config_strings.="\$host=\"".$host."\";\n";
$config_strings.="\$user=\"".$user."\";\n";
$config_strings.="\$password=\"".$password."\";\n";
$config_strings.="\$database=\"".$database."\";\n";
$config_strings.="\$pagenum=\"".$pagenum."\";\n";
$config_strings.="\$admin_name=\"".$admin_name."\";\n";
$config_strings.="\$admin_password=\"".$admin_password."\";\n";
$config_strings.="\$conn=mysql_connect(\$host,\$user,\$password);\n";
$config_strings.="mysql_select_db(\$database,\$conn);\n";
$config_strings.="?>";
if($fp=fopen($config_file,"wb")){
if(fwrite($fp,$config_strings)){
if($fp2 = fopen($lockfile, 'w'))
{
fwrite($fp2,'1212');
fclose($fp2);
}
echo "安装成功!配置文件为:config.php,您可以手工修改该文件";
echo "\n进入系统首页";
}else{
exit("文件写入失败");
}
fclose($fp);
}
}else{
exit("不能执行CREATE TABLE语句:".$sql_create_table);
}
}else{
exit("不能执行CREATE DATABASE语句:".$sql_create_database);
}
}else{
exit("不能执行DROP DATABASE语句:".$sql_drop_database);
}
}else{
exit("连接数据库失败,请检查MySQL主机名、用户名和密码");
}
}

生成的config.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:程序显示页面
复制代码 代码如下:

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 "  共分".$page_count."页显示";
echo "  当前为第".$page."页";
?>


if($row_num==0){
echo "暂时没有留言";
}else{
?>


$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 "";
echo "";
echo "";
if($row["photo"]!="NONE")
{
echo "";
}
$sql3="SELECT * FROM reply WHERE msgid=$row[id] ORDER BY id DESC";
$result3=mysql_query($sql3,$conn);
while($row3=mysql_fetch_array($result3)){
echo "";
echo "";
}
}
?>
if($page_count>1){
$prev_page=$page-1;
$next_page=$page+1;
if($pageecho "第一页 | ";
}else{
echo "第一页 | ";
}
if($prev_pageecho "上一页 | ";
}else{
echo "上一页 | ";
}
if($next_page>$page_count){
echo "下一页 | ";
}else{
echo "下一页 | ";
}
if($page>=$page_count){
echo "最后一页";
}else{
echo "最后一页";
}
}
?>
";
echo $offset.".PHP+MySQL 制作简单的留言本_PHP教程";
echo base64_decode($row[title])." - ".base64_decode($row[author])." [".date("Y年m月d日",$row[addtime])."] ";
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
echo "删除 | ";
echo "回复";
}
echo "
".base64_decode($row["content"])."
PHP+MySQL 制作简单的留言本_PHP教程
回复:".date("Y年m月d日",$row3[addtime]);
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
echo "删除";
}
echo "
".base64_decode($row3[content])."

}
?>
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
?>

退出管理


}else{
?>

登陆管理


}
?>

saveadd.php:保存留言
复制代码 代码如下:

if(!$_POST["author"] || !$_POST["content"])
{
echo "\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 {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
if($_FILES["upfile"]["name"]!=""){
$uploaddir="upfile/";
$type=array("jpg","gif","bmp","jpeg","png");
if(!in_array(strtolower(fileext($_FILES['upfile']['name'])),$type))
{
echo "\n";
$text=implode(",",$type);
echo "您只能上传以下类型文件: ",$text,"
";
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","
",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 "\n";
echo "谢谢您的留言,2秒钟返回首页";
}
?>
savereply.php:保存回复
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!="")
{
if(!$_POST["content"])
{
echo "\n";
echo "你没有填写回复内容,2秒钟返回首页";
exit();
}else{
$msgid=$_POST["msgid"];
$content=base64_encode(ereg_replace("\r\n","
",htmlspecialchars($_POST["content"])));
$addtime=time();
//保存数据
$sql="INSERT INTO reply(msgid,content,addtime) VALUES($msgid,'$content',$addtime)";
mysql_query($sql,$conn);
echo "\n";
echo "谢谢您的留言,2秒钟返回首页";
}
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/320898.htmlTechArticle留言显示页面:比上一个例子增加了分页和留言回复的功能 主要代码: install/index.php:程序安装页面 复制代码 代码如下: ?php if($_GET["acti...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage