php與mysql留言板程式實現代碼

來源:互聯網
上載者:User


關於PHP文法的那些就不說了,我就說一下串連mysql吧!

 代碼如下 複製代碼
$conn =@mysql_connect("localhost","root","mydown") or die("資料庫連結錯誤");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'");

第一句就是描述了串連資料庫的語句,並且如果失敗的提示”資料庫連接錯誤“;前面的三個參數分別代表了 資料庫地址localhost,資料庫使用者名稱root,串連資料庫密碼mydown;

第二句就是描述了串連到資料庫表bbs,後面的$conn代表哪個資料庫。

第三句話就是轉換編碼格式,顯示中文。

串連好了資料庫之後,我們來看下 資料庫語句怎麼在PHP中提交運行!

 代碼如下 複製代碼
    $sql="INSERT INTO message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);

上面中的藍色字型大家應該面熟,意思就是 在資料庫中插入資訊。第二句是在資料庫中運行該語句。想起中類似$_POST[title]的變數,大家應該知道,這是通過網頁提交擷取的資訊


執行個體

install安裝

 代碼如下 複製代碼

<html>
<head>
<title>留言板安裝</title>
</head>
<body>
<form action="check_install.php" method="POST">
<table>
<tr><td align="right"><font color="Red">*</font> 輸入資料庫伺服器地址:</td><td><input name="llcc_server" type="text" value="localhost"></td></tr>
<tr><td align="right"><font color="Red">*</font> 輸入資料庫伺服器使用者名稱:</td><td><input name="llcc_user_name" type="text" value=""></td></tr>
<tr><td align="right"><font color="Red">*</font> 輸入資料庫伺服器密碼:</td><td><input name="llcc_user_pass" type="text" value=""></td></tr>
<tr><td align="right"><font color="Red">*</font> 輸入資料庫名稱:</td><td><input name="llcc_data_name" type="text" value=""> </td></tr>
<tr><td><input type="submit" name="bt_install" value="提交"></td></tr>
</table>
</form>
</body>
</html>

check_install.php

 代碼如下 複製代碼

<?php
//擷取提交的資料庫值
$llcc_server=$_POST['llcc_server'];
$llcc_user_name=$_POST['llcc_user_name'];
$llcc_user_pass=$_POST['llcc_user_pass'];
$llcc_data_name=$_POST['llcc_data_name'];
//開啟資料庫設定檔
$fp=fopen("../config.php",'w');

if (!$fp) {
    echo "sorry,try again!";
}
//寫入檔案內容
$outconfig="<?phpn";
$outconfig.='$server=';
$outconfig.="".$llcc_server.";//資料庫所在IP,如果是本地,預設localhostn";
$outconfig.='$server_name=';
$outconfig.="".$llcc_user_name.";//串連資料庫使用者名稱,一般為rootn";
$outconfig.='$server_pass=';
$outconfig.="".$llcc_user_pass.";//串連資料庫密碼。n";
$outconfig.='$server_data=';
$outconfig.="".$llcc_data_name.";//資料庫名n";
$outconfig.="?>";
$fw=fwrite($fp,$outconfig);

if ($fw) {
    echo "<br><br><br><center>安裝完成!請手動刪除目錄下install檔案夾<br><a href='../'>返回首頁</a></center>";
}


?>

login.php

 代碼如下 複製代碼

<?php
include("header.php");
include("conn.php");

  if($_GET[out]){
   setcookie("cookie", "out");
    echo "<script language="javascript">location.href='login.php';</script>";
  }


  if($_POST[id]=='admin'){
    $pw=md5($_POST[pw]);
    if($pw=='1b3ae77a2e06580c8cb57e112ddc4a26'){
     setcookie("cookie", "ok");
       echo "<script language="javascript">location.href='login.php';</script>";
    }
  }
if($_COOKIE['cookie']!='ok'){
?>

<SCRIPT language=javascript>
function Checklogin()
{
 if (myform.id.value=="")
 {
  alert("請填寫登入名稱");
  myform.id.focus();
  return false;
 }
  if (myform.pw.value=="")
 {
  alert("密碼不可為空");
  myform.pw.focus();
  return false;
 }
}
</SCRIPT>
<table width="400">
<form action="" method="post" name="myform" onsubmit="return Checklogin();">
  <tr><th>登入名稱:</th><td><input type="text" name="id" /></td></tr>
  <tr><th>密碼:</th><td><input type="password" name="pw" /></td></tr>
  <tr><td colspan="2"><input type="submit" name="submit" value="登陸"/></td></tr>
  </form>
  </table>
<?
}else{
?>
 <a href='?out=login'>退出</a><form name="ff">
 <input type="submit" name="sub" value="清空資料庫"></form>
 <?php
 if($_GET[sub]=="清空資料庫")
 {
 $sql="delete from message";
 $query=mysql_query($sql);
 }
 ?>
<?php
}
?>

conn.php資料庫連接

 代碼如下 複製代碼
<?php
include("config.php");
$conn =@mysql_connect($server,$server_name,$server_pass) or die("資料庫連結錯誤");
mysql_select_db($server_data, $conn);//此處代表的要開啟的資料庫名稱,"bbs"是我的資料庫名,修改成自己的!
mysql_query("set names 'GBK'"); //使用GBK中文編碼;
?>

index.php檔案

 代碼如下 複製代碼

<?php
include("conn.php");
include("header.php");
?>
自由屋留言板<hr width="70%"><table width=750 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<?php
$sql="select * from message order by id DESC";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
?>
  <tr bgcolor="#eff3ff">
  <td><b><?php echo $row[id];?>使用者名稱:</b><?php echo $row[user];?>&nbsp;&nbsp;&nbsp;&nbsp;<b>標題:</b><?php echo $row[title];?>&nbsp;&nbsp;&nbsp;<b>發表時間:</b><?php echo $row[lastdate];?></td>
  </tr>
  <tr bgColor="#ffffff">
  <td><b>內容:<?php echo $row[content];?></b></td>
  </tr>
 <?php
  }
?>
</table>

add.php增加留方程式

 代碼如下 複製代碼

<?php
include("header.php");
include("conn.php");
if($_POST["submit"])
{
$sql="INSERT INTO message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo "留言已提交";
};
?><center>

<table border="0" width="450">
<form name="add" method="post" onsubmit="return CheckPost();">
<tr><th>使用者:</th><td><input type="text" name="user" size="10"></td></tr>
<tr><th>標題:</th><td><input type="text" name="title" size="30"></td></tr>
<tr><th>內容:</th><td><textarea name="content"></textarea></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="發表留言"> &nbsp;&nbsp;&nbsp;<input type="reset" value="重設"></td></tr>
</form>
</table>
</center>

<SCRIPT language=javascript>
function CheckPost()
{
 if (add.user.value=="")
 {
  alert("請填寫使用者名稱");
  add.user.focus();
  return false;
 }
 if (add.title.value.length<5)
 {
  alert("標題不能少於5個字元");
  add.title.focus();
  return false;
 }
 if (add.content.value=="")
 {
  alert("必須要填寫留言內容");
  add.content.focus();
  return false;
 }
}
</SCRIPT>

test.php資料庫結構

 代碼如下 複製代碼

<?php
include("config.php");
include("conn.php");
$sql="CREATE TABLE `message` (
  `id` tinyint(1) NOT NULL auto_increment,
  `user` varchar(25) NOT NULL,
  `title` varchar(50) NOT NULL,
  `content` tinytext NOT NULL,
  `lastdate` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;";
$query=mysql_query($sql);
if($query){
echo"執行成功";}
else echo"執行失敗!";
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.