Workshop สอนทำระบบเบิกจ่ายวัสดุอุปกรณ์ PHP8 PDO MySQL AdminLTE
- ตัวอย่างโค้ดสำหรับการเชื่อมต่อฐานข้อมูลกับ MySQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $host = 'localhost'; // ชื่อโฮสต์ของฐานข้อมูล $dbname = 'your_database'; // ชื่อฐานข้อมูล $username = 'your_username'; // ชื่อผู้ใช้ฐานข้อมูล $password = 'your_password'; // รหัสผ่านฐานข้อมูล try { $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // เปิดใช้งานโหมดแจ้งข้อผิดพลาด PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // ตั้งค่าให้ดึงข้อมูลเป็น associative array PDO::ATTR_EMULATE_PREPARES => false // ปิดการจำลอง Prepared Statements ]); echo "เชื่อมต่อฐานข้อมูลสำเร็จ!"; } catch (PDOException $e) { die("เกิดข้อผิดพลาดในการเชื่อมต่อฐานข้อมูล: " . $e->getMessage()); } ?> |
- คำอธิบายโค้ด
- ใช้
PDO
เพื่อเชื่อมต่อฐานข้อมูล MySQL - เปิดใช้งาน
PDO::ERRMODE_EXCEPTION
เพื่อตรวจจับข้อผิดพลาด - กำหนด
PDO::FETCH_ASSOC
ให้ดึงข้อมูลเป็น array แบบ key-value - ปิด
PDO::ATTR_EMULATE_PREPARES
เพื่อให้ใช้ Prepared Statements ได้อย่างปลอดภัย
- ใช้
- ตัวอย่างโค้ดสำหรับการสร้างฟอร์มสมัครสมาชิก register.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Register | Material</title> <!-- Google Font --> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback"> <!-- Font Awesome --> <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css"> <!-- icheck bootstrap --> <link rel="stylesheet" href="plugins/icheck-bootstrap/icheck-bootstrap.min.css"> <!-- Theme style --> <link rel="stylesheet" href="dist/css/adminlte.min.css"> <!-- toastr --> <link rel="stylesheet" href="plugins/toastr/toastr.min.css"> </head> <body class="hold-transition register-page"> <div class="register-box"> <div class="register-logo"> <a href="#"><b>Register</b> Account</a> </div> <div class="card"> <div class="card-body register-card-body"> <p class="login-box-msg">Register a new membership</p> <form action="register_save.php" method="post"> <div class="input-group mb-3"> <input type="text" class="form-control" placeholder="Username" name="username" required> <div class="input-group-append"> <div class="input-group-text"><span class="fas fa-user"></span></div> </div> </div> <div class="input-group mb-3"> <input type="password" class="form-control" placeholder="Password" name="password" required> <div class="input-group-append"> <div class="input-group-text"><span class="fas fa-lock"></span></div> </div> </div> <div class="input-group mb-3"> <input type="text" class="form-control" placeholder="Full Name" name="full_name" required> <div class="input-group-append"> <div class="input-group-text"><span class="fas fa-id-card"></span></div> </div> </div> <div class="input-group mb-3"> <input type="email" class="form-control" placeholder="Email" name="email"> <div class="input-group-append"> <div class="input-group-text"><span class="fas fa-envelope"></span></div> </div> </div> <div class="input-group mb-3"> <input type="text" class="form-control" placeholder="Phone" name="phone"> <div class="input-group-append"> <div class="input-group-text"><span class="fas fa-phone"></span></div> </div> </div> <div class="input-group mb-3"> <input type="number" class="form-control" placeholder="Department ID" name="department_id"> <div class="input-group-append"> <div class="input-group-text"><span class="fas fa-building"></span></div> </div> </div> <div class="input-group mb-3"> <select class="form-control" name="role" required> <option value="">Select Role</option> <option value="admin">Admin</option> <option value="user">User</option> </select> </div> <div class="input-group mb-3"> <select class="form-control" name="status" required> <option value="active">Active</option> <option value="inactive">Inactive</option> </select> </div> <input type="hidden" name="created_at" value="<?= date('Y-m-d H:i:s') ?>"> <div class="row"> <div class="col-12"> <button type="submit" class="btn btn-primary btn-block">Register</button> </div> </div> </form> <a href="index.php" class="text-center">I already have a membership</a> </div> </div> </div> <!-- jQuery --> <script src="plugins/jquery/jquery.min.js"></script> <!-- Bootstrap 4 --> <script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script> <!-- AdminLTE App --> <script src="dist/js/adminlte.min.js"></script> <!-- toastr App --> <script src="plugins/toastr/toastr.min.js"></script> </body> </html> |
- ตัวอย่างโค้ดสำหรับบันทึกข้อมูลลงฐานข้อมูล register_save.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php // echo"<pre>"; // print_r($_POST); // echo"</pre>"; // exit(); require_once 'condb.php'; //เชื่อมต่อฐานข้อมูล if ($_SERVER['REQUEST_METHOD'] === 'POST') { //ประกาศตัวแปร รับค่า POST $username = trim($_POST['username']); $password = $_POST['password']; $full_name = trim($_POST['full_name']); $email = $_POST['email'] ?? null; $phone = $_POST['phone'] ?? null; $department_id = $_POST['department_id'] ?? null; $role = $_POST['role']; $status = $_POST['status']; // Hash the password $hashedPassword = password_hash($password, PASSWORD_DEFAULT); // Prepare SQL with named parameters $sql = "INSERT INTO users (username, password, full_name, email, phone, department_id, role, status, created_at) VALUES (:username, :password, :full_name, :email, :phone, :department_id, :role, :status, NOW())"; $stmt = $con->prepare($sql); // Bind parameters (bindParam or bindValue both are valid) $stmt->bindParam(':username', $username, PDO::PARAM_STR); $stmt->bindParam(':password', $hashedPassword, PDO::PARAM_STR); $stmt->bindParam(':full_name', $full_name, PDO::PARAM_STR); $stmt->bindParam(':email', $email, PDO::PARAM_STR); $stmt->bindParam(':phone', $phone, PDO::PARAM_STR); $stmt->bindParam(':department_id', $department_id, PDO::PARAM_INT); $stmt->bindParam(':role', $role, PDO::PARAM_STR); $stmt->bindParam(':status', $status, PDO::PARAM_STR); try { $stmt->execute(); header("Location: register.php?register=success"); exit; } catch (PDOException $e) { $error = urlencode($e->getMessage()); header("Location: register.php?error=$error"); exit; } } ?> |
- ตัวอย่างโค้ดแสดง toastr notifications
1 2 3 4 5 6 7 8 9 |
<script> $(function() { <?php if (isset($_GET['register']) && $_GET['register'] === 'success'): ?> toastr.success('สมัครสมาชิกสำเร็จ', 'สำเร็จ'); <?php elseif (isset($_GET['error'])): ?> toastr.error("<?php echo addslashes($_GET['error']); ?>", 'เกิดข้อผิดพลาด'); <?php endif; ?> }); </script> |
ShoppingCart | ระบบร้านค้าออนไลน์ E-commerce
ราคา 2,999 บาท ได้ Code & Database ทั้งหมด เอาไปต่อยอดได้
demo: https://devtai.com/ShoppingCart ขอบเขต: https://devtai.com/?p=1529
พัฒนาจาก PHP PDO MySQL, jQuery AJAX, AdminLTE รองรับการทำงานบน PHP8 ล่าสุด
สอนทำระบบ POS Laravel + PostgreSQL & AdminLTE3

สนใจจ้างพัฒนาระบบ เขียนระบบ
ทำเว็บไซต์ รับสอนเขียนโปรแกรม
inbox มาที่เพจ หรือติดต่อ 098-373-8651
ช่องทางการชำระเงิน
เงินสด หรือ e-banking
ธนาคารกสิกรไทย
เลขบัญชี : 0951168564
ชื่อบัญชี : นายวัยวุฒิ ชุมเมืองปัก