【相關學習推薦:mysql教學】
#跟著書學的,程式碼不是自己寫的,但是都能理解,有時間自己去寫個好看一點的吼吼吼~(不熟練花了一天的時間…
留言板是接觸WEB開發的基礎,寫一個留言板需要知道前端的一些基礎標籤,對資料庫有一個了解會基礎SQL語言,PHP基礎知識,前段基礎資料庫基礎PHP基礎=>留言板。
前方高能哇(介面真的是吃藕誒…
先建一個資料庫,資料庫裡有兩張表,一個存帳號密碼,一個儲存留言資訊
//创建数据库,里面有两张表Admin和Message create database gbook; //创建Admin表,记录用户名和密码 create table admin( username varchar(20) not null, userpass varchar(20) not null ); //创建Message表,记录留言的id,留言人,留言日期,留言内容以及回复 create table message( id int(4) not null auto_increment primary key, author varchar(20) not null, addtime datetime not null, content varchar(1000) not null, reply varchar(1000) not null );
先實作使用者留言的部分,這是第一步,沒有留言index頁面就空了嘛~
<!-- 1.用户填写留言部分 send.php --> <!-- 可以首先编写send页面,只有用户提交了留言才能进行后面的留言显示,留言管理等等 --> <?php $name = $_POST["name"];//从input里面传过来的name //看用户是否提交了新留言,如果提交了,则写入表message if( $name != ""){ $content = $_POST["content"]; //下面的代码用于获得当前日期和时间 $addtime = date("Y-m-d h:i:s");//得到日期 $link = mysqli_connect("127.0.0.1","root","Vmorish");//PHP连接数据库 if( $link) echo "ok!<br>"; else { echo "bad!<br>"; } mysqli_select_db($link,"gbook");//选择数据库 $insert = "insert into message(author,addtime,content,reply) values('$name','$addtime','$content','')"; mysqli_query($link,$insert); mysqli_close($link); echo "<script language=javascript>alert('留言成功!单击确定查看留言.');location.href='index.php';</script>"; } mysqli_close($link); ?> <html> <head> <title>欢迎来到陈雨情的留言本吼吼吼</title> </head> <body> <!-- border-collapse:collapse合并表格的边框 --> <table border=1 cellspacing=0 cellspadding=0 style="border-collapse:collapse" align=center width=400 bordercolor=black> <tr> <td height=100 bgcolor=#6c6c6c> <font style="font-size:30px" color=#ffffff face="黑体">欢迎来到×××的留言本吼吼吼</font> </td> </tr> <tr> <td height=25> <a href=send.php>[我要写留言]</a> <a href=login.php>[管理留言]</a> </td> </tr> <tr> <td height=200> <form method="POST" action="send.php"> <table border="1" width="95%" id="table1" cellspacing="0" cellpadding="0" bordercolor="#808080" style="border-collapse:collapse" height="265"> <tr> <td colspan="2" height="29"> <p align="center">欢迎填写你的留言</p> </td> </tr> <tr> <td width="32%"> <p align="right">你的名字</p> </td> <td width="67%"> <input type="text" name="name" size="20"> </td> </tr> <tr> <td width="32%"> <p>留言内容</p> </td> <td width="67%"> <textarea rows="10" name="content" cols="31"></textarea> </td> </tr> <tr> <td width="99%" colspan="2"> <p align="center"> <input type="submit" value="提交" name="B1"> </p> </td> </tr> </table> </form> </td> </tr> <tr> <td height=80 bgcolor=#6c6c6c align=center> <font color="#FFFFFF"> 版权所有:<a href="http://blog.csdn.net/cherish0222" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Vmorish</a><br> E-mail:vmorish@163.com </font> </td> </tr> </table> </body> </html>
效果:
接著就可以上主頁面了
<!-- 2.留言本首页 index.php --> <!-- 本页面显示十条最近的的留言,并且有分页功能 --> <html> <head> <title>欢迎来到陈雨情的留言本吼吼吼</title> <style type="text/css"> TD{ font-size: 12px; line-height: 150%; } </style> </head> <body> <table border=1 cellspacing=0 cellspadding=0 style="border-collapse:collapse" align=center width=400 bordercolor=black height=382> <tr> <td height=100 bgcolor=#6c6c6c style="font-size:30px;line-height:30px"> <font color=#ffffff face="黑体">欢迎来到×××的留言本吼吼吼</font> </td> </tr> <tr> <td height=25> <a href=send.php>[我要写留言]</a> <a href=login.php>[管理留言]</a> </td> </tr> <tr> <td height=200> <?php $link = mysqli_connect("127.0.0.1","root","Vmorish"); mysqli_select_db($link,"gbook"); $query = "select * from message"; $result = mysqli_query($link,$query); if( mysqli_num_rows($result) < 1){ echo " 目前数据表中还没有任何留言!"; }else{ $totalnum = mysqli_num_rows($result);//获取数据库中所有数据条数 $pagesize = 7;//每页显示7条 $page = $_GET["page"]; if( $page == ""){ $page = 1; } $begin = ($page-1)*$pagesize; $totalpage = ceil($totalnum/$pagesize); //输出分页信息 echo "<table border=0 width=95%><tr><td>"; $datanum = mysqli_num_rows($result); echo "共有".$totalnum."条留言,每页".$pagesize."条,共".$totalpage."页。<br>"; //输出页码 for( $i = 1; $i <= $totalpage; $i++){ echo "<a href=index.php?page=".$i.">[".$i."] </a>"; } echo "<br>"; //从message表中查询当前页面所要显示的留言,并根据时间排序 $query = "select * from message order by addtime desc limit $begin,$pagesize"; $result = mysqli_query($link,$query); $datanum = mysqli_num_rows($result); //循环输出所有留言,如果管理员已经回复则同时输出回复 for( $i = 1; $i <= $datanum; $i++){//$datanum??? $info = mysqli_fetch_array($result); echo "->[".$info['author']."]于".$info['addtime']."说:<br>"; echo " ".$info['content']."<br>"; if( $info['reply'] != ""){ // <b></b>显示粗体 echo "<b>管理员回复:</b>".$info['reply']."<br>"; } echo "<hr>"; }//else结束 echo "</td></tr></table>"; } mysqli_close($link) ?> </td> </tr> <tr> <td height=80 bgcolor=#6c6c6c align=center> <font color="#FFFFFF"> 版权所有:<a href="http://blog.csdn.net/cherish0222" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Vmorish</a><br> E-mail:vmorish@163.com </font> </td> </tr> </table> </body> </html>
效果:
接著管理員登入咯
<!-- 3.管理员登录页面 login.php --> <!-- 供管理员登录 --> <!-- 体会session实现用户登录的方法 --> <?php $name = $_POST["name"]; if( $name != ""){ $password = $_POST['password']; $link = mysqli_connect("127.0.0.1","root","Vmorish"); mysqli_select_db($link,"gbook"); $query = "select * from admin where username = '$name'"; $result = mysqli_query($link,$query); if( mysqli_num_rows($result) < 1){ echo "该用户不存在,请重新登录!<br>"; }else{ $info = mysqli_fetch_array($result); if( $info['userpass'] != $password){ echo "密码输入错误,请重新登录!<br>"; }else{ //如果用户名密码都正确,则注册一个session来标记其登录状态 echo "hhhh<br>"; session_start(); // $_SESSION["login"] = "YES"; echo "<script language=javascript>alert('登录成功!');location.href='manage.php';</script>"; } } mysqli_close($link); } ?> <html> <head> <title>欢迎来到陈雨情的留言本吼吼吼</title> </heda> <body> <table border=1 cellspacing=0 cellspadding=0 style="border-collapse:collapse" align=center width=400 bordercolor=black height="358"> <tr> <td height=100 bgcolor=#6c6c6c style="font-size:30px;line-height:30px"> <font color=#ffffff face="黑体">欢迎来到×××的留言本吼吼吼</font> </td> </tr> <tr> <td height=25> <a href=send.php>[我要写留言]</a> <a href=login.php>[管理留言]</a> </td> </tr> <tr> <td height=178> <form method="POST" action="login.php"> <table border="1" width="95%" id="table1" cellspcing="0" cellpadding="0" bordercolor="#808080" style="border-collapse" height="154"> <tr> <td colspan="2" height="29"> <p align="center">欢迎管理员登录</p> </td> </tr> <tr> <td width="32%"> <p align="center">用户名</P> </td> <td width="67%"> <input type="text" name="name" size="20"> </td> </tr> <tr> <td width="32%"> <p align="center">密 码</p> </td> <td> <input type="password" name="password" size="20"> </td> </tr> <tr> <td width="99%" colspan="2"> <p align="center"><input type="submit" value="登录" name="B1"></p> </td> </tr> </table> </form> </td> </tr> <tr> <td height=80 bgcolor=#6c6c6c align=center> <font color="#FFFFFF"> 版权所有:<a href="http://blog.csdn.net/cherish0222" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Vmorish</a><br> E-mail:vmorish@163.com </font> </td> </tr> </table> </body> </html>
效果:
#manage.php和reply.php和前面類似,就不給了(我也還沒寫好誣…但要實現的跟前面類似
最後註銷登入
<!-- 6.注销登录页面 --> <?php session_start(); $_SESSION["login"]=""; echo "已成功退出。[<a href=index.php>回首页</a>]"; exit; ?>
##相關學習推薦:php程式設計(影片)
以上是PHP+MySql實作簡單的留言板功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中