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}    <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>   "; //} $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>   "; //} $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;