<?php
session_start();
# ปรับปรุง 21 ตุลาคม 2562
# เผยแพร่ใน http://www.thaiall.com/source
# เริ่มต้นต้องสร้างตารางก่อน ดูที่ตัวแปร $create_table_sql
# ===
# ส่วนกำหนดค่าเริ่มต้นของระบบ
$host = "localhost";
$db = "test";
$tb = "test";
$user = "root"; // รหัสผู้ใช้ ให้สอบถามจากผู้ดูแลระบบ
$password = ""; // รหัสผ่าน ให้สอบถามจากผู้ดูแลระบบ
$create_table_sql = "create table test (id varchar(20), ns varchar(20), salary varchar(20))
engine = InnoDB default charset=utf8 collate=utf8_unicode_ci;";
if (isset($_REQUEST{'action'})) $act = $_REQUEST{'action'}; else $act = "";
# ===
# ส่วนแสดงผลหลัก ทั้งปกติ และหลังกดปุ่ม del หรือ edit
if (strlen($act) == 0 || $act == "del" || $act == "edit") {
$conn = new mysqli($host, $user, $password, $db);
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
$r = $conn->query("select * from test");
if(!$r) { die("no result<br/>".$create_table_sql); } // if($r->num_rows == 0) { die("no result"); }
echo "<table>";
while ($o = $r->fetch_object()) {
if (isset($_REQUEST{'id'}) && $_REQUEST{'id'} == $o->id) $chg = " style='background-color:#f9f9f9"; else $chg = " readonly style='background-color:#ffffdd";
echo "<tr><form action='' method=post>
<td><input name=id size=5 value='". $o->id . "' style='background-color:#dddddd' readonly></td>
<td><input name=ns size=40 value='". $o->ns . "' $chg'></td>
<td><input name=salary size=20 value='". $o->salary . "' $chg;text-align:right'></td>
<td>";
if (isset($_REQUEST{'id'}) && $_REQUEST{'id'} == $o->id) {
if ($act == "del") echo "<input type=submit name=action value='del : confirm' style='height:40;background-color:yellow'>";
if ($act == "edit") echo "<input type=submit name=action value='edit : confirm' style='height:40;background-color:#aaffaa'>";
} else {
echo "<input type=submit name=action value='del' style='height:26'> <input type=submit name=action value='edit' style='height:26'>";
}
echo "</td></form></tr>";
}
echo "<tr><form action='' method=post><td><input name=id size=5></td><td><input name=ns size=40></td><td><input name=salary size=20></td><td><input type=submit name=action value='add' style='height:26'></td></tr>
</form></table>";
if (isset($_SESSION["msg"])) echo "<br>".$_SESSION["msg"];
$_SESSION["msg"] = "";
exit;
}
# ===
# ส่วนเพิ่มข้อมูล
if ($act == "add") {
$q = "insert into test values('". $_REQUEST{'id'} . "','". $_REQUEST{'ns'} . "','". $_REQUEST{'salary'} . "')";
$conn = new mysqli($host, $user, $password, $db);
$r = $conn->query($q);
if ($r) $_SESSION["msg"] = "insert : completely";
$conn->close();
header("Location: ". $_SERVER["SCRIPT_NAME"]);
}
# ===
# ส่วนลบข้อมูล
if ($act == "del : confirm") {
$q = "delete from test where id ='". $_REQUEST{'id'} . "'";
$conn = new mysqli($host, $user, $password, $db);
$r = $conn->query($q);
if ($r) $_SESSION["msg"] = "delete : completely";
$conn->close();
header("Location: ". $_SERVER["SCRIPT_NAME"]);
}
# ===
# ส่วนแก้ไขข้อมูล
if ($act == "edit : confirm") {
$q = "update $tb set ns ='". $_REQUEST{'ns'} . "', salary ='". $_REQUEST{'salary'} . "' where id =" . $_REQUEST{'id'};
$conn = new mysqli($host, $user, $password, $db);
$r = $conn->query($q);
if ($r) $_SESSION["msg"] = "edit : completely";
$conn->close();
header("Location: ". $_SERVER["SCRIPT_NAME"]);
}
?>
จำนวน : 75 บรรทัด