樹心幽徑

« 20190112將Ubuntu1604的64位元Linux核心由4.13.0-45更新成linux-4.20.0成功 | Main | 20190115自selldb資料庫的custs資料表選擇要刪除的資料錄來刪除的PHP程式(a4.php) »

20190114列出selldb資料庫的cust資料表的所有資料錄並編修選定的資料錄(a5.php)
2019/01/14,16:08

(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'] 成功!

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType