這篇文章主要為大家詳細介紹了php資料庫的增刪改查,以及php與javascript之間的互動,具有一定的參考價值,感興趣的小夥伴們可以參考一下
使用語言操作資料庫是重中之重,如果一門語言你不懂得如何操作資料庫,那麼你還是沒有學會這門語言。
PHP操作資料庫的方法並不難
同時php的值還可以與JavaScript指令碼之間進行控制
一般是php的值傳遞到javascript中,一般不會反過來操作
一、基本目標
首先,在mysql中有一張使用者資訊表user,裡面的欄位分別是id,username與password,開啟網頁dbselect.php,首先就用php查出整張user表:
然後,插入資料的一欄,輸入資料,就可把資料插入到mysql中的user表當中
在修改資料的一欄中,第一個下拉式功能表是通過javascript來建立的,根據表中的資料多少,而給予多少的下拉選項。
第二個下拉式功能表讓使用者選擇要修改的列
第三個輸入框就是讓使用者輸入要修改的值
至於為什麼沒有做刪除資料,那是因為一來刪除資料的操作與修改資料類似,二是因為在自增表中一般不刪除資料的,僅僅是設定索引值讓這條資料隱藏
二、基本思想
程式入口是dbselect.php,操作資料庫的過程分別是兩個新頁面,一個dbinsert.php,一個是dbupdate.php,這兩個頁面操作完資料庫,馬上通過javascript返回。
三、製作過程
(1)dbselect.php
也是本實現過程中,最複雜的一個頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>dbselect</title> </head> <body> user表: <table border="1"> <tr> <td>id</td> <td>username</td> <td>password</td> </tr> <?php //php串連資料庫的指定動作,其中第一個root是資料庫的使用者名稱,第二個root是資料庫的密碼 //如果串連失敗,馬上通過die語句打斷後面的所有程式,只輸出“串連失敗” $con=mysql_connect("localhost","root","root"); if(!$con){ die("串連失敗!"); } //要操作test資料庫 mysql_select_db("test",$con); //total變數是用來記錄user記錄條數的 $total; //要在test資料庫中操作select count(*) as total from user語句並且把結果放到result變數裡 $result=mysql_query("select count(*) as total from user"); //result變數是個資料,$total=$row["total"];把查詢結果中的total列的值賦予給php中的total變數 //$row=mysql_fetch_array($result)能夠把當前行的值賦予給row數組,並把遊標下移一行,遊標並不需要初始化,自動完成 while($row=mysql_fetch_array($result)){ $total=$row["total"]; } //輸出整個表的過程與上面的過程類此 $result=mysql_query("select * from user"); while($row=mysql_fetch_array($result)){ echo "<tr>"; echo "<td>${row["id"]}</td>"; echo "<td>${row["username"]}</td>"; echo "<td>${row["password"]}</td>"; echo "</tr>"; } //查詢完畢,記得人走帶門 mysql_close($con); ?> </table> <br /> <!--以下是兩個表單,不再贅述了--> 插入資料: <form action="dbinsert.php" method="get"> username:<input type="text" name="username" /> password:<input type="text" name="password" /> <input type="submit" value="go!" /> </form> 修改資料: <form action="dbupdate.php" method="get"> <select id="userid" name="userid"></select> <script> //這是php與javascript互動部分,把上面求出來的php的$total變數,賦予給javascript的var total var total=<?php echo $total; ?>; var i=1; for(i=1;i<total+1;i++){ //javascript增加節點過程 var selectnode=document.createElement("option"); selectnode.value=i; selectnode.innerHTML=i; document.getElementById("userid").appendChild(selectnode); } </script> <select name="rowname"> <option value="username">username</option> <option value="password">password</option> </select> <input type="text" name="rowtext" /> <input type="submit" value="go!" /> </form> </body> </html>
javascript控制html節點的詳細,可以參照我之前寫的《【JavaScript】網頁節點的增刪改查》一文(點擊開啟連結)
(2)dbinsert.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>dbinsert.php</title> </head> <body> <?php //首先從dbselect.php的表單中接受操作的資料 //dbselect.php故意用到get方法,只是想說明php中對get與post的處理同樣可以通過$_REQUEST["變數名"]來實現 $username=$_REQUEST["username"]; $password=$_REQUEST["password"]; //操作資料庫的指定動作同dbselect.php。 $con=mysql_connect("localhost","root","root"); if(!$con){ die("串連失敗!"); } mysql_select_db("test",$con); //控制資料庫比dbselect.php更加簡單,因為不用對資料庫的查詢結果進行處理 //只是要注意,這裡連接字串是用到.的,而不是jsp的+,asp的&,請注意! mysql_query("insert into user(username,password) values ('".$username."','".$password."');"); mysql_close($con); ?> <script> alert("添加成功"); window.location.href="dbselect.php" rel="external nofollow" rel="external nofollow" ; </script> </body> </html>
(3)dbupdate.php
與dbinsert.php邏輯是一模一樣的,只是mysql_query那個的查詢語句,從insert into語句變成了update語句而已
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <?php $userid=$_REQUEST["userid"]; $rowname=$_REQUEST["rowname"]; $rowtext=$_REQUEST["rowtext"]; $con=mysql_connect("localhost","root","root"); if(!$con){ die("串連失敗!"); } mysql_select_db("test",$con); mysql_query("update user set ".$rowname."='".$rowtext."' where id=".$userid.";"); mysql_close($con); ?> <script> alert("修改成功"); window.location.href="dbselect.php" rel="external nofollow" rel="external nofollow" ; </script> </body> </html>
以上,就是整個製作過程。