這篇文章主要為大家詳細介紹了PHP簡單留言本功能的實作程式碼,具有一定的參考價值,有興趣的小夥伴們可以參考一下
#本文實例為大家分享了PHP留言本功能的具體程式碼,供大家參考,具體內容如下
index.php
<?php error_reporting(0); //关闭NOTICE提示 require_once "conn.php"; $pagesize=5; //每页显示5条数据 $sql="select count(*) from guestlist "; //选择数据库,计算符合条件的行数并返回行数 $result= mysql_query($sql); //执行,如果成功则返回结果集(从数据库中找到所有的数据,返回条数) $row = mysql_fetch_row($result); //获得数组 Array[0]="数据库里的总条数" $infoCount =$row[0]; //获得总条数:取得数组中的值$row[0]="数据库里的总条数" $pageCount = ceil($infoCount/$pagesize); //获取总页数(总个数/每页的个数5) $currpage=empty ($_GET["page"])?1:$_GET["page"]; //如果当前页为空 则定义page=1即$currpage=1反之亦然 if($currpage>$pageCount) //如果输入的页数超过总页数则默认跳转到最后一页 { $currpage=$pageCount; } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <!--此处添加了bootstrip样式--> <link href="../dist/css/bootstrap.min.css" rel="external nofollow" type="text/css" rel="stylesheet" /> <link href="css/index.css" rel="external nofollow" type="text/css" rel="stylesheet" /> <script> function test(){ var sum; if(document.frm.title.value==''){ alert('请填写标题'); return false; }else{ sum =document.frm.title.value.length; if(sum<5 || sum>20){ alert('标题长度 5-20个字符'); return false; } } if(document.frm.username.value==''){ alert('请填写用户网名'); return false; } if(document.frm.content.value==''){ alert("请填写内容"); return false; } return true; } </script> </head> <body> <p class="content"> <h5 id="php-nbsp-echo-nbsp-infoCount-条留言"><?php echo $infoCount;?>条留言</h5><br/> <ul class="bt"> <li>留言标题</li> <li>用户网名</li> <li>时间</li> </ul> <?php //从当前页开始 向下取出5个 $re= mysql_query("select * from guestlist order by id desc limit ".($currpage-1)*$pagesize.",".$pagesize); while($row= mysql_fetch_assoc($re)) //得到一行数据的数组,再执行则得到再下一行,如果得到是最后一行,那么再执行则返回false { ?> <ul class="nr"> <li><?php echo $row["title"];?></li> <li><?php echo $row["username"];?></li> <li><?php echo $row["addtime"];?></li> </ul> <p class="lynr"> <p><strong>留言内容:</strong></p><span><?php echo $row["content"];?></span> </p> <?php } ?> <hr style="width:800px"/> <ul class="pagination"> <!--上一页--> <?php for($i=1;$i<=$pageCount;$i++) { if($i==$currpage) { echo "<li><a href=?page=".($i-1).">«</a></li>"; } } ?> <!--数字页--> <?php for($i=1;$i<=$pageCount;$i++) { if($i==$currpage) { echo "<li ><a style='background-color:#EEEEEE'>$i</a></li>"; }else{ echo "<li><a href='?page=$i'>$i</a></li>";} } ?> <!--下一页--> <?php for($i=1;$i<$pageCount;$i++) { if($i==$currpage) { echo "<li><a href=?page=".($i+1).">»</a></li>"; } } ?> </ul> <br/> <ul> </ul> <hr/> <strong style="color:red">发表留言</strong> <form action="result.php" method="post" name="frm" onsubmit="return test()"> <table cellpadding="0" cellspacing="0" > <tr> <td >留言标题:</td> <td><input type="text" name="title" autocomplete="off"/></td> </tr> <tr> <td>网名:</td> <td><input type="text" name="username" autocomplete="off"/></td> </tr> <tr> <td>留言内容:</td> <td><textarea name="content" cols="42" rows="5" autocomplete="off"/></textarea></td> </tr> <tr> <td></td> <td><input class="btn" type="submit" name="submit" value="提交"/></td> </tr> </table> </form> </p> </body> </html>
conn.php
<?php $link = mysql_connect("localhost","root"," "); mysql_select_db("guestbook"); mysql_query("set names utf-8"); if(!$link){ die("Connection failed: " . mysqli_connect_error()); } //echo "链接成功"; ?>
result.php
<?php error_reporting(0); //关闭NOTICE提示 require_once "conn.php"; $title = $_REQUEST['title']; $username = $_REQUEST['username']; $content = $_REQUEST['content']; $content = str_replace("\n","<br>",str_replace(" "," ",$content)); //显示'空格'和'回车' $week = '星期'.mb_substr( "日一二三四五六",date("w"),1,"utf-8" ); $isok =mysql_query("insert into guestlist(title,username,content,addtime)values('$title','$username','$content','".date("Y-m-d H:i:s")." $week ')"); if($isok) { echo "<script> alert('提交成功'); location.href='index.php'; </script>"; }else { echo "<script> alert('提交失败'); location.href='index.php'; </script>"; } ?>
css /index.css
body{margin:0;padding:0;} ul,li{list-style: none;margin:0;padding:0;} a{text-decoration: none;} .content{ width:800px; margin:0 auto; } .bt{ width:799px; height:20px; text-align: center; background:#EB9316; margin:0 0 5px 0; } .bt>li{ float:left; width:265px; height:20px; text-align: center; line-height: 20px; font-size:13px; } .nr{ float:left; /*如果不浮动 后面的lynr会受影响*/ width:799px; height:20px; text-align: center; background:#B9DEF0; } .nr>li{ float:left; width:265px; height:20px; text-align: center; line-height: 20px; font-size:13px; } .lynr{ float:left; /*如果不浮动会 布局会乱*/ width:800px; margin:1px 0 1px 0; } .content p{ width:70px; height:50px; float:left; } .content span{ display: block; width:710px; float:left; } td{ width:80px; padding:5px 0; /*border: 1px solid #79ABFE;*/ } td input,textarea{ border: 1px solid #79ABFE; } /*tr{ display:block; /*将tr设置为块体元素 显示块状后 就将其包围住了 不是一个矩形了 }*/
dist/css/bootstrap.min.css(自己下載)
效果圖:
相關推薦:
#
以上是PHP簡單留言本功能實作程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP可以輕鬆創建互動網頁內容。 1)通過嵌入HTML動態生成內容,根據用戶輸入或數據庫數據實時展示。 2)處理表單提交並生成動態輸出,確保使用htmlspecialchars防XSS。 3)結合MySQL創建用戶註冊系統,使用password_hash和預處理語句增強安全性。掌握這些技巧將提升Web開發效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

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

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

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具