<tr><td></td><td><input type=submit value="Edit it!"></td></tr></table>
x2 l0 m9 `9 ~, I, J1 N/ ]" M, c* N
<input type=hidden name=idx value="<?php echo "$idx";?>">
8 L9 K, L$ B3 D4 y* d, R- n7 Y( I, J" r, j
</form> 9 o/ w5 N4 [2 x3 a$ d+ i
) h9 {1 j# P/ ^- F7 V0 P* P; v<?php include ('links.x');?> ( Z3 z2 y7 V* m8 J! j& v
</body> 7 s a4 L6 Y. h4 y% d" k
</html>-------------------------------------------------------------------------------- + {4 @$ a: g& M/ _
好,這個腳本很簡單。我們要關心的是,當表單打印出來時,它記錄了當前記錄的數據,通過在 <input 1 o1 v6 ~, U4 J6 w" H6 c
type= > 命令中的value屬性。這些數據是從前一個頁面傳遞過來的。 : R' F' i( e1 m
" _% f7 K) q) M3 ]9 T8 M9 t: h 現在,如果我們不改變記錄的信息,它將傳回當前值,即缺省值。如果我們改變了字段的值,字段的值
% q% U* B ~; M' H$ e2 N將變成新的值。接著我們可以將新值傳給另一個腳本,它將會改變MySQL表中的值。 ( D5 I3 } ^ d5 Y
5 `# r7 q& Y) R. u, H, I9 N) |
--------------------------------------------------------------------------------
( w) h) O u9 [" q, u& _# n) s$ Seditdb.php3: - b: n. I/ T0 C6 m# U
8 E" I. R9 Y+ [+ A7 ]* l<?php
' f% Z& B. q" F4 I7 G2 U2 P# \$ Z$ \" y3 _' D& f% L7 A a
mysql_connect() or die ("Problem connecting to DataBase");
- {+ ]/ P1 T6 D1 S" Q3 y2 {) x" S, [7 o3 k. d$ t
$query = "update tbl set - P( X5 d, n2 @2 ]
idx='$idx',UserName='$UserName',LastName='$LastName',FreeText='$FreeText' where
( `, R) j' p" h, V9 M; vidx='$idx'";
3 q6 ?7 Q, g+ }1 G r; S- u6 I( `$ j3 M* v$ ~8 X) f/ Z& S
$result = mysql_db_query("example", $query); ! w1 v( Z! ~% z) Z$ c9 t
$query = "SELECT * FROM tbl"; $ Q" S s! a3 N6 Y/ t( c
$result = mysql_db_query("example", $query); , H; B3 t1 b- g. ^$ C
7 H$ v, Z1 {! C+ z( l, }
if ($result)
$ U: y m; L/ c8 i3 a{ % ^. Z, j9 i8 Q, _7 B* U& b N
echo "Found these entries in the database:<br><p></p>";
/ i- o0 h% a* T2 Techo "<table width=90% align=center border=1><tr> & t' m, n, h r ]
<td align=center bgcolor=#00FFFF>idx</td> * B! d( e1 Z: U# ^ [: G
<td align=center bgcolor=#00FFFF>User Name</td>
' q+ j2 [* B6 g* t<td align=center bgcolor=#00FFFF>Last Name</td> 4 m( D7 @' A9 q1 i7 r- G$ G
<td align=center bgcolor=#00FFFF>Free Text</td> ) v: \; M( Q* v* b, O
</tr>";
4 m* d8 y6 N8 I$ I8 u( d q
$ t; b! r; Y( g* F9 H/ |while ($r = mysql_fetch_array($result)) & Y* w# L, W+ R1 f Y5 |
{ " v$ A0 ?" q& t- q
$idx = $r["idx"];
# v' a- X+ E3 V. V9 b5 ~( l& X) t4 `5 x$user = $r["UserName"];
8 B$ [2 E! i( R7 }1 L2 k$last = $r["LastName"]; & ?. r, t( {4 i
$text = $r["FreeText"];
- J1 @8 x( J, ^) v- \: B) L V& f# ~, R
echo "<tr> + e4 B( t+ n# B; `; ]$ R
<td>$idx</td>
$ i3 c$ Q1 p3 |/ j$ c& f<td>$user</td> 1 D2 X+ m f: u' B; r* i% \
<td>$last</td> $ n; p2 U+ ^1 F6 k8 E
<td>$text</td>
) f8 f Q: \# s1 X" f/ P+ G) |# @</tr>";
* D6 L. O+ [; a* R2 @/ k8 p# `+ }) D
} . V6 x2 m1 Y T1 Y" s- n
echo "</table>"; : F4 s8 s6 D9 `% `6 V
} ) Z4 [8 I% q" p
else
" v1 s& p" l( e9 T6 Z2 {4 {/ X{ # t6 \9 G+ H: z6 @
echo "No data."; . d- ]4 T- h7 D- B3 q+ w5 D
}
: n1 V" ~; u2 e# y* \ [/ v' z: s& y& _, I4 V
mysql_free_result($result);
4 a/ t1 o5 P- \( R+ I5 G; }8 W7 h' |9 M8 G% g( ^! `
include ('links.x'); , y' u# O$ w+ }! _) d. P
1 W8 ]8 {4 Z- w0 N# \9 i
?>--------------------------------------------------------------------------------
! G( f2 U' Y5 i. z, L8 U 基本上要關心的一件事情是下面一行:
3 |5 j9 r8 T- D# Q) C% W# S9 G' L
$query = "update tbl set idx='$idx',UserName='$UserName',LastName='$LastName',FreeText='$FreeText' where idx='$idx'";
3 N; ^! I* _2 H! l/ A; q9 n0 `; @5 G6 J% K' q( A4 k
注意,它與我們在前面MySQL部分解釋的語法相同。另一件事,注意這個腳本改變的是idx=$idx的記錄, / G: X5 V, N2 O; L& z9 u# C
如果表中有多條idx等於$idx的記錄,所以這些記錄都將被改變。如果我們想更嚴格一些,我們可以像下面
3 W0 N2 l' M, R0 H" h改動一下where子句: % R, l! U0 K9 {. S1 ^& L
. g+ q7 q* k9 }' H$ `* z
$query = "update tbl set idx='$idx',UserName='$UserName', LastName='$LastName',FreeText='$FreeText' where idx='$idx' and UserName='$UserName' and LastName='$LastName' and FreeText='$FreeText'";
$ J; h5 V- C" `+ A" J+ P3 o5 X: K" u$ u$ o, k! z
這個語法將檢查所有的字段,而不僅僅是檢查idx。
- G% I# X2 H8 j& \2 P5 l' w) x& b5 _
從數據庫中刪除一條記錄:
/ D1 h* ~0 T9 f+ X8 K. t# ]5 [: w# p3 t; l
好,刪除很容易。我們仍然需要兩個腳本:一個用來選擇要刪除的記錄(基本上同上面選擇要編輯的記
3 ^: ]7 \+ u" i' Q錄一樣),一個用來真正地進行刪除和打印新的表格。 6 `# R m/ E1 h& ]) _ T* v0 O
# l! L% Y1 f) e) F
-------------------------------------------------------------------------------- # X6 V2 ]( I3 s% A$ o J
del.php3:
* W+ V- L6 K* ]# V( W
2 w# _% j. g Y' o& P<html>
! r8 R. c5 g2 g3 d5 Z; _1 [% [<head><title>Deleting an entry from the database</title> ' w/ E' G; X9 H5 F9 _) A
</head>
1 H% x2 O8 A! i) a! f, t" M; f! m- F b, K/ t1 C1 J2 s a
<body bgcolor=#ffffff> # b# Z5 E) |5 Q% X; G& o7 [! q1 a0 F* j
<h1>Del an entry</h1> 8 Q0 |- H# Q5 n/ _0 [: v0 K( _
2 W$ C0 {' q7 [2 y- g8 Y7 o0 n9 n$ R<? 0 P* i3 V g- K) u
0 T0 n; u: N1 Q- X& g
mysql_connect() or die ("Problem connecting to DataBase");
: ?/ t9 U8 Q& V ]$query = "select * from tbl";
5 B, D$ [0 m+ {4 B* i9 w1 e$result = mysql_db_query("example", $query);
L# T3 T* I O
0 a. ?# V0 y" U* Q4 Oif ($result)
- _( ?2 D. p* ~- v" H& e9 \{ 9 m; B% i7 @* A$ p1 c! R! O5 i1 U
echo "Found these entries in the database:<br><p></p>";
7 W y' r6 ^, n+ decho "<table width=90% align=center border=1><tr> 0 l: H: P6 w( w
<td align=center bgcolor=#00ffff>idx</td>
9 c2 J n1 |& w/ a' P! {7 y<td align=center bgcolor=#00FFFF>User Name</td> " \+ ~% \. }! e- C
<td align=center bgcolor=#00FFFF>Last Name</td>
9 N1 X' v! O0 I1 {% ]( z7 e! \<td align=center bgcolor=#00FFFF>Free Text</td> # }2 f" N" ]1 i
</tr>";
/ k9 |& u1 V. F2 X/ N/ o5 t4 v$ w2 n* L, C+ n0 I H
while ($r = mysql_fetch_array($result)) 1 T4 z8 F5 v @4 G& T8 C
{
0 h# _% ?5 q/ J7 I: g% r: `$idx = $r["idx"]; 0 T& b5 V4 r9 [, a
$user = $r["UserName"];
0 p. a: ]; ~- O: {! H% b$last = $r["LastName"];
) O0 O X6 f9 a. B& ~$text = $r["FreeText"]; 5 [: ~+ v0 t3 a, Y- l1 X
4 q" d4 g9 ~$ h6 S
echo "<tr>
P8 d: U/ s- m<td align=center>
8 o8 g0 Y% r* a7 u# {<a href=\"dele.php3? % g/ X& u7 ^: O
idx=$idx&UserName=$user&LastName=$last&FreeText=$text\">$idx</a></td> - \6 ~9 q! J# C$ ?& h" M& B& z
<td>$user</td> 4 t; }. i" c* U) X1 p# ]% b
<td>$last</td> 8 D+ Q$ Z6 m" C% j) X: Q# y
<td>$dtext</td> % P' K8 p. x; {! E; R6 p9 A- ~- m$ y* |* c
</tr>"; ' X$ R7 Z! A. d7 H
& T$ M+ u9 u' Q- J& [4 Y
} * B% z- I# l) d: y
echo "</table>"; 5 V3 [8 x3 R& D+ b2 Z$ J: g/ a
} $ G4 B" r1 b6 ~2 E
else : R9 {" H. p0 R5 T( D" b+ }( ?( q6 F
{
' }7 l8 O/ t; oecho "No data.";
5 I6 C; x& u" ^5 E8 c8 N) e5 S}
1 B3 m7 r% D. T2 X5 `- K* M d: M) N- G; d
mysql_free_result($result); O3 y5 A; K# Z' G
include ('links.x');
" O7 e+ L) X$ X1 o N* w5 J
, Q, _, _4 H3 m$ r4 Z& z/ z?> 6 C! |! o# w" w) [% w' d$ @
' W( c% G P7 E9 X `. @9 [' {+ T</body>
4 ^2 J1 H& J6 ^8 E3 g, x</html>
5 J; Q+ O% `4 ]0 v4 ?: z--------------------------------------------------------------------------------
4 a# B0 s5 E( {, D" e 這個腳本與我們用過的編輯腳本很像,所以可以看一下那裡的說明。
' g$ s" Q2 K( B: A0 q8 H7 L+ K4 ^3 N3 { |1 _" W
-------------------------------------------------------------------------------- 8 j3 P% F3 K# [2 n. [
dele.php3: & ~8 A. X2 G2 y' T: B* n0 S+ f
$ ?3 x- V: D$ r
<?php ( A6 X3 p1 C% p5 Y) H
; H: z) d# b7 f6 Y9 R
mysql_connect() or die ("Problem connecting to DataBase"); * [: P4 [0 H& p
$query = "delete from tbl where idx='$idx' and UserName='$UserName' and ; v+ V( o3 k7 r U; Z
LastName='$LastName' and FreeText='$FreeText'"; 5 t5 |- t! k" Z0 w0 w3 b) e# D }
+ a X7 q* O" Y; I" W: C4 I$result = mysql_db_query("example", $query);
& K$ S) v& A6 s1 `5 Z% p$query = "SELECT * FROM tbl"; 6 Q$ `5 V$ ^% k @. _
$result = mysql_db_query("example", $query);
( h; G, A% K6 R* d8 {4 o8 ^ ^4 C j: v5 x6 ?1 @9 H* {
if ($result)
- x8 z8 e7 w3 K: {5 o{
7 [1 v8 T2 C y! w3 mecho "Found these entries in the database:<br><p></p>"; R+ G/ ^; { h0 f; P% @4 ~8 @
echo "<table width=90% align=center border=1><tr>
m, {8 _' I K$ y+ Z6 Y+ l' X4 W<td align=center bgcolor=#00FFFF>idx</td> - b: f; e7 y" B# n% S! X
<td align=center bgcolor=#00FFFF>User Name</td>
* Q, X0 Y0 ]) A& d7 H( |( X. }<td align=center bgcolor=#00FFFF>Last Name</td> # p2 I0 ^/ N3 e# p7 ?+ \9 e
<td align=center bgcolor=#00FFFF>Free Text</td> 5 R# K. _- v; c! T! ~( y5 u; Z
</tr>";
9 R9 s" D8 G- }# m
( T$ n6 {: Q0 _6 Fwhile ($r = mysql_fetch_array($result))
7 a0 u, q4 Z' V& j{ * U8 ~& B! F) S1 _8 X
$idx = $r["idx"]; 4 l$ w z8 U$ n7 b$ y0 x! u& P8 H0 B
$user = $r["UserName"]; ) R/ |; d" f- g- K& A( Z" }
$last = $r["LastName"]; ) m$ T" ]5 `/ @5 [, N' _, V
$text = $r["FreeText"]; / {6 `! m3 {8 ?7 R
7 \8 n4 {( ^, Q6 Q! n9 |echo "<tr> ; h" |* Z4 ^: e3 O% l/ p& A
<td>$idx</td> ; d% w2 k4 h8 ~- }
<td>$user</td> 0 b( {+ s! ]! s5 p$ i
<td>$last</td> # O g& Z% R5 k! c7 U" O6 Q- r
<td>$text</td> ; k: a- P+ M- L
</tr>";
7 F3 N7 X# ~, E1 ~, U
% g- s; T' l( [ z" c+ X- p}
3 o L9 }& R a" Y( {7 S$ Iecho "</table>"; ! p; O# Z. l! v2 k( D4 X8 k
}
) ?: v F6 F+ E! s8 R1 c3 ~( H, Lelse 6 ?5 D. a& b5 P: l
{ ; I# V' N! {" Q) P
echo "No data.";
# H* K R8 d4 @6 U1 e Z}
# Q* e8 [- h$ ~. ^( W/ M4 S# `; i5 u% Z% w5 j
mysql_free_result($result); ( @: a4 R+ h6 S6 G5 }; Y' b: K
( b' ~5 L. x: F" b. N
include ('links.x'); 1 k3 l0 u5 h) P7 D7 r$ _# ^
$ B' s( G: V* @% `$ i; W
?>--------------------------------------------------------------------------------
3 [( j( {3 R/ T5 a2 S. U6 w/ c 這個腳本看上去很熟悉,唯一不同的就是刪除查詢的語法:
& A( ]. L1 W& `5 r
' K" H) V* ], Y& F5 C) l$query = "delete from tbl where idx='$idx' and UserName='$UserName' and LastName='$LastName' and FreeText='$FreeText'"; 2 K5 ~+ f: h; W$ ~0 n' @% @4 W+ Q
: }5 Z% \1 ^7 _ z. U1 `
這個查詢將會刪除所有與前面的腳本傳遞來的信息相配匹的記錄。容易吧。
7 X5 T, `5 b2 n( l* \1 u- ] f/ K( Z6 P% q! j! D* @- F
好了,這就是全部內容了。 |