樹心幽徑

« 20181227終於在ubuntu1604裝上了googleEarth並能開啟gpx航跡檔 | Main | 20190105將Fedora17的Linux核心由3.9.10-100.fc17 更新成linux-3.19.8 »

20181227輸入一筆客戶資料並新增到selldb資料庫的custs資料表中(m6.php)
2018/12/27,21:26

(1)code :m6.php
<body bgcolor=lightblue>
<?php
function executeSQL($con,$sql){
    if( $query= mysqli_query($con, $sql) ){ echo " [$sql] OK!<br> "; }
    else { echo " [$sql] Failure!<br> "; }
    return $query;
}

function dumpRecordSet($sql,$con,$bgcolor){
    $query=executeSQL($con,$sql);
    echo "<table bgcolor=$bgcolor border=3>";
    echo "<tr>";
    $fc= mysqli_num_fields($query) ;
    while ($field = mysqli_fetch_field($query)){ printf("<td>%s", $field->name); }
    $rowc=0;
    while ($row = mysqli_fetch_array($query)){
        echo "<tr>";
        for ($i=0;$i<$fc;$i++){
            echo  "<td>" . $row[$i];
        }
        $rowc++;
    }
    echo "</table>";
    echo "資料表 $table 共計有 $rowc 筆資料。<br><br>";
}

echo "請輸入要新增的顧客資料並按確定來寫入:";
echo "<form>";
echo "id <input type=text name=uid size=4 value='101' >";
echo "name <input type=text name=uname size=8 value='小虎' >";
echo "amount <input type=text size=6 name=uamount value='5000'>";
echo "tel <input type=text size=9 name=utel value='02-1234567'>";
echo  "<input type=submit value='確定'>";
echo "</form><hr>";
$uid=$_GET['uid']; $uname=$_GET['uname']; $uamount=$_GET['uamount']; $utel=$_GET['utel'];
if ($uid<>NULL){
    $con = @mysqli_connect('localhost', 'root', 'w?????', 'selldb');
    if (!$con) { echo "Error: " . mysqli_connect_error(); exit(); }
    $sql = 'set names utf8';
    executeSQL($con,$sql);
    $sql="insert into custs (id,name,amount,tel)  values ('$uid','$uname' ,$uamount,'$utel');";
    executeSQL($con,$sql);
    echo "如下資料錄已寫入selldb資料庫的custs資料表中。<br>";
    $sql= "select id,name,amount,tel from custs where id='$uid'";
    dumpRecordSet($sql,$con,'yellow');
    echo "selldb資料庫的custs資料表的全部資料錄如下:<br>";
    $sql= "select id,name,amount,tel from custs";
    dumpRecordSet($sql,$con,'cyan');
    echo "[<a href='m4.php'>刪除程式m4.php</a>] ";
    echo "[<a href='m5.php'>編修程式m5.php</a>] ";
    echo "[<a href='m6.php'>新增程式m6.php</a>] ";
    mysqli_close ($con);
}
?>
</body>

(2)執行結果:

id name amount tel


[set names utf8] OK!
[insert into custs (id,name,amount,tel) values ('101','小虎' ,5000,'02-1234567');] OK!
如下資料錄已寫入selldb資料庫的custs資料表中。
[select id,name,amount,tel from custs where id='101'] OK!

id name amount tel
101 小虎 5000 02-1234567

資料表 共計有 1 筆資料。

selldb資料庫的custs資料表的全部資料錄如下:
[select id,name,amount,tel from custs] OK!

id name amount tel
10001 大樹555 1000 08-2333756
10003 地虎8909 5000 02-444375
101d 小虎5556 5000 02-1234567
101 小虎 5000 02-1234567

資料表 共計有 4 筆資料。

[刪除程式m4.php] [編修程式m5.php] [新增程式m6.php]

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType