이 글은 주로 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 (직접 다운로드하세요)
렌더링:

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

SecList
SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.
