SAE实践——创建简单留言板
1. 创建数据库
将mysaeapptest的数据库初始化之后,建立新的数据库,在PHPMyAdmin输入下列SQL。
CREATE TABLE IF NOT EXISTS `ny_diary` ( `id` int(10) NOT NULL AUTO_INCREMENT, `title` varchar(1000) NOT NULL, `content` varchar(5000) NOT NULL, `date` datetime NOT NULL, `user` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=40 ;
2. 在index.php中输入下列代码。
1 <?php 2 // 连主库 3 $link=mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 4 5 // 连从库 6 // $link=mysql_connect(SAE_MYSQL_HOST_S.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 7 8 //echo $key = md5("900821Ny"); 9 ?> 10 <!DOCTYPE html> 11 <html> 12 <head> 13 <title>Simple Message</title> 14 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 15 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 16 <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /> 17 <meta name="apple-mobile-web-app-capable" content="yes"> 18 <meta name="apple-mobile-web-app-status-bar-style" content="black"> 19 <meta name="format-detection" content="telephone=no"> 20 21 <style> 22 a, a:visited, a:hover, a:link, a:active { text-decoration: none; color: #0080FF; } 23 body { margin: 0; background: #E6E6E6;} 24 label { width: 68px; display: inline-block; margin: 0 auto; text-align: right; vertical-align: top; } 25 input { width: 224px; margin: 0;padding: 1px 0; border: 1px solid #ccc; border-radius: 2px; line-height: 20px;} 26 textarea { width: 220px; height : 50px;} 27 input[type="submit"] { width: 300px; height: 28px; border: 1px solid #FFF; background: #0080FF; border-radius: 3px; color: #fff; } 28 .line { margin: 8px 0;} 29 .wrap { margin: 5px auto; padding: 5px; border-radius: 3px; width: 300px; background: #fff;} 30 .diary-block { width: 300px; padding: 5px; margin: 5px auto; border-radius: 3px; background: #fff;} 31 .diary-tit { margin: 0; border-bottom: dashed 1px #ccc; } 32 .diary-cont { margin: 0; padding: 5px 10px; text-indent: 20px; } 33 .diary-user { display: inline-block; } 34 .diary-date { display: inline-block; } 35 .diary-user, .diary-date { margin: 0 auto; } 36 .diary-bottom { margin: 0; text-align: right; } 37 .diary-page-span { text-align: center; vertical-align: middle;background: #fff; display: inline-block; border-radius: 2px; width: 17px; height: 17px; margin: 0 3px; padding: 2px;} 38 .diary-page-span a { display: block; } 39 .diary-page-div { width: 300px; height: 25px; margin: 0 auto; overflow-x: scroll; } 40 </style> 41 </head> 42 <body> 43 <?php 44 if($_POST) { 45 // var_dump($_POST); 46 $key = md5($_POST['nykey']); 47 $diary = array( 48 'tit' => $_POST['diarytit'], 49 'cont' => $_POST['diarycont'], 50 'date' => date("Y-m-d H:i:s", time()), 51 'user' => $_POST['name'] 52 ); 53 //echo "ok!"; 54 $insertSQL = "INSERT INTO `app_mysaeapptest`.`ny_diary` (`id`, `title`, `content`, `date`, `user`) VALUES (NULL, '".$diary['tit']."', '".$diary['cont']."', '".$diary['date']."', '".$diary['user']."');"; 55 $result = mysql_query($insertSQL); 56 } 57 58 if($link) { 59 mysql_select_db(SAE_MYSQL_DB,$link); 60 //your code goes here 61 //page part 62 $pageNo = 1; 63 //echo $_GET['pnum']; 64 if ($_GET['pnum']) { 65 $pageNo = $_GET['pnum']; 66 } 67 //limit condition id,size 68 $pageSize = 3; 69 $diaryId = ($pageNo - 1) * $pageSize; 70 //total page 71 $totalSQL = "SELECT COUNT(*) AS `total` FROM `ny_diary`"; 72 $totalRes = mysql_fetch_array(mysql_query($totalSQL)); 73 $totalNum = $totalRes['total']; 74 // var_dump($totalNum); 75 if ($totalNum) { 76 // var_dump($totalNum % $pageSize); 77 if ($totalNum % $pageSize) { 78 $page = $totalNum / $pageSize + 1; 79 } else { 80 $page = $totalNum / $pageSize; 81 } 82 } 83 84 //select 85 $selectSQL = "SELECT * FROM `ny_diary` ORDER BY `id` DESC LIMIT {$diaryId},{$pageSize}"; 86 $diaryResource = mysql_query($selectSQL); 87 //var_dump($diaryResource); 88 while ( $row = mysql_fetch_array($diaryResource) ) { 89 //echo '<div class="diary-id">'.$row['id'].'</div>'; 90 echo '<div class="diary-block">'; 91 echo '<div class="diary-tit"><strong>'.$row['title'].'</strong></div>'; 92 echo '<div class="diary-cont">'.$row['content'].'</div>'; 93 echo '<div class="diary-bottom">'; 94 echo '<div class="diary-user"><strong>'.$row['user'].'</strong>@</div>'; 95 echo '<div class="diary-date">'.$row['date'].'</div>'; 96 echo '</div>'; 97 echo '</div>'; 98 } 99 //page div 100 echo '<div class="diary-page-div">'; 101 $mid = 0; 102 for ($i = 1, $j = $page ; $i <= $page ; ++$i, --$j) { 103 if ($i <= 5 || $j <= 5) { 104 echo '<span class="diary-page-span"><a href="a.php?pnum='.$i.'" target="_self">'.$i.'</a></span>'; 105 } 106 if ($i == 5) { 107 echo '<span class="diary-page-span"><a href="a.php?pnum='.$i.'" target="_self">...</a></span>'; 108 } 109 } 110 echo '</div>'; 111 //only if the link var is true can show 112 ?> 113 114 <div class="wrap"> 115 <form action="" method="post"> 116 <div class="line"><label for="diary-tit">Title</label> 117 <input id="diary-tit" name="diarytit" type="text" /> 118 </div> 119 <div class="line"> 120 <label for="ny-key">Name</label> 121 <input type="text" id="ny-key" name="name"/> 122 </div> 123 <div class="line"> 124 <label for="diary-cont">Content</label> 125 <textarea id="diary-cont" name="diarycont"></textarea> 126 </div> 127 <div> 128 <input type="submit" /> 129 </div> 130 </form> 131 </div> 132 <?php 133 134 }//$link 135 ?> 136 137 </body> 138 </html>