PHP簡單留言本功能的實現代碼
這篇文章主要為大家詳細介紹了PHP簡單留言本功能的實現代碼,具有一定的參考價值,感興趣的小夥伴們可以參考一下,想了解更多相關信息,請持續關注我們應屆畢業生考試網!
:
<?php
error_reporting(0); //關閉NOTICE提示
require_once "";
$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/" rel="external nofollow" type="text/css" rel="stylesheet" />
<link href="css/" rel="external nofollow" type="text/css" rel="stylesheet" />
<script>
function test(){
var sum;
if(e==''){
alert('請填寫標題');
return false;
}else{
sum =th;
if(sum<5 || sum>20){
alert('標題長度 5-20個字符');
return false;
}
}
if(e==''){
alert('請填寫用户網名');
return false;
}
if(e==''){
alert("請填寫內容");
return false;
}
return true;
}
</script>
</head>
<body>
<p class="content">
<h5 style="color: red;"><?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="" 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>
<?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 "鏈接成功";
?>
<?php
error_reporting(0); //關閉NOTICE提示
require_once "";
$title = $_REQUEST['title'];
$username = $_REQUEST['username'];
$content = $_REQUEST['content'];
$content = str_replace("n","<br>",str_replace(" "," ",$content)); //顯示'空格'和'回車'
$week = '星期'_substr( "日一二三四五六",date("w"),1,"utf-8" );
$isok =mysql_query("insert into guestlist(title,username,content,addtime)values('$title','$username','$content','"("Y-m-d H:i:s")." $week ')");
if($isok)
{
echo "<script>
alert('提交成功');
='';
</script>";
}else {
echo "<script>
alert('提交失敗');
='';
</script>";
}
?>
css/:
body{margin:0;padding:0;}
ul,li{list-style: none;margin:0;padding:0;}
a{text-decoration: none;}
ent{
width:800px;
margin:0 auto;
}
{
width:799px;
height:20px;
text-align: center;
background:#EB9316;
margin:0 0 5px 0;
}
>li{
float:left;
width:265px;
height:20px;
text-align: center;
line-height: 20px;
font-size:13px;
}
{
float:left; /*如果不浮動 後面的lynr會受影響*/
width:799px;
height:20px;
text-align: center;
background:#B9DEF0;
}
>li{
float:left;
width:265px;
height:20px;
text-align: center;
line-height: 20px;
font-size:13px;
}
{
float:left; /*如果不浮動會 佈局會亂*/
width:800px;
margin:1px 0 1px 0;
}
ent p{
width:70px;
height:50px;
float:left;
}
ent 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/(自己下載)
下載地址:
-
PHP中多態如何實現
PHP是一種HTML內嵌式的,用來製作動態網頁的服務器端的腳本語言。以下是本站小編整理的PHP中多態如何實現的內容,歡迎學習!1PHP語言介紹PHP是一種HTML內嵌式的,用來製作動態網頁的服務器端的腳本語言。其特點是:開發週期短,穩定安全,簡單易學,免費開源,良好的跨平台特性...
-
php統計數組元素個數的方法
PHP是一種HTML內嵌式的語言,是一種在服務器端執行的嵌入HTML文檔的腳本語言,語言的.風格有類似於C語言,被廣泛地運用。下面是小編為大家搜索整理的php統計數組元素個數的方法,希望大家能有所收穫,更多精彩內容請及時關注我們應屆畢業生考試網!count():對數組中的元...
-
2017年PHP錯誤總結範文
導語:如果你還是PHP新手的話,不妨就找些PHP錯誤點來看看。下面的是本站小編為大家蒐集的2017年PHP錯誤總結,供大家參考。錯誤總結PHP-不斷更新(新手必看)做開發請開啟全部錯誤提示:error_reporting=E_ALL|E_STRICT屏蔽錯誤提示等於掩耳盜鈴。代碼寫規範,錯誤少一半...
-
php通過sudo執行root指令
PHP是一種很強大的語言,那麼下面就一起來看他在指令方面的應用吧!下面是本站小編精心為大家整理的php通過sudo執行root指令,希望對大家學習有幫助,更多內容請關注應屆畢業生網!1.高級的用法如限制主機、用户組、命令組的用法可以參照下面的規則,詳細可參看"mansudo...