PHP实现简单留言板(Smarty版)

这篇博客介绍了如何使用Smarty模板引擎来实现一个简单的PHP留言板应用。涵盖了连接数据库、创建数据表、用户注册登录以及留言展示和删除的功能。涉及到的文件包括配置、规则处理和模板页面。数据库中包含了`mianshi_liuyanfan`(留言表)和`mianshi_user`(用户表)两个表。
摘要由CSDN通过智能技术生成
D:\WWW\StudySmarty\smarty3130\demo2

D:\WWW\StudySmarty\smarty3130\demo2\config

D:\WWW\StudySmarty\smarty3130\demo2\config\connect.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/16
 * Time: 9:36
 */
$conn=mysqli_connect('127.0.0.1','root','');
//$create_db_sql='create database mianshi';//创建数据库
//$query_sql=mysqli_query($conn,$create_db_sql);//创建数据库
//if($query_sql){
//    echo '创建成功';
//}
$sele_db=mysqli_select_db($conn,'mianshi'); //选择数据库

mysqli_query($conn,'set names utf-8');
 
//创建表
//$create_table_sql='create table mianshi_user ( 
//             id int   NOT NULL    PRIMARY KEY  AUTO_INCREMENT,
//              user_name VARCHAR(20) NOT NULL , 
//              pwd VARCHAR (50) NOT NULL ,
//              head VARCHAR(255) NOT NULL 
//            
//          
//)';
//$query=mysqli_query($conn,$create_table_sql) ;
 
//if($query){
//    echo '创建表成功';
//}

D:\WWW\StudySmarty\smarty3130\demo2\rule

D:\WWW\StudySmarty\smarty3130\demo2\rule\rule_dele_ly.php

<?php
include '../config/connect.php';
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/16
 * Time: 15:13
 */
$sql='delete  from mianshi_liuyanfan where id= '.$_GET['id'];

$query=mysqli_query($conn,$sql);
if($query){
    echo  "<script>alert('删除成功');location.href='../lygl.php'</script>";
}else{
    echo  "<script>alert('删除失败');location.href='../lygl.php'</script>";
}

D:\WWW\StudySmarty\smarty3130\demo2\rule\rule_liuyan.php

<?php
session_start();
include ('../config/connect.php');

if(!$_SESSION['username']){
    echo  "<script>alert('请先登录');location.href='../templates/login.html'</script>";
}else{
    $info=$_POST['info'];
    $sql="select id from mianshi_user where user_name='".$_SESSION['username']."'";
    $query=mysqli_query($conn,$sql);
    $result=mysqli_fetch_array($query);
    $uip=$_SERVER['REMOTE_ADDR'];
    $sql2="insert into mianshi_liuyanfan (uid,info,ip)VALUES(".$result['id'].",'".$info."','".$uip."') ";
//    echo $sql2;
    $query2=mysqli_query($conn,$sql2);
    if($query2){
        
        echo  "<script>alert('留言成功');location.href='../'</script>";
    }
}

D:\WWW\StudySmarty\smarty3130\demo2\rule\rule_login.php

<?php
session_start();
include ('../config/connect.php');
$username=$_POST['username'];
$pwd=$_POST['pwd'];

if(!empty($username)&&!empty($pwd)&&isset($username)&&isset($pwd)){
        $sql="SELECT * from mianshi_user WHERE user_name='".$username."' AND pwd='".$pwd."'";
//    echo $sql;
    $query=mysqli_query($conn,$sql);
     $num=mysqli_num_rows($query);
    if($num==0){
        echo  "<script>alert('用户名不存在');location.href='../templates/login.html'</script>";
    }else{
        $_SESSION['username']=$username;
        echo  "<script>alert('登录成功');location.href='../'</script>";
    }
}else{
    echo  "<script>alert('请填写全部信息');location.href='../templates/login.html'</script>";
}

D:\WWW\StudySmarty\smarty3130\demo2\rule\rule_zhuce.php

<?php
session_start();
include('../config/connect.php');

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/16
 * Time: 10:35
 */
$username=$_POST['username'];
$pwd=$_POST['pwd'];
$pwd2=$_POST['pwd2'];
$head=$_FILES['head'];
if(!empty($username)&&$pwd==$pwd2){

    $array['username']=$username;
    $array['pwd']=$pwd;
//is_uploaded_file() 函数判断指定的文件是否是通过 HTTP POST 上传的。
if(is_uploaded_file($head['tmp_name'])){
echo '图片上传名称:'.$head['name'].'<br>';
echo '图片类型:'.$head['type'].'<br>';
echo '图片大小:'.$head['size'].'<br>';
echo '上传图片的路径:'.$head['tmp_name'].'<br>';
echo '图片错误码:'.$head['error'].'<br>';
    /**
     * 0:文件上传成功<br/>
     * 1:超过了文件大小,在php.ini文件中设置<br/>
     * 2:超过了文件的大小MAX_FILE_SIZE选项指定的值<br/>
     * 3:文件只有部分被上传<br/>
     * 4:没有文件被上传<br/>
     * 5:上传文件大小为0
     */
    switch($head['type']){
        case 'image/pjpeg':
            $isimage=true;
            break;
        case 'image/jpeg':
            $isimage=true;
            break;
        case 'image/gif':
            $isimage=true;
            break;
        case 'image/png':
            $isimage=true;
            break;
    }
    if(!$isimage){
        echo '非法上传文件';
        exit();
    }
    echo "开始移动上传文件<br/>";

$uploadpath='uploads/'.$head['name'];
//    无法上传中文名字的文件
    move_uploaded_file($head['tmp_name'],$uploadpath);
    if($head['error']==0){
        //文件上传成功
        echo '<img src="'.$uploadpath.'"style="width:250px;height:250px">'  ;
        $array['head']=$uploadpath;
    }

}

    $sql="insert into mianshi_user (user_name,pwd,head) VALUES('". $array['username']."','".$array['pwd']."','".$array['head']."')";
//    echo $sql;
    $query=mysqli_query($conn,$sql);
    if($query){
        echo  "<script>alert('注册成功');location.href='../templates/login.html'</script>";
    }
}else{
    echo  "alert('请检查填写是否出错')";

}

D:\WWW\StudySmarty\smarty3130\demo2\templates

D:\WWW\StudySmarty\smarty3130\demo2\templates\index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<a href="templates/login.html">登录</a>
<a href="templates/zhuce.html">注册</a></br>
<form action="rule/rule_liuyan.php" method="post">
    <textarea name="info" style="width: 200px;height: 100px;"></textarea>
    <input type="submit" value="留言">
</form>
{section name=outer loop=$result3}
<img src="{$result3[outer].head}" style='width:50px;height:50px'>  {$result3[outer].username} 说:{$result3[outer].info} <br/>
{/section}


{section name=one loop=$tobal start=0 step=1}
<a href="{$SCRIPT_NAME}?page={$smarty.section.one.index+1}">{$smarty.section.one.index+1}</a>
{/section}


<br/><a href="lygl.php">留言管理</a>
</body>
</html>

D:\WWW\StudySmarty\smarty3130\demo2\templates\login.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<form action="../rule/rule_login.php" method="post">
用户:<input type="text" name="username"></br>

密码:<input type="text" name="pwd"></br>
<input type="submit" value="登录"></br>
</form>
</body>
</html>

D:\WWW\StudySmarty\smarty3130\demo2\templates\lygl.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<form action="rule/rule_liuyan.php" method="post">
    <textarea name="info" style="width: 200px;height: 100px;"></textarea>
    <input type="submit" value="留言">
</form>
{section name=outer loop=$result3}
<img src="{$result3[outer].head}" style='width:50px;height:50px'>  {$result3[outer].username} 说:{$result3[outer].info} &nbsp&nbsp&nbsp<a href='rule/rule_dele_ly.php?id={$result3[outer].id}'>修改</a>|<a href='rule/rule_dele_ly.php?id={$result3[outer].id}'>删除</a><br>
{/section}




{section name=one loop=$tobal start=0 step=1}
<a href="{$SCRIPT_NAME}?page={$smarty.section.one.index+1}">{$smarty.section.one.index+1}</a>
{/section}


<br/><a href="index.php">留言管理</a>
</body>
</html>

D:\WWW\StudySmarty\smarty3130\demo2\templates\zhuce.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
</head>
<body>
<form action="../rule/rule_zhuce.php" method="post" enctype="multipart/form-data">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="pwd"><br>
    重复密码:<input type="password" name="pwd2"><br>
    头像:<input type="file" name="head"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

D:\WWW\StudySmarty\smarty3130\demo2\templates_c
D:\WWW\StudySmarty\smarty3130\demo2\uploads

D:\WWW\StudySmarty\smarty3130\demo2\index.php

<?php
session_start();

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/17
 * Time: 16:42
 */
require '../libs/Smarty.class.php';
include 'config/connect.php';
$page=@$_GET['page'];
$num=2;
$sql4='select * from mianshi_liuyanfan ';
$query4=mysqli_query($conn,$sql4);
$numpage=mysqli_num_rows($query4);
$tobal=ceil($numpage/$num);
if(empty($page)||!isset($page)){
    $page=1;
}
$sql='select * from mianshi_liuyanfan limit '.($page-1).','.$num;
$query=mysqli_query($conn,$sql);
$array3=array();
$i=0;
while ($result=mysqli_fetch_array($query)) {


    $sql2="select * from mianshi_user where id=".$result['uid'];
    $query2=mysqli_query($conn,$sql2);
    $result2=mysqli_fetch_array($query2);
    $result3[$i]['username']=$result2['user_name'];
    $result3[$i]['head']=$result2['head'];
    $result3[$i]['info']=$result['info'];
//    echo "<img src='".$result2['head']."' style='width:50px;height:50px'>".$result2['user_name'].'说:'.$result['info'].'<br>';
    $i++;
}

//for ($i=1;$i<=$tobal;$i++){
//    echo "<a href='../html/index.php?page=".$i."'>".$i."</a>&nbsp&nbsp&nbsp";
//}
$smarty=new Smarty;

$smarty->assign('name','xiajie');
 

$smarty->assign('result3',$result3);
$smarty->assign('tobal',$tobal);
// print_r($result3);
$smarty->display('index.html');

D:\WWW\StudySmarty\smarty3130\demo2\lygl.php

<?php
session_start();

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/17
 * Time: 16:42
 */
require '../libs/Smarty.class.php';
include 'config/connect.php';
$page=@$_GET['page'];
$num=2;
$sql4='select * from mianshi_liuyanfan ';
$query4=mysqli_query($conn,$sql4);
$numpage=mysqli_num_rows($query4);
$tobal=ceil($numpage/$num);
if(empty($page)||!isset($page)){
    $page=1;
}
$sql='select * from mianshi_liuyanfan limit '.($page-1).','.$num;
$query=mysqli_query($conn,$sql);
$array3=array();
$i=0;
while ($result=mysqli_fetch_array($query)) {


    $sql2="select * from mianshi_user where id=".$result['uid'];
    $query2=mysqli_query($conn,$sql2);
    $result2=mysqli_fetch_array($query2);
    $result3[$i]['username']=$result2['user_name'];
    $result3[$i]['head']=$result2['head'];
    $result3[$i]['info']=$result['info'];
    $result3[$i]['id']=$result['id'];
//    echo "<img src='".$result2['head']."' style='width:50px;height:50px'>".$result2['user_name'].'说:'.$result['info'].'<br>';
    $i++;
}

//for ($i=1;$i<=$tobal;$i++){
//    echo "<a href='../html/index.php?page=".$i."'>".$i."</a>&nbsp&nbsp&nbsp";
//}
$smarty=new Smarty;

$smarty->assign('name','xiajie');


$smarty->assign('result3',$result3);
$smarty->assign('tobal',$tobal);
// print_r($result3);
$smarty->display('lygl.html');

 数据库文件代码

/*
Navicat MySQL Data Transfer


Source Server         : 3306
Source Server Version : 50540
Source Host           : 127.0.0.1:3306
Source Database       : mianshi


Target Server Type    : MYSQL
Target Server Version : 50540
File Encoding         : 65001


Date: 2017-09-17 19:57:50
*/


SET FOREIGN_KEY_CHECKS=0;


-- ----------------------------
-- Table structure for mianshi_liuyanfan
-- ----------------------------
DROP TABLE IF EXISTS `mianshi_liuyanfan`;
CREATE TABLE `mianshi_liuyanfan` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) DEFAULT NULL,
  `info` text,
  `ip` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;


-- ----------------------------
-- Table structure for mianshi_user
-- ----------------------------
DROP TABLE IF EXISTS `mianshi_user`;
CREATE TABLE `mianshi_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(20) NOT NULL,
  `pwd` varchar(50) NOT NULL,
  `head` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

<html> <head> <title>留言板设计</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <style type="text/css"> <!-- body,td,th { font-size: 14px; color: #003399; } .xs{ font-size:12px} --> </style></head> <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" background="images/pub_01.gif" style="background-repeat:repeat-x"> <!-- ImageReady Slices (留言板设计(参考)副本.jpg) --> <form name="form1" method="post" action="add.asp"> <table width="600" height="500" border="0" align="center" cellpadding="0" cellspacing="0" id="__01"> <tr> <td width="36" height="500" rowspan="6"> </td> <td height="35" colspan="4"> </td> <td width="50" height="500" rowspan="6"> </td> </tr> <tr> <td colspan="4"><img src="images/pub_04.gif" width="514" height="69" alt=""></td> </tr> <tr> <td width="19" rowspan="4"><img src="images/pub_05.gif" width="19" height="396" alt=""></td> <td colspan="3"><img src="images/pub_06.gif" width="495" height="22" alt=""></td> </tr> <tr> <td><img src="images/pub_07.gif" width="49" height="321" alt=""></td> <td width="317" height="321" background="images/2_02.gif"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="17" colspan="4"> </td> </tr> <tr> <td width="61" height="30">昵称:</td> <td width="235" height="30" colspan="2"><input type="text" name="nc" style="width:120px; height:25px; border:solid #000000 1px;"></td> <td width="21" height="30"> </td> </tr> <tr> <td width="61" height="30">标题:</td> <td height="30" colspan="2"><input type="text" name="bt" style="width:180px; height:25px; border:solid #000000 1px;"></td> <td height="30"> </td> </tr> <tr> <td height="135" valign="middle">内容:</td> <td height="135" colspan="2" valign="middle"><textarea name="nr" cols="30" rows="10" style="border:solid #000000 1px;"></textarea></td> <td height="135"> </td> </tr> <tr> <td height="30"> </td> <td height="30" align="center"><input type="image" name="imageField" src="images/2_05.gif">  <a href="#"><img src="images/2_07.gif" width="62" height="33" border="0"></a></td> <td align="center" class="xs"><a href="view.asp">显示留言</a></td> <td height="30"><a href="view.asp"></a></td> </tr> </table></td> <td><img src="images/pub_09.gif" width="129" height="321" alt=""></td> </tr> <tr> <td colspan="3"><img src="images/pub_10.gif" width="495" height="22" alt=""></td> </tr> <tr> <td height="31" colspan="3"> </td> </tr> </table> </form> <!-- End ImageReady Slices --> </body> </html> <script language="vbscript"> function biaodan() dim usernc,userbt,usernr usernc=form1.nc.value userbt= form1.bt.value usernr=form1.nr.value if len(trim(usernc))<6 then alert("你输入的昵称有误!") biaodan=false exit function else if userbt="" then alert("你输入的标题有误!") biaodan=false exit function else if usernr="" then alert ("你输入的内容有误!") biaodan=false exit function end if end if end if end function sub quxiao() form1.nc.value="" form1.bt.value="" form1.nr.value="" end sub </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值