正在阅读:初学入门PHP和MySQL初学入门PHP和MySQL

2004-02-14 09:33 出处:PConline 作者:limodou 责任编辑:pjl
现在,如果我们不改变记录的信息,它将传回当前值,即缺省值。如果我们改变了字段的值,字段的值 将变成新的值。接着我们可以将新值传给另一个脚本,它将会改变MySQL表中的值。 ------------------------------------------------------------- editdb.php3: <?php mysql_connect() or die ("Problem connecting to DataBase"); $query = "update tbl set idx='$idx',UserName='$UserName',LastName='$LastName',FreeText='$FreeText' where idx='$idx'"; $result = mysql_db_query("example", $query); $query = "SELECT * FROM tbl"; $result = mysql_db_query("example", $query); if ($result) { echo "Found these entries in the database:<br><p></p>"; echo "<table width=90% align=center border=1><tr> <td align=center bgcolor=#00FFFF>idx</td> <td align=center bgcolor=#00FFFF>User Name</td> <td align=center bgcolor=#00FFFF>Last Name</td> <td align=center bgcolor=#00FFFF>Free Text</td> </tr>"; while ($r = mysql_fetch_array($result)) { $idx = $r["idx"]; $user = $r["UserName"]; $last = $r["LastName"]; $text = $r["FreeText"]; echo "<tr> <td>$idx</td> <td>$user</td> <td>$last</td> <td>$text</td> </tr>"; } echo "</table>"; } else { echo "No data."; } mysql_free_result($result); include ('links.x'); ?> -----------------------------------------------------------   基本上要关心的一件事情是下面一行: $query = "update tbl set idx='$idx',UserName='$UserName',LastName='$LastName',FreeText='$FreeText' where idx='$idx'";   注意,它与我们在前面MySQL部分解释的语法相同。另一件事,注意这个脚本改变的是idx=$idx的记录, 如果表中有多条idx等于$idx的记录,所以这些记录都将被改变。如果我们想更严格一些,我们可以象下面 改动一下where子句: $query = "update tbl set idx='$idx',UserName='$UserName', LastName='$LastName',FreeText='$FreeText' where idx='$idx' and UserName='$UserName' and LastName='$LastName' and FreeText='$FreeText'";   这个语法将检查所有的字段,而不仅仅是检查idx。   从数据库中删除一条记录:   好,删除很容易。我们仍然需要两个脚本:一个用来选择要删除的记录(基本上同上面选择要编辑的记 录一样),一个用来真正地进行删除和打印新的表格。 -------------------------------------------------------------- del.php3: <html> <head><title>Deleting an entry from the database</title> </head> <body bgcolor=#ffffff> <h1>Del an entry</h1> <? mysql_connect() or die ("Problem connecting to DataBase"); $query = "select * from tbl"; $result = mysql_db_query("example", $query); if ($result) { echo "Found these entries in the database:<br><p></p>"; echo "<table width=90% align=center border=1><tr> <td align=center bgcolor=#00ffff>idx</td> <td align=center bgcolor=#00FFFF>User Name</td> <td align=center bgcolor=#00FFFF>Last Name</td> <td align=center bgcolor=#00FFFF>Free Text</td> </tr>"; while ($r = mysql_fetch_array($result)) { $idx = $r["idx"]; $user = $r["UserName"]; $last = $r["LastName"]; $text = $r["FreeText"]; echo "<tr> <td align=center> <a href=\"dele.php3? idx=$idx&UserName=$user&LastName=$last&FreeText=$text\">$idx</a></td> <td>$user</td> <td>$last</td> <td>$dtext</td> </tr>"; } echo "</table>"; } else { echo "No data."; } mysql_free_result($result); include ('links.x'); ?> </body> </html> -------------------------------------------------------------   这个脚本与我们用过的编辑脚本很象,所以可以看一下那里的说明。 -------------------------------------------------------------- dele.php3: <?php mysql_connect() or die ("Problem connecting to DataBase"); $query = "delete from tbl where idx='$idx' and UserName='$UserName' and LastName='$LastName' and FreeText='$FreeText'"; $result = mysql_db_query("example", $query); $query = "SELECT * FROM tbl"; $result = mysql_db_query("example", $query); if ($result) { echo "Found these entries in the database:<br><p></p>"; echo "<table width=90% align=center border=1><tr> <td align=center bgcolor=#00FFFF>idx</td> <td align=center bgcolor=#00FFFF>User Name</td> <td align=center bgcolor=#00FFFF>Last Name</td> <td align=center bgcolor=#00FFFF>Free Text</td> </tr>"; while ($r = mysql_fetch_array($result)) { $idx = $r["idx"]; $user = $r["UserName"]; $last = $r["LastName"]; $text = $r["FreeText"]; echo "<tr> <td>$idx</td> <td>$user</td> <td>$last</td> <td>$text</td> </tr>"; } echo "</table>"; } else { echo "No data."; } mysql_free_result($result); include ('links.x'); ?> ---------------------------------------------------------------   这个脚本看上去很熟悉,唯一不同的就是删除查询的语法: $query = "delete from tbl where idx='$idx' and UserName='$UserName' and LastName='$LastName' and FreeText='$FreeText'";   这个查询将会删除所有与前面的脚本传递来的信息相配匹的记录。容易吧。   好了,这就是全部内容了。
键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品