标签:
说明:代码来源《PHP和MySQL Web应用开发》一书,还有就是代码有些是经过修改的,经过我的测验全部都可以用。
通过自己的修改实现中文数据的输入,主要是编码的问题。
本来还想加添加表情的功能,不过今天时间有限,就先做到这,后续有时间再添加那个表情功能。
本示例的数据库脚本 ~~~ MySQL执行语句: source d:\test\book.sql;
CREATE DATABASE IF NOT EXISTS book COLLATE ‘gb2312_chinese_ci‘; USE book; CREATE TABLE IF NOT EXISTS Users ( UserName VARCHAR(50) PRIMARY KEY, UserPwd VARCHAR(50), ShowName VARCHAR(50)); CREATE TABLE IF NOT EXISTS Content ( ContId INT AUTO_INCREMENT PRIMARY KEY, Subject VARCHAR(200) NOT NULL, Words VARCHAR(1000) NOT NULL, UserName VARCHAR(50) NOT NULL, Face VARCHAR(50), Email VARCHAR(50), Homepage VARCHAR(50), CreateTime DATETIME, UpperId INT ); INSERT INTO Users VALUES(‘admin‘, ‘pass‘, ‘admin‘);
Class文件包含2个文件 Content.php(Content类) 和 Users.php (Users类) 。
Users.php
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″> <?PHP class Users { var $conn; public $UserName; // 鐢ㄦ埛鍚? public $UserPwd; // 鐢ㄦ埛瀵嗙爜 public $ShowName; // 鏄剧ず鍚嶇О function __construct() { // 杩炴帴鏁版嵁搴? $this->conn = mysqli_connect("localhost", "root", "123456", "book"); mysqli_query($this->conn, "SET NAMES utf-8"); } function __destruct() { // 鍏抽棴杩炴帴 mysqli_close($this->conn); } // 鍒ゆ柇鎸囧畾鐢ㄦ埛鏄惁瀛樺湪 function exists($user) { $result = $this->conn->query("SELECT * FROM Users WHERE UserName=‘" . $user . "‘"); if($row = $result->fetch_row()) { $this->UserName = $user; $this->UserPwd = $row[1]; $this->ShowName = $row[2]; return true; } else return false; } // 鍒ゆ柇鎸囧畾鐨勭敤鎴峰悕鍜屽瘑鐮佹槸鍚﹀瓨鍦? function verify($user, $pwd) { $sql = "SELECT * FROM Users WHERE UserName=‘" . $user . "‘ AND UserPwd=‘" . $pwd . "‘"; $result = $this->conn->query($sql); if($row = $result->fetch_row()) { $this->UserName = $user; $this->UserPwd = $pwd; $this->ShowName = $row[2]; return true; } else return false; } // 鎻掑叆鏂拌褰? function insert() { $sql = "INSERT INTO Users VALUES(‘" . $this->UserName . "‘, ‘" . $this->UserPwd . "‘, ‘" . $this->ShowName . "‘)"; $this->conn->query($sql); } // 鏇存柊鏄剧ず鍚嶇О function updateShowName() { $sql = "UPDATE Users SET ShowName=‘" . $this->ShowName . "‘ WHERE UserName=‘" . $this->UserName . "‘"; $this->conn->query($sql); } // 鏇存柊瀵嗙爜 function updatePassword() { $sql = "UPDATE Users SET UserPwd=‘" . $this->UserPwd . "‘ WHERE UserName=‘" . $this->UserName . "‘"; $this->conn->query($sql); } // 鍒犻櫎鐢ㄦ埛 function delete() { $sql = "DELETE FROM Users WHERE UserName=‘" . $this->UserName . "‘"; $this->conn->query($sql); } function load_users() { $sql = "SELECT * FROM Users"; $result = $this->conn->query($sql); Return $result; } } ?>
Content.php
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″> <?php class Content { var $conn; public $ContId; public $Subject;//public $words; public $words; public $UserName; public $Face; public $Email; public $Homepage; public $CreateTime; public $UpperId; function __construct() { $this->conn = mysqli_connect("localhost", "root", "123456", "book"); mysqli_query($this->conn, "SET NAMES utf-8"); } function __destruct() { mysqli_close($this->conn); } function GetInfo($Id) { $sql = "SELECT * FROM Content WHERE ContId=" . $Id; $result = $this->conn->query($sql); if($row = $result->fetch_row()) { $this->ContId = $Id; $this->Subject = $row[1]; $this->words = $row[2]; $this->UserName = $row[3]; $this->Face = $row[4]; $this->Email = $row[5]; $this->Homepage = $row[6]; $this->CreateTime = $row[7]; $this->UpperId = (int)$row[8]; } } function GetRecordCount() { $sql = "SELECT COUNT(*) FROM Content"; $result = $this->conn->query($sql); if($row = $result->fetch_row()) Return (int)$row[0]; else Return 0; } function insert() { $sql = "INSERT INTO Content (Subject, words, UserName, Face, Email, Homepage, CreateTime, UpperId) VALUES(‘" . $this->Subject . "‘, ‘" . $this->Words . "‘, ‘" . $this->UserName . "‘, ‘" . $this->Face . "‘, ‘" . $this->Email . "‘, ‘" . $this->Homepage . "‘, ‘" . $this->CreateTime . "‘, " . $this->UpperId . ")"; $this->conn->query($sql); } function delete($Id) { $sql = "DELETE FROM Content WHERE ContId=" . $Id . " OR UpperId=" . $Id; $this->conn->query($sql); } function load_content_byUpperid($uid) { $sql = "SELECT * FROM Content WHERE UpperId=" . $uid . " ORDER BY CreateTime DESC"; $result = $this->conn->query($sql); Return $result; } function load_content_byPage($pageNo, $pageSize) { $sql = "SELECT * FROM Content WHERE UpperId=0 ORDER BY CreateTime DESC LIMIT " . ($pageNo-1) * $pageSize . "," . $pageSize; $result = $this->conn->query($sql); Return $result; } } ?>
index.php 留言板主页
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″> <?PHP error_reporting(0); $UserName = $_POST[‘UserName‘]; if($UserName != "") include(‘ChkPwd.php‘); include(‘Show.php‘); include("Class\Content.php"); $objContent = new Content(); // 定义Content对象,用于访问表Content $pageSize = 5; // 设置每页显示留言记录的数量 @$pageNo = (int)$_GET[‘Page‘]; // 获取当前显示的页码 $recordCount = $objContent->GetRecordCount(); // 处理不合法的页码 if($pageNo < 1) $pageNo = 1; // 计算总页数 if( $recordCount ){ //如果记录总数量小于每页显示的记录数量,则只有一页 if( $recordCount < $pageSize ){ $pageCount = 1; } //取记录总数量不能整除每页显示记录的数量,则页数等于总记录数量除以每页显示记录数量的结果取整再加1 if( $recordCount % $pageSize ){ $pageCount = (int)($recordCount / $pageSize) + 1; } else { //如果没有余数,则页数等于总记录数量除以每页显示记录的数量 $pageCount = $recordCount / $pageSize; } } else{ // 如果结果集中没有记录,则页数为0 $pageCount = 0; } if($pageNo > $pageCount) $pageNo = $pageCount; ?> <html> <head> <title>网络留言板</title> <style> <!-- .main { font-size: 10pt } --> </style> <script language="JavaScript"> function newwin(url) { var newwin=window.open(url,"newwin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=460"); newwin.focus(); return false; } </script> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head> <body topmargin="0" vlink="#000000" link="#000000"> <div align="center"> <center> <table width="714" border="0" height="218" cellspacing="0" cellpadding="0"> <tr> <td height="18" class="main" bordercolorlight="#0000FF" bordercolordark="#00FFFF" bgcolor="#3399FF" background="images/b3.gif"> <?PHP if(!$_SESSION[‘Passed‘]) { ?> <form method="POST" action="<?PHP $_SERVER[‘PHP_SELF‘] ?>" name="myform"> <font size="2">用户名:</font><input type="text" name="UserName" size="12"> 密码: <input type="password" name="UserPwd" size="12"> <input type="submit" value="登录" name="B1"> <?PHP } else { echo("<b>欢迎管理员光临!</b>"); } ?> [ <a target="_blank" href="newRec.php" onclick="return newwin(this.href)"> 我要留言</a> ] <?PHP if($_SESSION[‘Passed‘]) { echo(‘[<a href="logout.php">退出登录</a>]‘); } ?> </td> </form> </tr> </center> <tr> <td height="161" class="main"> <?PHP ShowList($pageNo, $pageSize); ?> </td></tr> <tr> <td height="15"> </td></tr> <tr> <td height="13" class="main" background="images/b3.gif"> <?PHP ShowPage($pageCount, $pageNo); ?></td> </tr> <tr> <td height="15"> </td></tr> </table> </body>
ChkPwd.php 进行身份验证
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″> <?PHP include(‘class\Users.php‘); // 包含Users类 $user = new Users(); session_start(); //如果尚未定义Passed对象,则将其定义为False,表示没有通过身份认证 if(!isset($_SESSION[‘Passed‘])) { $_SESSION[‘Passed‘] = False; } // 如果$_SESSION[‘Passed‘]=False,则表示没有通过身份验证 if($_SESSION[‘Passed‘]==False) { // 读取从表单传递过来的身份数据 $UserName = $_POST[‘UserName‘]; $UserPwd = $_POST[‘UserPwd‘]; if($UserName == "") $Errmsg = "请输入用户名和密码"; else { // 验证用户名和密码 if(!$user->verify($UserName, $UserPwd)) { ?> <script language="javascript"> alert("用户名或密码不正确!"); </script>"; <?PHP } else { // 登录成功 ?> <script language="javascript"> alert("登录成功!"); </script> <?PHP $_SESSION[‘Passed‘] = True; $_SESSION[‘UserName‘] = $UserName; $_SESSION[‘ShowName‘] = $user->ShowName; //$_SESSION[‘ShowName‘] = $row[2]; } } } // 经过登录不成功,则画出登录表单MyForm if(!$_SESSION[‘Passed‘]) { ?> <script language="javascript"> history.go(-1); </script> <?PHP } ?>
logout.php 按退出时执行的,清空$_SESSION的某些数据
<?PHP session_start(); unset($_SESSION[‘Passed‘]); unset($_SESSION[‘UserName‘]); unset($_SESSION[‘ShowName‘]); header("Location: index.php"); ?>
Show.php 显示主题留言,主要存放ShowPage()和ShowList()两个函数
<head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″> <style> <!-- .main { font-size: 10pt } --> </style> <script language="JavaScript"> function newwin(url) { var newwin=window.open(url,"newwin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=460"); newwin.focus(); return false; } </script> </head> <?PHP function ShowList( $pageNo, $pageSize ) { ?> <div align="center"> <center> <table border="1" width="738" bordercolor="#3399FF" cellspacing="0" cellpadding="0" height="46" bordercolorlight="#FFCCFF" bordercolordark="#CCCCFF"> <?PHP $existRecord = False; $objContent = new Content(); $results = $objContent->load_content_byPage($pageNo, $pageSize); // 使用while语句显示所有$results中的留言数据 while($row = $results->fetch_row()) { $existRecord = True; ?> <tr> <td width="148" height="16" class="main" align=center> <br> <img border="0" src="images/<?PHP echo($row[4]); /* 输入Face字段的内容*/ ?>.gif" width="100" height="100"><br> <?PHP echo($row[3]); /* 输出UserName字段的值 */?><br><br> <a href="<?PHP echo($row[6]); /* 输出Homepage字段的值 */?>" target=_blank> <img border="0" src="images/homepage.gif" width="16" height="16"></a> <a href="mailto:<?PHP echo($row[5]); /* 输出Email字段的值 */ ?>"> <img border="0" src="images/email.gif" width="16" height="16"></a><br> <?PHP if($_SESSION["UserName"] <> "") {?> <a href=newRec.php?UpperId=<?PHP echo($row[0]); /*ContId*/ ?> target=_blank onclick="return newwin(this.href)">回复</a> <a href=deleteRec.php?ContId=<?PHP echo($row[0]); /*ContId*/ ?> target=_blank onclick="return newwin(this.href)">删除</a> <?PHP } /* end of if */ ?> </td> <td width="584" height="16" class="main" align="left" valign="top"> <br><b>标题:<?PHP echo($row[1]); /* Subject */ ?> 时间: <?PHP echo($row[7]); /* CreateTime */ ?></b><hr><br> <?PHP echo($row[2]); /* Words */ // 下面用于显示所有回复留言 $content = new Content(); // 定义Content对象 $sub_results = $content->load_content_byUpperid($row[0]); while($subrow = $sub_results->fetch_row()) { echo("<BR><BR><BR>"); ?> <img border="0" src="images/<?PHP echo($subrow[4]); /* Face */ ?>.gif" width="50" height="50"> <?PHP echo($subrow[3]); /* UserName") */ ?> <a href="<?PHP echo($subrow[6]); /*homepage*/ ?>" target=_blank> <img border="0" src="images/homepage.gif" width="16" height="16"></a> <a href="mailto:<?PHP echo($subrow[5]); /* email */ ?>"> <img border="0" src="images/email.gif" width="16" height="16"></a> <b> 标题:<?PHP echo($subrow[1]); /*Subject */?> 时间: <?PHP echo($subrow[7]); /* CreateTime */ ?></b><hr><br> <?PHP echo($subrow[2]); /* Words */ ?> <?PHP } // end of while ?> </td> </tr> <?PHP } // end of while if(!$existRecord) { ?> <tr> <td width="148" height="16" align=center class="main">没有留言数据</td> </tr> <?PHP } // end of if echo("</table></center></div>"); } // end of function ?> <?PHP // $recordCount表示返回结果集中的总页数,$pageNo表示当前页码 function ShowPage( $pageCount, $pageNo ) { echo("<table width=738> <tr> <td align=right class=main>"); // 显示第一页,如果当前页就是第一页,则不生成链接 if($pageNo>1) echo("<A HREF=index.php?Page=1>第一页</A> "); else echo("第一页 "); // 显示上一页,如果不存在上一页,则不生成链接 if($pageNo>1) echo("<A HREF=index.php?Page=" . ($pageNo-1) . ">上一页</A> "); else echo("上一页 "); // 显示下一页,如果不存在下一页,则不生成链接 if($pageNo<>$pageCount) echo("<A HREF=index.php?Page=" . ($pageNo+1) . ">下一页</A> "); else echo("下一页 "); // 显示最后一页,如果当前页就是最后一页,则不生成链接 if($pageNo <> $pageCount) echo("<A HREF=index.php?Page=" . $pageCount . ">最后一页</A> "); else echo("最后一页 "); // 输出页码 echo($pageNo . "/" . $pageCount . "</td></tr></table>"); } ?>
newRec.php 添加新留言的代码
<html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″> <style> <!-- .main { font-size: 9pt } --> </style> <script Language="JavaScript"> function ChkFields() { if (document.formadd.name.value==‘‘) { window.alert ("请输入姓名!"); formadd.name.focus(); return false } if (document.formadd.Subject.value==‘‘) { window.alert ("请输入主题!"); formadd.Subject.focus(); return false } if (document.formadd.Subject.value.length>50) { window.alert ("主题超长!"); formadd.Subject.focus(); return false } if (document.formadd.Words.value.length>1000) { window.alert ("留言内容超长!"); formadd.Words.focus(); return false } return true } </script> <meta http-equiv="Content-Language" content="zh-cn"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <meta name="GENERATOR" content="Microsoft FrontPage 6.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <title>添加新留言</title> </head> <p align="center">添加新留言(*为必填项)</p> <?PHP // 获取上级留言的编号 $UpperId = (int)$_GET[‘UpperId‘]; ?> <form method="POST" action="recSave.php?UpperId=<?PHP echo($UpperId); ?>" name="formadd" onsubmit = "return ChkFields()"> <table align="center" border="1" cellpadding="1" cellspacing="1" width="473" bordercolor="#008000" bordercolordark="#FFFFFF" height="108"> <tr> <td align=left bgcolor="#FFCCFF" width="77" height="24" class="main"> 您的姓名<font color="#FF0000">*</font></td> <td width="380" height="24" class="main"> <input name="name" size="51"></td> </tr> <tr> <td align=left bgcolor="#FFCCFF" width="77" height="23" class="main"> 电子邮件</td> <td width="380" height="23" class="main"> <input name="email" size="51"></td> </tr> <tr> <td align=left bgcolor="#FFCCFF" width="77" height="23" class="main"> 主页地址</td> <td width="380" height="23" class="main"> <input name="homepage" size="51"></td> </tr> <tr> <td align=left bgcolor="#FFCCFF" width="77" height="23" class="main">留言标题<font color="#FF0000">*</font></td> <td width="380" height="23" class="main"><input name="Subject" size="51"></td> </tr> <tr> <td align=left bgcolor="#FFCCFF" width="77" height="43" class="main" valign="top">具体内容<font color="#FF0000">*</font></td> <td width="380" height="43" class="main" valign="top"><textarea rows="4" name="Words" cols="50"></textarea></td> </tr> <tr> <td align=left bgcolor="#FFCCFF" width="165" class="main" valign="top">请选择头像</td> <td width="292" class="main" valign="top"><select size="1" name="logo" onChange="showlogo()"> <option selected value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> </select> <img src="images/1.gif" name="img"> <script language="javascript"> function showlogo(){ document.images.img.src = "images/" + document.formadd.logo.options[document.formadd.logo.selectedIndex].value + ".gif"; } </script> </td> </tr> </table> <p align="center"><input type="submit" value="提交" name="B1"><input type="reset" value="全部重写" name="B2"></p> </form> <p align="center"> </p> </body> </html>
recSave.php 将新留言的相关数据写入MySQL数据库之中
<html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″> <title>保存留言信息</title> </head> <body> <?PHP date_default_timezone_set(‘Asia/Chongqing‘); //系统时间差8小时问题 include(‘Class\Content.php‘); $objContent = new Content(); // 从参数或表单中接收数据到变量中 $objContent->UserName = $_POST["name"]; $objContent->Subject = $_POST["Subject"]; $objContent->Words = $_POST["Words"]; $objContent->Email = $_POST["email"]; $objContent->Homepage = $_POST["homepage"]; $objContent->Face = $_POST["logo"]; $objContent->UpperId = $_GET["UpperId"]; if($objContent->UpperId == "") $objContent->UpperId = 0; // 获取当前当前时间 $now = getdate(); $objContent->CreateTime = $now[‘year‘] . "-" . $now[‘mon‘] . "-" . $now[‘mday‘] . " " . $now[‘hours‘] . ":" . $now[‘minutes‘] . ":" . $now[‘seconds‘]; $objContent->insert(); echo("<h2>信息已成功保存!</h2>"); ?> </body> <Script language="javascript"> //打开此脚本的网页将被刷新 opener.location.reload(); //停留800毫秒后关闭窗口 setTimeout("window.close()",2800); </Script> </html>
deleteRec.php 删除留言代码
<html> <head> <title>删除用户信息</title> </head> <body> <?PHP include(‘ChkPwd.php‘); include(‘Class\Content.php‘); $ContId = $_GET["ContId"]; // 获取要删除的留言记录编号 $objContent = new Content(); $objContent->delete($ContId); // 删除留言记录 echo("已成功删除留言。"); ?> <Script Language="JavaScript"> //打开此脚本的网页将被刷新 opener.location.reload(); //停留800毫秒后关闭窗口 setTimeout("window.close()",800); </Script> </body> </html>
界面截图:
未登录界面
已登录界面
留言界面
标签:
原文地址:http://www.cnblogs.com/yzmb/p/4625146.html