(1) code : m4.php
<body bgcolor=lightblue> <?php function DumpTableForDel($table,$con,$bgcolor){ $sql= "select * from $table"; if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; } echo "<table bgcolor=$bgcolor border=3>"; echo "<tr>"; $fc= mysqli_num_fields($query) ; while ($field = mysqli_fetch_field($query)){ printf("<td>%s", $field->name); } echo "<td>DEL"; $rowc=0; while ($row = mysqli_fetch_array($query)){ echo "<tr>"; for ($i=0;$i<$fc;$i++){ echo "<td>" . $row[$i]; } echo "<td><a href='m4.php?id=" . $row[0] . "'>DEL</a>"; $rowc++; } echo "</table>"; echo "資料表 $table 共計有 $rowc 筆資料。<br><br>"; } $con = @mysqli_connect('localhost', 'root', 'w10023', 'selldb'); if (!$con) { echo "Error: " . mysqli_connect_error(); exit(); } $sql = 'set names utf8;'; if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; } dumpTableForDel("custs",$con,"yellow"); $id=$_GET['id']; if ($id<>NULL){ $sql="delete from custs where id=$id"; if( $query= mysqli_query($con, $sql) ){ echo " $sql OK!<br> "; } else { echo " $sql Failure!<br> "; } echo "刪掉 id=$id 的資料錄後,custs資料表內容如下<br>"; dumpTableForDel("custs",$con,"cyan"); } mysqli_close ($con); ?> </body>
(2)執行結果:
set names utf8; OK! select * from custs OK!
id |
name |
amount |
tel |
DEL |
10001 |
小草 |
1000 |
08-233375 |
DEL |
10002 |
天牛 |
3000 |
03-933375 |
DEL |
10003 |
地虎 |
5000 |
02-444375 |
DEL |
資料表 custs 共計有 3 筆資料。
delete from custs where id=10002 OK! 刪掉 id=10002 的資料錄後,custs資料表內容如下 select * from custs OK!
id |
name |
amount |
tel |
DEL |
10001 |
小草 |
1000 |
08-233375 |
DEL |
10003 |
地虎 |
5000 |
02-444375 |
DEL |
資料表 custs 共計有 2 筆資料。
|