相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說明
- thinkPHP5框架實(shí)現(xiàn)分頁查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
php數(shù)據(jù)庫的增刪改查 php與javascript之間的交互

使用語言操作數(shù)據(jù)庫是重中之重,如果一門語言你不懂得如何操作數(shù)據(jù)庫,那么你還是沒有學(xué)會(huì)這門語言。
PHP操作數(shù)據(jù)庫的方法并不難
同時(shí)php的值還可以與JavaScript腳本之間進(jìn)行控制
一般是php的值傳遞到j(luò)avascript中,一般不會(huì)反過來操作
一、基本目標(biāo)
首先,在mysql中有一張用戶信息表user,里面的字段分別是id,username與password,打開網(wǎng)頁dbselect.php,首先就用php查出整張user表:
然后,插入數(shù)據(jù)的一欄,輸入數(shù)據(jù),就可把數(shù)據(jù)插入到mysql中的user表當(dāng)中
在修改數(shù)據(jù)的一欄中,第一個(gè)下拉菜單是通過javascript來創(chuàng)建的,根據(jù)表中的數(shù)據(jù)多少,而給予多少的下拉選項(xiàng)。
第二個(gè)下拉菜單讓用戶選擇要修改的列
第三個(gè)輸入框就是讓用戶輸入要修改的值
至于為什么沒有做刪除數(shù)據(jù),那是因?yàn)橐粊韯h除數(shù)據(jù)的操作與修改數(shù)據(jù)類似,二是因?yàn)樵谧栽霰碇幸话悴粍h除數(shù)據(jù)的,僅僅是設(shè)置鍵值讓這條數(shù)據(jù)隱藏
二、基本思想
程序入口是dbselect.php,操作數(shù)據(jù)庫的過程分別是兩個(gè)新頁面,一個(gè)dbinsert.php,一個(gè)是dbupdate.php,這兩個(gè)頁面操作完數(shù)據(jù)庫,馬上通過javascript返回。
三、制作過程
(1)dbselect.php
也是本實(shí)現(xiàn)過程中,最復(fù)雜的一個(gè)頁面
<!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連接數(shù)據(jù)庫的指定動(dòng)作,其中第一個(gè)root是數(shù)據(jù)庫的用戶名,第二個(gè)root是數(shù)據(jù)庫的密碼 //如果連接失敗,馬上通過die語句打斷后面的所有程序,只輸出“連接失敗” $con=mysql_connect("localhost","root","root"); if(!$con){ die("連接失?。?); } //要操作test數(shù)據(jù)庫 mysql_select_db("test",$con); //total變量是用來記錄user記錄條數(shù)的 $total; //要在test數(shù)據(jù)庫中操作select count(*) as total from user語句并且把結(jié)果放到result變量里 $result=mysql_query("select count(*) as total from user"); //result變量是個(gè)數(shù)據(jù),$total=$row["total"];把查詢結(jié)果中的total列的值賦予給php中的total變量 //$row=mysql_fetch_array($result)能夠把當(dāng)前行的值賦予給row數(shù)組,并把游標(biāo)下移一行,游標(biāo)并不需要初始化,自動(dòng)完成 while($row=mysql_fetch_array($result)){ $total=$row["total"]; } //輸出整個(gè)表的過程與上面的過程類此 $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 /> <!--以下是兩個(gè)表單,不再贅述了--> 插入數(shù)據(jù): <form action="dbinsert.php" method="get"> username:<input type="text" name="username" /> password:<input type="text" name="password" /> <input type="submit" value="go!" /> </form> 修改數(shù)據(jù): <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增加節(jié)點(diǎn)過程 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>