留言版 1.先Create一個留言板的table mysql> use dbname; 建立gbook table mysql> CREATE TABLE gbook(id int unsigned not null auto_increment,name char(20) not null, > email char(50),face tinyint,ip char(20),ctime datetime,title char(50) not null, > content text,primary key(id)); 建立一個可以從internet連線進入學校存取gbook的user mysql> GRANT insert,update,delete,select on dbname.gbook to iuser@localhost > identified by '1234'; 2.一般設定檔:設定資料庫參數 <global.php> <? $HOST="localhost"; $DB=“dbname"; $TABLE1="gbook"; $USER="iuser"; $PASS="1234"; $FACE=array("普通","高興","難過","快樂"); $PAGE=5; ?>
新增留言 add.php 新增留言資料:[範例] //database連結 $link=mysql_pconnect($HOST,$USER,$PASS); //選擇資料庫 mysql_select_db($DB,$link); //取得來源ip $ip=getenv("REMOTE_ADDR"); //新增SQL語法 $str="INSERT INTO $TABLE1 (name,email,face,ip,ctime,title,content) VALUES('$name','$email',$face,'$ip',now(),'$title','$content')"; //執行SQL命令 mysql_query($str,$link);
新增留言-原始碼 <? include("global.php"); if($submit){ $link=mysql_pconnect($HOST,$USER,$PASS); mysql_select_db($DB,$link); $ip=getenv("REMOTE_ADDR"); $str="INSERT INTO $TABLE1 (name,email,face,ip,ctime,title,content) VALUES('$name','$email',$face,'$ip',now(),'$title','$content')"; mysql_query($str,$link); echo "<h1>您的資料已經新增了</h1>"; echo "<a href=view.php>觀看留言</a>"; exit; } ?> <HTML> <HEAD><TITLE>留言板資料新增</TITLE></HEAD> <BODY bgcolor="FFFFFF"> <h1>新增留言</h1> <a href="view.php">觀看留言</a> <FORM METHOD=POST ACTION="add.php"> 姓 名:<INPUT TYPE="text" NAME="name"><br> 電子郵件:<INPUT TYPE="text" NAME="email"><br> 心 情:<SELECT NAME="face"><option value=0>普通</option><option value=1>高興</option><option value=2>難過</option><option value=3>快樂</option></SELECT><br> 留言主旨:<INPUT TYPE="text" NAME="title"><br> <p> <TEXTAREA NAME="content" ROWS="5" COLS="50"></TEXTAREA><br> <INPUT TYPE="submit" name="submit" value="填好了!送出"> </FORM> </BODY> </HTML> 新增留言-原始碼
瀏覽留言 view.php 觀看留言資料:[範例] //讀取資料,var1,var2...為欄位 list($var1,$var2,...)=mysql_fetch_row($result); //重覆讀取多筆資料 while(list($id,$name,$email,$face,$ip,$ctime,$title,$content)=mysql_fetch_row($result)) { ...... }
瀏覽留言-程式碼 <? include("global.php"); $link=mysql_pconnect($HOST,$USER,$PASS); mysql_select_db($DB); $str="select * from $TABLE1"; $result=mysql_query($str); ?> <HTML> <HEAD><TITLE>顯示留言板</TITLE></HEAD> <BODY bgcolor="FFFFFF"> <H1>留言板</H1> <a href="add.php">新增留言</a> <TABLE border=0> <?while(list($id,$name,$email,$face,$ip,$ctime,$title,$content)=mysql_fetch_row($result)){ echo "<TR>"; echo "<TD>"; echo "留言者 :".$name."<br>"; echo "電子郵件:".$email."<br>"; echo "心情 :".$FACE[$face]."<br>"; echo "日期 :".$ctime."<br>"; echo "IP :".$ip."<br>"; echo "主旨 :".$title."<br>"; echo nl2br($content)."<br>"; echo "<hr>"; echo "</TD></TR>"; } </TABLE> </BODY> </HTML> 瀏覽留言-程式碼
nl2br nl2br -- Inserts HTML line breaks before all newlines in a string Description string nl2br ( string string) Returns string with '<br />' inserted before all newlines. Note: Starting with PHP 4.0.5, nl2br() is now XHTML compliant. All versions before 4.0.5 will return string with '<br>' inserted before newlines instead of '<br />'. Example 1. using nl2br() <?php echo nl2br("foo isn't\n bar"); ?> this will output : foo isn't<br /> bar
分頁view_page.php //------------------------------------------------- $num=mysql_num_rows($result); //總筆數 if ($num){ $pages=intval(($num-1)/3)+1; //總頁數 if ($page<1) $page=1; if ($page>$pages) $page=$pages; mysql_data_seek($result,($page-1)*3); //重新取的資料 }
分頁view_page.php <? for ($i=1;$i<=3;$i++){ if($record=mysql_fetch_row($result)){ echo "<TR>"; echo "<TD>"; echo "編號 :".$record[0]."<br>"; echo "留言者 :".$record[1]."<br>"; echo "電子郵件:".$record[2]."<br>"; echo "心情 :".$record[3]."<br>"; echo "日期 :".$record[5]."<br>"; echo "IP :".$record[4]."<br>"; echo "主旨 :".$record[6]."<br>"; echo nl2br($record[7])."<br>"; echo "<hr>"; echo "</TD></TR>"; }} ?>
分頁view_page.php <table width="100%" border="0" align="center"> <tr> <td width="66%"> <div align="right"> <a href="view_page.php?page=<? if ($num){echo ($page-1);}?>">前一頁</a> | <a href="view_page.php?page=<? if ($num){echo ($page+1);}?>">下一頁</a> </div> </td> <td width="34%"> <div align="center"> <font size="2">頁數:<? echo $page;?> / <? echo $pages;?> <select name="menu1" onChange="MM_jumpMenu('self',this,0)"> <option selected>跳頁</option> <? for ($i=1;$i<=$pages ; $i++){?> <option value="view_page.php?page=<? echo $i;?>">第<? echo $i;?>頁</option> <? }?> </select> </font> </tr> </table>
分頁view_page.php <script language="JavaScript" type="text/JavaScript"> <!-- function MM_jumpMenu(targ,selObj,restore){ //v3.0 eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); if (restore) selObj.selectedIndex=0; } //--> </script>