国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php與mysql留言板程序?qū)崿F(xiàn)代碼

2024-05-04 23:06:25
字體:
供稿:網(wǎng)友

本程序僅供php+mysql愛好者學(xué)習(xí)測試用,測試過程中,可能會出現(xiàn)錯誤,請朋友們根據(jù)錯誤提示修改本程序,本程序?yàn)榘惭b版,先運(yùn)行install目錄,然后根據(jù)提示操作.

關(guān)于PHP語法的那些就不說了,我就說一下連接mysql吧,代碼如下:

  1. $conn =@mysql_connect("localhost","root","mydown"or die("數(shù)據(jù)庫鏈接錯誤"); 
  2. mysql_select_db("bbs"$conn); 
  3. mysql_query("set names 'GBK'");  

第一句就是描述了連接數(shù)據(jù)庫的語句,并且如果失敗的提示”數(shù)據(jù)庫連接錯誤“;前面的三個參數(shù)分別代表了 數(shù)據(jù)庫地址localhost,數(shù)據(jù)庫用戶名root,連接數(shù)據(jù)庫密碼mydown;

第二句就是描述了連接到數(shù)據(jù)庫表bbs,后面的$conn代表哪個數(shù)據(jù)庫.

第三句話就是轉(zhuǎn)換編碼格式,顯示中文.

連接好了數(shù)據(jù)庫之后,我們來看下 數(shù)據(jù)庫語句怎么在PHP中提交運(yùn)行,代碼如下:

  1. $sql="INSERT INTO message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())"
  2. mysql_query($sql);  

上面中的藍(lán)色字體大家應(yīng)該面熟,意思就是 在數(shù)據(jù)庫中插入信息,第二句是在數(shù)據(jù)庫中運(yùn)行該語句,想起中類似$_POST[title]的變量,大家應(yīng)該知道,這是通過網(wǎng)頁提交獲取的信息.

PHP實(shí)例,install安裝,代碼如下:

  1. <html> 
  2. <head> 
  3. <title>留言板安裝</title> 
  4. </head> 
  5. <body> 
  6. <form action="check_install.php" method="POST"> 
  7. <table> 
  8. <tr><td align="right"><font color="Red">*</font> 輸入數(shù)據(jù)庫服務(wù)器地址:</td><td><input name="llcc_server" type="text" value="localhost"></td></tr> 
  9. <tr><td align="right"><font color="Red">*</font> 輸入數(shù)據(jù)庫服務(wù)器用戶名:</td><td><input name="llcc_user_name" type="text" value=""></td></tr> 
  10. <tr><td align="right"><font color="Red">*</font> 輸入數(shù)據(jù)庫服務(wù)器密碼:</td><td><input name="llcc_user_pass" type="text" value=""></td></tr> 
  11. <tr><td align="right"><font color="Red">*</font> 輸入數(shù)據(jù)庫名稱:</td><td><input name="llcc_data_name" type="text" value=""> </td></tr> 
  12. <tr><td><input type="submit" name="bt_install" value="提交"></td></tr> 
  13. </table> 
  14. </form> 
  15. </body> 
  16. </html> 

check_install.php,代碼如下:

  1. <?php 
  2. //獲取提交的數(shù)據(jù)庫值 
  3. $llcc_server=$_POST['llcc_server']; 
  4. $llcc_user_name=$_POST['llcc_user_name']; 
  5. $llcc_user_pass=$_POST['llcc_user_pass']; 
  6. $llcc_data_name=$_POST['llcc_data_name']; 
  7. //打開數(shù)據(jù)庫配置文件 
  8. $fp=fopen("../config.php",'w'); 
  9.  
  10. if (!$fp) { 
  11.     echo "sorry,try again!"
  12. //寫入文件內(nèi)容 
  13. $outconfig="<?phpn"
  14. $outconfig.='$server='
  15. $outconfig.="".$llcc_server.";//數(shù)據(jù)庫所在IP,如果是本地,默認(rèn)localhostn"
  16. $outconfig.='$server_name='
  17. $outconfig.="".$llcc_user_name.";//連接數(shù)據(jù)庫用戶名,一般為rootn"
  18. $outconfig.='$server_pass='
  19. $outconfig.="".$llcc_user_pass.";//連接數(shù)據(jù)庫密碼。n"
  20. $outconfig.='$server_data='
  21. $outconfig.="".$llcc_data_name.";//數(shù)據(jù)庫名n"
  22. $outconfig.="?>"
  23. $fw=fwrite($fp,$outconfig); 
  24.  
  25. if ($fw) { 
  26.     echo "<br><br><br><center>安裝完成!請手動刪除目錄下install文件夾<br><a href='../'>返回首頁</a></center>"
  27. ?> 

login.php,代碼如下:

  1. <?php 
  2. include("header.php"); 
  3. include("conn.php"); 
  4.  
  5.   if($_GET[out]){ 
  6.    setcookie("cookie""out"); 
  7.     echo "<script language="javascript">location.href='login.php';</script>"
  8.   } 
  9.  
  10.  
  11.   if($_POST[id]=='admin'){ 
  12.     $pw=md5($_POST[pw]); 
  13.     if($pw=='1b3ae77a2e06580c8cb57e112ddc4a26'){ 
  14.      setcookie("cookie""ok"); 
  15.        echo "<script language="javascript">location.href='login.php';</script>"
  16.     } 
  17.   } 
  18. if($_COOKIE['cookie']!='ok'){ 
  19. ?> 
  20.  
  21. <SCRIPT language=javascript> 
  22. function Checklogin() 
  23.  if (myform.id.value==""
  24.  { 
  25.   alert("請?zhí)顚懙卿浢?quot;); 
  26.   myform.id.focus(); 
  27.   return false; 
  28.  } 
  29.   if (myform.pw.value==""
  30.  { 
  31.   alert("密碼不能為空"); 
  32.   myform.pw.focus(); 
  33.   return false; 
  34.  } 
  35. </SCRIPT> 
  36. <table width="400"
  37. <form action="" method="post" name="myform" onsubmit="return Checklogin();"
  38.   <tr><th>登錄名:</th><td><input type="text" name="id" /></td></tr> 
  39.   <tr><th>密碼:</th><td><input type="password" name="pw" /></td></tr> 
  40.   <tr><td colspan="2"><input type="submit" name="submit" value="登陸"/></td></tr> 
  41.   </form> 
  42.   </table> 
  43. <? 
  44. }else
  45. ?> 
  46.  <a href='?out=login'>退出</a><form name="ff"
  47.  <input type="submit" name="sub" value="清空數(shù)據(jù)庫"></form> 
  48.  <?php 
  49.  if($_GET[sub]=="清空數(shù)據(jù)庫"
  50.  { 
  51.  $sql="delete from message"
  52.  $query=mysql_query($sql); 
  53.  } 
  54.  ?> 
  55. <?php 
  56. ?> 

conn.php數(shù)據(jù)庫連接,代碼如下:

  1. <?php 
  2. include("config.php"); 
  3. $conn =@mysql_connect($server,$server_name,$server_passor die("數(shù)據(jù)庫鏈接錯誤"); 
  4. mysql_select_db($server_data$conn);//此處代表的要打開的數(shù)據(jù)庫名稱,"bbs"是我的數(shù)據(jù)庫名,修改成自己的! 
  5. mysql_query("set names 'GBK'"); //使用GBK中文編碼; 
  6. ?> 

index.php文件,代碼如下:

  1. <?php 
  2. include("conn.php"); 
  3. include("header.php"); 
  4. ?> 
  5. 自由屋留言板<hr width="70%"><table width=750 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef"
  6. <?php 
  7. $sql="select * from message order by id DESC"
  8. $query=mysql_query($sql); 
  9. while($row=mysql_fetch_array($query)){ 
  10. ?> 
  11.   <tr bgcolor="#eff3ff"
  12.   <td><b><?php echo $row[id];?>用戶名:</b><?php echo $row[user];?>&nbsp;&nbsp;&nbsp;&nbsp;<b>標(biāo)題:</b><?php echo $row[title];?>&nbsp;&nbsp;&nbsp;<b>發(fā)表時間:</b><?php echo $row[lastdate];?></td> 
  13.   </tr> 
  14.   <tr bgColor="#ffffff"
  15.   <td><b>內(nèi)容:<?php echo $row[content];?></b></td> 
  16.   </tr> 
  17.  <?php  
  18.   } 
  19. ?> 
  20. </table> 

add.php增加留方程序,代碼如下:

  1. <?php 
  2. include("header.php"); 
  3. include("conn.php"); 
  4. if($_POST["submit"]) 
  5. $sql="INSERT INTO message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())"
  6. mysql_query($sql); 
  7. echo "留言已提交"
  8. }; 
  9. ?><center> 
  10.  
  11. <table border="0" width="450"
  12. <form name="add" method="post" onsubmit="return CheckPost();"
  13. <tr><th>用戶:</th><td><input type="text" name="user" size="10"></td></tr> 
  14. <tr><th>標(biāo)題:</th><td><input type="text" name="title" size="30"></td></tr> 
  15. <tr><th>內(nèi)容:</th><td><textarea name="content"></textarea></td></tr> 
  16. <tr><td colspan="2" align="center"><input type="submit" name="submit" value="發(fā)表留言"> &nbsp;&nbsp;&nbsp;<input type="reset" value="重設(shè)"></td></tr> 
  17. </form> 
  18. </table> 
  19. </center> 
  20.  
  21. <SCRIPT language=javascript> 
  22. function CheckPost() 
  23.  if (add.user.value==""
  24.  { 
  25.   alert("請?zhí)顚懹脩裘?quot;); 
  26.   add.user.focus(); 
  27.   return false; 
  28.  } 
  29.  if (add.title.value.length<5) 
  30.  { 
  31.   alert("標(biāo)題不能少于5個字符"); 
  32.   add.title.focus(); 
  33.   return false; 
  34.  } 
  35.  if (add.content.value==""
  36.  { 
  37.   alert("必須要填寫留言內(nèi)容"); 
  38.   add.content.focus(); 
  39.   return false; 
  40.  } 
  41. </SCRIPT> 

test.php數(shù)據(jù)庫結(jié)構(gòu),代碼如下:

  1. <?php 
  2. include("config.php"); 
  3. include("conn.php"); 
  4. $sql="CREATE TABLE `message` ( 
  5.   `id` tinyint(1) NOT NULL auto_increment, 
  6.   `uservarchar(25) NOT NULL
  7.   `title` varchar(50) NOT NULL
  8.   `content` tinytext NOT NULL
  9.   `lastdate` date NOT NULL
  10.   PRIMARY KEY  (`id`) 
  11. ) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;"; 
  12. $query=mysql_query($sql); 
  13. if($query){ 
  14. echo"執(zhí)行成功";} 
  15. else echo"執(zhí)行失敗!"
  16. ?> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 富平县| 小金县| 民勤县| 安徽省| 镇远县| 大连市| 合阳县| 青龙| 湟源县| 图们市| 晋城| 玉田县| 西平县| 集贤县| 乌拉特前旗| 西青区| 商洛市| 兴化市| 闸北区| 平舆县| 郯城县| 宣武区| 梅河口市| 应城市| 河北区| 巴彦县| 武安市| 隆尧县| 广宁县| 渭源县| 石屏县| 罗定市| 剑河县| 株洲县| 黄冈市| 康乐县| 金华市| 元阳县| 惠安县| 衡阳市| 泸西县|