php數(shù)據(jù)庫(kù)的增刪改查 php與javascript之間的交互
使用語(yǔ)言操作數(shù)據(jù)庫(kù)是重中之重,如果一門語(yǔ)言你不懂得如何操作數(shù)據(jù)庫(kù),那么你還是沒(méi)有學(xué)會(huì)這門語(yǔ)言。
PHP操作數(shù)據(jù)庫(kù)的方法并不難
同時(shí)php的值還可以與JavaScript腳本之間進(jìn)行控制
一般是php的值傳遞到j(luò)avascript中,一般不會(huì)反過(guò)來(lái)操作
一、基本目標(biāo)
首先,在mysql中有一張用戶信息表user,里面的字段分別是id,username與password,打開(kāi)網(wǎng)頁(yè)dbselect.php,首先就用php查出整張user表:

然后,插入數(shù)據(jù)的一欄,輸入數(shù)據(jù),就可把數(shù)據(jù)插入到mysql中的user表當(dāng)中

在修改數(shù)據(jù)的一欄中,第一個(gè)下拉菜單是通過(guò)javascript來(lái)創(chuàng)建的,根據(jù)表中的數(shù)據(jù)多少,而給予多少的下拉選項(xiàng)。

第二個(gè)下拉菜單讓用戶選擇要修改的列
第三個(gè)輸入框就是讓用戶輸入要修改的值

至于為什么沒(méi)有做刪除數(shù)據(jù),那是因?yàn)橐粊?lái)刪除數(shù)據(jù)的操作與修改數(shù)據(jù)類似,二是因?yàn)樵谧栽霰碇幸话悴粍h除數(shù)據(jù)的,僅僅是設(shè)置鍵值讓這條數(shù)據(jù)隱藏
二、基本思想
程序入口是dbselect.php,操作數(shù)據(jù)庫(kù)的過(guò)程分別是兩個(gè)新頁(yè)面,一個(gè)dbinsert.php,一個(gè)是dbupdate.php,這兩個(gè)頁(yè)面操作完數(shù)據(jù)庫(kù),馬上通過(guò)javascript返回。

三、制作過(guò)程
(1)dbselect.php
也是本實(shí)現(xiàn)過(guò)程中,最復(fù)雜的一個(gè)頁(yè)面
<!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ù)庫(kù)的指定動(dòng)作,其中第一個(gè)root是數(shù)據(jù)庫(kù)的用戶名,第二個(gè)root是數(shù)據(jù)庫(kù)的密碼
//如果連接失敗,馬上通過(guò)die語(yǔ)句打斷后面的所有程序,只輸出“連接失敗”
$con=mysql_connect("localhost","root","root");
if(!$con){
die("連接失敗!");
}
//要操作test數(shù)據(jù)庫(kù)
mysql_select_db("test",$con);
//total變量是用來(lái)記錄user記錄條數(shù)的
$total;
//要在test數(shù)據(jù)庫(kù)中操作select count(*) as total from user語(yǔ)句并且把結(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è)表的過(guò)程與上面的過(guò)程類此
$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交互部分,把上面求出來(lái)的php的$total變量,賦予給javascript的var total
var total=<?php echo $total; ?>;
var i=1;
for(i=1;i<total+1;i++){
//javascript增加節(jié)點(diǎn)過(guò)程
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ì),可以參照我之前寫(xiě)的《【JavaScript】網(wǎng)頁(yè)節(jié)點(diǎn)的增刪改查》一文(點(diǎn)擊打開(kāi)鏈接)
(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方法,只是想說(shuō)明php中對(duì)get與post的處理同樣可以通過(guò)$_REQUEST["變量名"]來(lái)實(shí)現(xiàn)
$username=$_REQUEST["username"];
$password=$_REQUEST["password"];
//操作數(shù)據(jù)庫(kù)的指定動(dòng)作同dbselect.php。
$con=mysql_connect("localhost","root","root");
if(!$con){
die("連接失??!");
}
mysql_select_db("test",$con);
//控制數(shù)據(jù)庫(kù)比dbselect.php更加簡(jiǎn)單,因?yàn)椴挥脤?duì)數(shù)據(jù)庫(kù)的查詢結(jié)果進(jìn)行處理
//只是要注意,這里連接字符串是用到.的,而不是jsp的+,asp的&,請(qǐng)注意!
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那個(gè)的查詢語(yǔ)句,從insert into語(yǔ)句變成了update語(yǔ)句而已
<!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>無(wú)標(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>
以上,就是整個(gè)制作過(guò)程。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一個(gè)自定義位數(shù)的php多用戶計(jì)數(shù)器代碼
一個(gè)自定義位數(shù)的php多用戶計(jì)數(shù)器代碼...2007-03-03
PHP實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪查改功能及完整代碼
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪查改功能及完整代碼,需要的朋友可以參考下2018-04-04
CodeIgniter刪除和設(shè)置Cookie的方法
這篇文章主要介紹了CodeIgniter刪除和設(shè)置Cookie的方法,涉及CodeIgniter操作cookie的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
使用PHP+MySql+Ajax+jQuery實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)功能示例
下面小編就為大家?guī)?lái)一篇使用PHP+MySql+Ajax+jQuery實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)功能示例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能
這篇文章主要介紹了PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能,通過(guò)ajax實(shí)現(xiàn)主界面,php處理上傳文件,具體實(shí)例代碼大家跟隨腳本之家小編一起看看吧2018-06-06

