本程序僅供php+mysql愛好者學(xué)習(xí)測試用,測試過程中,可能會出現(xiàn)錯誤,請朋友們根據(jù)錯誤提示修改本程序,本程序?yàn)榘惭b版,先運(yùn)行install目錄,然后根據(jù)提示操作.
關(guān)于PHP語法的那些就不說了,我就說一下連接mysql吧,代碼如下:
- $conn =@mysql_connect("localhost","root","mydown") or die("數(shù)據(jù)庫鏈接錯誤");
- mysql_select_db("bbs", $conn);
- 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)行,代碼如下:
- $sql="INSERT INTO message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
- mysql_query($sql);
上面中的藍(lán)色字體大家應(yīng)該面熟,意思就是 在數(shù)據(jù)庫中插入信息,第二句是在數(shù)據(jù)庫中運(yùn)行該語句,想起中類似$_POST[title]的變量,大家應(yīng)該知道,這是通過網(wǎng)頁提交獲取的信息.
PHP實(shí)例,install安裝,代碼如下:
- <html>
- <head>
- <title>留言板安裝</title>
- </head>
- <body>
- <form action="check_install.php" method="POST">
- <table>
- <tr><td align="right"><font color="Red">*</font> 輸入數(shù)據(jù)庫服務(wù)器地址:</td><td><input name="llcc_server" type="text" value="localhost"></td></tr>
- <tr><td align="right"><font color="Red">*</font> 輸入數(shù)據(jù)庫服務(wù)器用戶名:</td><td><input name="llcc_user_name" type="text" value=""></td></tr>
- <tr><td align="right"><font color="Red">*</font> 輸入數(shù)據(jù)庫服務(wù)器密碼:</td><td><input name="llcc_user_pass" type="text" value=""></td></tr>
- <tr><td align="right"><font color="Red">*</font> 輸入數(shù)據(jù)庫名稱:</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
- //獲取提交的數(shù)據(jù)庫值
- $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'];
- //打開數(shù)據(jù)庫配置文件
- $fp=fopen("../config.php",'w');
- if (!$fp) {
- echo "sorry,try again!";
- }
- //寫入文件內(nèi)容
- $outconfig="<?phpn";
- $outconfig.='$server=';
- $outconfig.="".$llcc_server.";//數(shù)據(jù)庫所在IP,如果是本地,默認(rèn)localhostn";
- $outconfig.='$server_name=';
- $outconfig.="".$llcc_user_name.";//連接數(shù)據(jù)庫用戶名,一般為rootn";
- $outconfig.='$server_pass=';
- $outconfig.="".$llcc_user_pass.";//連接數(shù)據(jù)庫密碼。n";
- $outconfig.='$server_data=';
- $outconfig.="".$llcc_data_name.";//數(shù)據(jù)庫名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("請?zhí)顚懙卿浢?quot;);
- 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="清空數(shù)據(jù)庫"></form>
- <?php
- if($_GET[sub]=="清空數(shù)據(jù)庫")
- {
- $sql="delete from message";
- $query=mysql_query($sql);
- }
- ?>
- <?php
- }
- ?>
conn.php數(shù)據(jù)庫連接,代碼如下:
- <?php
- include("config.php");
- $conn =@mysql_connect($server,$server_name,$server_pass) or die("數(shù)據(jù)庫鏈接錯誤");
- mysql_select_db($server_data, $conn);//此處代表的要打開的數(shù)據(jù)庫名稱,"bbs"是我的數(shù)據(jù)庫名,修改成自己的!
- 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];?> <b>標(biāo)題:</b><?php echo $row[title];?> <b>發(fā)表時間:</b><?php echo $row[lastdate];?></td>
- </tr>
- <tr bgColor="#ffffff">
- <td><b>內(nèi)容:<?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>標(biāo)題:</th><td><input type="text" name="title" size="30"></td></tr>
- <tr><th>內(nèi)容:</th><td><textarea name="content"></textarea></td></tr>
- <tr><td colspan="2" align="center"><input type="submit" name="submit" value="發(fā)表留言"> <input type="reset" value="重設(shè)"></td></tr>
- </form>
- </table>
- </center>
- <SCRIPT language=javascript>
- function CheckPost()
- {
- if (add.user.value=="")
- {
- alert("請?zhí)顚懹脩裘?quot;);
- add.user.focus();
- return false;
- }
- if (add.title.value.length<5)
- {
- alert("標(biāo)題不能少于5個字符");
- add.title.focus();
- return false;
- }
- if (add.content.value=="")
- {
- alert("必須要填寫留言內(nèi)容");
- add.content.focus();
- return false;
- }
- }
- </SCRIPT>
test.php數(shù)據(jù)庫結(jié)構(gòu),代碼如下:
- <?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"執(zhí)行成功";}
- else echo"執(zhí)行失敗!";
- ?>
新聞熱點(diǎn)
疑難解答