php數(shù)據(jù)庫的增刪改查 php與javascript之間的交互
使用語言操作數(shù)據(jù)庫是重中之重,如果一門語言你不懂得如何操作數(shù)據(jù)庫,那么你還是沒有學(xué)會這門語言。
PHP操作數(shù)據(jù)庫的方法并不難
同時php的值還可以與JavaScript腳本之間進(jìn)行控制
一般是php的值傳遞到j(luò)avascript中,一般不會反過來操作
一、基本目標(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ù)的一欄中,第一個下拉菜單是通過javascript來創(chuàng)建的,根據(jù)表中的數(shù)據(jù)多少,而給予多少的下拉選項(xiàng)。
第二個下拉菜單讓用戶選擇要修改的列
第三個輸入框就是讓用戶輸入要修改的值
至于為什么沒有做刪除數(shù)據(jù),那是因?yàn)橐粊韯h除數(shù)據(jù)的操作與修改數(shù)據(jù)類似,二是因?yàn)樵谧栽霰碇幸话悴粍h除數(shù)據(jù)的,僅僅是設(shè)置鍵值讓這條數(shù)據(jù)隱藏
二、基本思想
程序入口是dbselect.php,操作數(shù)據(jù)庫的過程分別是兩個新頁面,一個dbinsert.php,一個是dbupdate.php,這兩個頁面操作完數(shù)據(jù)庫,馬上通過javascript返回。
三、制作過程
(1)dbselect.php
也是本實(shí)現(xiàn)過程中,最復(fù)雜的一個頁面
<!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ù)庫的指定動作,其中第一個root是數(shù)據(jù)庫的用戶名,第二個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變量是個數(shù)據(jù),$total=$row["total"];把查詢結(jié)果中的total列的值賦予給php中的total變量 //$row=mysql_fetch_array($result)能夠把當(dāng)前行的值賦予給row數(shù)組,并把游標(biāo)下移一行,游標(biāo)并不需要初始化,自動完成 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 /> <!--以下是兩個表單,不再贅述了--> 插入數(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>
javascript控制html節(jié)點(diǎn)的詳細(xì),可以參照我之前寫的《【JavaScript】網(wǎng)頁節(jié)點(diǎn)的增刪改查》一文(點(diǎn)擊打開鏈接)
(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的表單中接受操作的數(shù)據(jù) //dbselect.php故意用到get方法,只是想說明php中對get與post的處理同樣可以通過$_REQUEST["變量名"]來實(shí)現(xiàn) $username=$_REQUEST["username"]; $password=$_REQUEST["password"]; //操作數(shù)據(jù)庫的指定動作同dbselect.php。 $con=mysql_connect("localhost","root","root"); if(!$con){ die("連接失??!"); } mysql_select_db("test",$con); //控制數(shù)據(jù)庫比dbselect.php更加簡單,因?yàn)椴挥脤?shù)據(jù)庫的查詢結(jié)果進(jìn)行處理 //只是要注意,這里連接字符串是用到.的,而不是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>無標(biāo)題文檔</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>
以上,就是整個制作過程。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一個自定義位數(shù)的php多用戶計(jì)數(shù)器代碼
一個自定義位數(shù)的php多用戶計(jì)數(shù)器代碼...2007-03-03PHP實(shí)現(xiàn)數(shù)據(jù)庫的增刪查改功能及完整代碼
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)據(jù)庫的增刪查改功能及完整代碼,需要的朋友可以參考下2018-04-04CodeIgniter刪除和設(shè)置Cookie的方法
這篇文章主要介紹了CodeIgniter刪除和設(shè)置Cookie的方法,涉及CodeIgniter操作cookie的技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04使用PHP+MySql+Ajax+jQuery實(shí)現(xiàn)省市區(qū)三級聯(lián)動功能示例
下面小編就為大家?guī)硪黄褂肞HP+MySql+Ajax+jQuery實(shí)現(xiàn)省市區(qū)三級聯(lián)動功能示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動態(tài)顯示進(jìn)度功能
這篇文章主要介紹了PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動態(tài)顯示進(jìn)度功能,通過ajax實(shí)現(xiàn)主界面,php處理上傳文件,具體實(shí)例代碼大家跟隨腳本之家小編一起看看吧2018-06-06