php搭建简单的留言板
一,mysql数据库配置,
使用phpmyadmin新建一个数据库,message,新建一个text的表,并增加两个字段:context varchar(30),date datetime;
增加一个message用户,并设密码123456;
给message用户赋予权限,在phpmyadmin里面,运行sql:
GRANT ALL PRIVILEGES ON `message` . * TO 'message'@'localhost' WITH GRANT OPTION ;
至此,数据库配置结束.
二,html
新建2个htm文档,分别为index.htm(主界面),leave_success.htm(留言成功提示);
index.htm代码如下:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html" /> <meta name="author" content="" /> <title>Messages</title> </head> <body> <a href="watch_message.php">Watch Messages</a> <p> Please Submit your message! </p> <form action="leave_message.php" method="post"> <p><textarea name="text_area" rows="10" cols="30"></textarea> <p><input type="submit" value="Submit" /></p> </form> </body> </html>
leave_success.htm代码如下:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html" /> <meta name="author" content="" /> <title>Leave_success</title> </head> <body> <p>Leave message ok!</p> <a href='index.htm'>Back</a> </body> </html>
新建connect.php(连接数据库),watch_message.php(查看留言),leave_message.php(留言主页面)
connect.php:
<?php try{ $dsn='mysql:host=localhost;dbname=message'; $username='message'; $password='123456'; $dbh = new PDO($dsn,$username,$password); // $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,FALSE); }catch(Exception $e){ die("Unable to connect:".$e->getMessage()); } ?>
watch_message.php:
<p>Message_list</p> <a href='index.htm'>Back</a><hr> <?php require_once "connect.php"; try{ $sql = "SELECT COUNT(*) FROM text"; $res = $dbh->query($sql); $res ->execute(); $result_num = $res->fetchColumn(); $rowsperpage = 3; $totalpages = ceil($result_num / $rowsperpage); if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])){ $currentpage = (int) $_GET['currentpage']; }else{ $currentpage = 1; } if ($currentpage > $totalpages){ $currentpage = $totalpages; } if ($currentpage < 1){ $currentpage = 1; } $offset = ($currentpage - 1) * $rowsperpage; $sql = "SELECT context,date FROM text LIMIT $offset,$rowsperpage"; foreach($dbh->query($sql) as $row){ echo "<p>",$row['date'],"</p><p>",$row['context'],"</p>"; } if ($currentpage > 1){ echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; $prevpage = $currentpage - 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } if ($currentpage != $totalpages){ $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } //$sth = $dbh->prepare($sql); //$sth ->execute(); $res = null; $dbh = null; }catch(PDOException $e){ print "Error!:".$e->getmessage()."<br/>"; die(); } ?>
leave_message.php:
<?php $message_text=htmlspecialchars($_POST['text_area']); if($message_text==''){ Header("Location: index.htm"); }else{ $today = date("Y-m-d H:i:s",time()); ; require_once "connect.php"; try{ $dbh->exec("INSERT INTO text(context,date) values('$message_text','$today')"); $dbh = null; Header("Location: leave_success.htm"); }catch(PDOException $e){ print "Error!:".$e->getmessage()."<br/>"; die(); } } ?>
遇到的问题:
1,php取到的时间不能直接插入到mysql数据库中,需要使用$today = date("Y-m-d H:i:s",time());
2,取到的时间时区不对,需要在php.ini中修改时区为PRC;
3,分页,使用了网上的分页代码;http://www.phpfreaks.com/tutorial/basic-pagination
这个分页代码最为简洁.而且很好用.
4,由于使用的是PDO,分页代码中获取select取出的行数,参考了http://www.php.net/manual/zh/pdostatement.rowcount.php
即使用PDOStatement::fetchColumn();
参考:
界面:
index.htm
查看留言:
留言成功: