(1)code m5.php
treehrt@treehrt-D630MT:~/phpsell$ cat m5.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 dumpRecordSetForEdit($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); } echo "<td>EDIT"; $rowc=0; while ($row = mysqli_fetch_array($query)){ echo "<tr>"; for ($i=0;$i<$fc;$i++){ echo "<td>" . $row[$i]; } echo "<td><a href='m5.php?id=" . $row[0] . "'>Edit</a>"; $rowc++; } echo "</table>"; echo "資料表 $table 共計有 $rowc 筆資料。<br><br>"; }
$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,"cyan"); $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,"cyan"); $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)執行結果
[set names utf8] OK! [select * from custs] OK!
id |
name |
amount |
tel |
EDIT |
10002 |
天牛9 |
3122 |
03-933375 |
Edit |
10003 |
地虎6 |
5000 |
02-444375 |
Edit |
資料表 共計有 2 筆資料。
待修的資料錄(id=10003)如下 [select * from custs where id='10003'] OK!
id |
name |
amount |
tel |
EDIT |
10003 |
地虎6 |
5000 |
02-444375 |
Edit |
資料表 共計有 1 筆資料。
[select * from custs where id='10003'] OK! 請修改下列待修資料並按確定來寫入編修:id name amount tel
|