這篇文章主要為大家詳細介紹了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)與web開發緊密結合,處理HTTP請求和數據庫交互出色。 3)龐大的生態系統提供了豐富的工具和庫。 4)活躍的社區和開源性質使其適應新需求和技術趨勢。

PHP和Python都是高層次的編程語言,廣泛應用於Web開發、數據處理和自動化任務。 1.PHP常用於構建動態網站和內容管理系統,而Python常用於構建Web框架和數據科學。 2.PHP使用echo輸出內容,Python使用print。 3.兩者都支持面向對象編程,但語法和關鍵字不同。 4.PHP支持弱類型轉換,Python則更嚴格。 5.PHP性能優化包括使用OPcache和異步編程,Python則使用cProfile和異步編程。

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP在現代化進程中仍然重要,因為它支持大量網站和應用,並通過框架適應開發需求。 1.PHP7提升了性能並引入了新功能。 2.現代框架如Laravel、Symfony和CodeIgniter簡化開發,提高代碼質量。 3.性能優化和最佳實踐進一步提升應用效率。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

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

禪工作室 13.0.1
強大的PHP整合開發環境