湖濱散記部落格的樹心幽徑[login][主頁]
465:20190114列出selldb資料庫的cust資料表的所有資料錄並編修選定的資料錄(a5.php)

(1) code

<body bgcolor=lightgreen>

<?php
function executeSQL($con,$sql){   

if( $query= mysqli_query($con, $sql) ){ echo " [$sql] 成功!<br> "; }    

else { echo " [$sql] 失敗!<br> "; }    return $query;

}
function dumpRecordSetForEdit($sql,$con,$bgcolor){   

$query=executeSQL($con,$sql);   

echo "<table bgcolor=$bgcolor border=10>";   

echo "<tr bgcolor=yellow>";   

$fc= mysqli_num_fields($query) ;   

while ($field = mysqli_fetch_field($query)){

printf("<td>%s", $field->name);

}   

echo "<td bgcolor=cyan>EDIT";   

$rowc=0;   

while ($row = mysqli_fetch_array($query)){

        echo "<tr>";

        for ($i=0;$i<$fc;$i++){

            echo  "<td>" . $row[$i];

        }

        echo "<td bgcolor=white><a href='a5.php?id=" . $row[0] . "'>Edit</a>";

        $rowc++;    }

    echo "</table>";

    echo "<font color=red>共計有 $rowc 筆資料。<br><hr color=red></font>";

}

$con = @mysqli_connect('localhost', 'root', 'w????????????', 'selldb');

if (!$con) { echo "Error: " . mysqli_connect_error(); exit(); }

$sql = 'set names utf8';

$query=executeSQL($con,$sql);

$sql= "select id,name,amount,tel from custs";

dumpRecordSetForEdit($sql,$con,"orange");

$id=$_GET['id'];$uname="mo";

if ($id<>NULL){

    $sql="select id,name,amount,tel from custs where id='$id'";

    echo "待修的資料錄(id=$id)如下<br>"; 

   dumpRecordSetForEdit($sql,$con,"green");

    $query=executeSQL($con,$sql);

    $row = mysqli_fetch_array($query);

    $uname=$row[1]; $uamount=$row[2]; $utel=$row[3];

     echo "請修改下列待修資料並按確定來寫入編修:";

    echo "<form>";

    echo "id <input type=text name=uid size=4 value=$id>";

    echo "name <input type=text name=uname size=8 value=$uname >";

    echo "amount <input type=text size=6 name=uamount value=$uamount>";

    echo "tel <input type=text size=9 name=utel value=$utel>";

    echo  "<input type=submit value=Submit>";

    echo "</form><hr>";

}

$uid=$_GET['uid']; $uname=$_GET['uname'];

$uamount=$_GET['uamount']; $utel=$_GET['utel'];

if ($uname<>NULL){

    $sql= "update custs set name='$uname' ,amount=$uamount ,tel='$utel' where id='$uid'";

    $query=executeSQL($con,$sql);

}

mysqli_close ($con);

?>

</body>

(2) middle

[set names utf8] 成功!
[select id,name,amount,tel from custs] 成功!

id name amount tel EDIT
10001 小草 1000 08-233375 Edit
10002 天牛 3000 03-933375 Edit
20002 天牛 3000 03-933375 Edit
30002 天牛777 3000 03-933375 Edit

共計有 4 筆資料。


待修的資料錄(id=10001)如下
[select id,name,amount,tel from custs where id='10001'] 成功!

 

id name amount tel EDIT
10001 小草 1000 08-233375 Edit

共計有 1 筆資料。


[select id,name,amount,tel from custs where id='10001'] 成功!
請修改下列待修資料並按確定來寫入編修:

id name amount tel 
(3)final result

[set names utf8] 成功!

[select id,name,amount,tel from custs] 成功!

id name amount tel EDIT
10001 小草 1000 08-233375 Edit
10002 天牛 3000 03-933375 Edit
20002 天牛 3000 03-933375 Edit
30002 天牛777 3000 03-933375 Edit

共計有 4 筆資料。


[update custs set name='小草8888' ,amount=1000 ,tel='08-233375' where id='10001'] 成功!


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