一,mysql數據庫配置,php
使用phpmyadmin新建一個數據庫,message,新建一個text的表,並增長兩個字段:context varchar(30),date datetime;html
<!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代碼以下:mysql
<!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:sql
<?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:shell
<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(); } } ?>
遇到的問題:ide
1,php取到的時間不能直接插入到mysql數據庫中,須要使用$today = date("Y-m-d H:i:s",time());post
2,取到的時間時區不對,須要在php.ini中修改時區爲PRC;fetch
3,分頁,使用了網上的分頁代碼;http://www.phpfreaks.com/tutorial/basic-paginationui
這個分頁代碼最爲簡潔.並且很好用.
4,因爲使用的是PDO,分頁代碼中獲取select取出的行數,參考了http://www.php.net/manual/zh/pdostatement.rowcount.php
即便用PDOStatement::fetchColumn();