湖濱散記部落格的樹心幽徑[login][主頁]
562:20200109自selldb資料庫的custs資料表選擇要刪除的資料錄來刪除的PHP程式(c3.php)

(1)c3.php程式碼:

<body bgcolor=green>
<?php
function exeSQL($con,$sql){
if( $query= mysqli_query($con, $sql) ) {echo " $sql 成功!<br> "; }
else { echo " $sql Failure!<br> "; }     return $query;
}

function dumpTableForDel($table,$con,$bgcolor){
    $sql= "select * from $table";
    $query=exeSQL($con,$sql);
    echo "<table bgcolor=$bgcolor border=7>";
    echo "<tr bgcolor=cyan>";
    $fc= mysqli_num_fields($query) ;
    while ($field = mysqli_fetch_field($query)){
        printf("<td bgcolor=pink>%s", $field->name);
        }
    echo "<td>DEL";
    $rowc=0;
    while ($row = mysqli_fetch_array($query)){
        echo "<tr bgcolor=purple>";
        for ($i=0;$i<$fc;$i++){
            echo  "<td bgcolor=gray>" . $row[$i];
        }
echo "<td><a href=c3.php?id=" . $row[0] .">DEL</a>";
        $rowc++;
}
echo "</table>";
echo "total $rowc records <br><br>";
}
$con = mysqli_connect('127.0.0.1', 'root', 'w??????', 'selldb');
if (!$con) { echo "Error"; exit(); }
$sql = 'set names utf8;';
$query= exeSQL($con,$sql);
dumpTableForDel("custs",$con,"yellow");
$id=$_GET['id'];
if ($id<>NULL){
    $sql="delete from custs where id=$id";
    $query= exeSQL($con,$sql);
    echo "刪掉 id=$id 的資料錄後,custs資料表內容如下<br>";
    dumpTableForDel("custs",$con,"cyan");
}

mysqli_close ($con);
?>
</body>


(2)執行結果:

set names utf8; 成功!
select * from custs 成功!

id name amount tel DEL
10001 小狗 156 08-233375 DEL
10002 小貓 256 08-233375 DEL
10001 小草 1000 08-233375 DEL
10002 天牛 3000 03-933375 DEL
20002 天牛 3000 03-933375 DEL
30002 天牛 3000 03-933375 DEL

total 6 records

delete from custs where id=10001 OK!
刪掉 id=10001 的資料錄後,custs資料表內容如下
select * from custs 成功!

id name amount tel DEL
10002 小貓 256 08-233375 DEL
10002 天牛 3000 03-933375 DEL
20002 天牛 3000 03-933375 DEL
30002 天牛 3000 03-933375 DEL

total 4 records

 


select id,article_id,topic,text from lt_articles_text where article_id =562; ok. update lt_articles set num_reads=num_reads +1 where id=562; ok.