Home >php教程 >php手册 >一个php留言板实例详解

一个php留言板实例详解

WBOY
WBOYOriginal
2016-05-25 16:48:141424browse

留言板可以说是所有php入门者都会练习的一个小不上的WEB应用程序了,下面我把我写的一个php留言板实例分享给各位同学,有需要了解的同学可进入参考参考.

1.在你的PHP的根目录下创建一个名为"msgboard"的文件夹.在"msgboard"下创建一个"msglist.php"文件,数据表结构大家可直接导入。

实例代码如下:

CREATE TABLE `msgboard` ( 
  `id` int(10) NOT NULL AUTO_INCREMENT, 
  `username` varchar(50) CHARACTER SET latin1 NOT NULL, 
  `sex` tinyint(1) NOT NULL DEFAULT '1', 
  `msg` text CHARACTER SET latin1 NOT NULL, 
  PRIMARY KEY (`id`) 
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

msglist.php 文件,包括了留言增加,删除,修改

实例代码如下:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
<title>我的留言板</title> 
</head> 
<body> 
<?php
$username = isset($_REQUEST[&#39;username&#39;]) ? $_REQUEST[&#39;username&#39;] : &#39;&#39;; //姓名
$sex = isset($_REQUEST[&#39;sex&#39;]) ? intval($_REQUEST[&#39;sex&#39;]) : 1; //性别
$msg = isset($_REQUEST[&#39;msg&#39;]) ? $_REQUEST[&#39;msg&#39;] : &#39;&#39;; //留言
mysql_connect("127.0.0.1", "root", "lzy"); //链接
mysql_select_db("test"); //选择数据库
if (!emptyempty($username) && !emptyempty($msg)) {
    mysql_query("INSERT INTO msgboard(username,sex,msg) VALUES(&#39;$username&#39;,&#39;$sex&#39;,&#39;$msg&#39;)");
} else {
    echo "输入不正确<br/>";
}
$source = mysql_query("SELECT * FROM msgboard ORDER BY id DESC");
$result = array();
?>
 <table border="1" width="1000"> 
  <tr align="center"> 
   <td width="10%">姓名</td> 
   <td width="10%">性别</td> 
   <td width="80%">留言内容</td> 
  </tr>  
  <tr> 
  <?php
while ($row = mysql_fetch_array($source)) {
    echo &#39;<td>&#39; . $row[&#39;username&#39;] . &#39;</td>&#39;;
    echo &#39;<td>&#39; . ($row[&#39;sex&#39;] == 1 ? &#39;男&#39; : &#39;女&#39;) . &#39;</td>&#39;;
    echo &#39;<td>&#39; . $row[&#39;msg&#39;] . &#39;</td>&#39;;
}
?>
  </tr> 
 </table><p/> 
 
 <form action="msglist.php" method="POST"> 
 <table width="1000" align="left"> 
  <tr> 
   <td width="100%"> 
姓名:<input type="text" name="username" value=""/> 
   </td> 
  </tr> 
  <tr> 
   <td width="100%"> 
性别:男<input type="radio" name="sex" value="1" checked="checked" />    
  女<input type="radio" name="sex" value="0" /> 
   </td> 
  </tr> 
  <tr> 
   <td width="100%"> 
请留言:<br/><textarea name="msg" rows="5" cols="100"></textarea> 
   </td> 
  </tr>  
  <tr> 
   <td width="100%"> 
 <input type="submit" value="提 交" /> 
   </td> 
  </tr>  
 </table> 
 </form> 
</body> 
</html>


本文地址:

转载随意,但请附上文章地址:-)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn