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/(自己下载)
下载地址:
-
为什么说结对编程实际操作起来很难
人们常说团结就是力量,那么在编程中人多是不是也能产生更高的效率呢?下面就和小编一起来看看吧!更多内容请关注应届毕业生网!结对编程有助于提高软件质量和加强团队成员合作。它有非常多的好处,但是,团队成员真的很容易结成对子吗?MarcosBrizeno,巴西ThoughtWorks...
-
如何利用PHP访问带有密码的Redis
导语:如何利用PHP访问带有密码的Redis方法,下面小编给大家提供了代码实现步骤,大家可以参考阅读,更多详情请关注应届毕业生考试网。一、首先设置Redis密码,以提供远程登陆打开配置文件,找到requirepass,然后修改如下:requirepassyourpasswordyourpassword就是redis验...
-
PHP数据类型之数组变量详解
导语:PHP是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。下面我们来看看PHP数据类型之数组变量详解,希望对大家有所帮助。PHP中的数组实际上是一个有序图,是一种把values映射到keys的类型。一个数组实际上就...
-
PHP学习入门的一些基础知识
导语:PHP是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。下面我们来看看PHP学习入门的一些基础知识,希望对大家有所帮助。一,PHP脚本代码标记PHP的脚本是文件中一对特殊标记所包括的内容,如ASP是“”,PHP可看...