สำหรับเนื้อหาตอนนี้ เรามาดูในเรื่องของการเชื่อมต่อกับฐานข้อมูล และการใช้งาน
ในรูปแบบอย่างง่าย
จากตอนที่แล้ว เราได้สร้างรูปแบบโครงสร้างไฟล์ระบบสมาชิกผู้ดูแลระบบ
การกำหนด routing เพิ่มเติม เพื่อจัดการ URL ในระบบ admin
https://www.ninenik.com/content.php?arti_id=665 via @ninenik
ก่อนอื่นให้เราสร้างตาราง สำหรับระบบสมาชิก admin ดังโครงสร้างต่อไปนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 | CREATE TABLE IF NOT EXISTS `tbl_admin` ( `admin_id` int (3) unsigned NOT NULL , `admin_name` varchar (100) NOT NULL , `admin_pass` varchar (150) NOT NULL , `admin_adddate` datetime NOT NULL , `admin_lastlogin` datetime NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `tbl_admin` ADD PRIMARY KEY (`admin_id`); ALTER TABLE `tbl_admin` MODIFY `admin_id` int (3) unsigned NOT NULL AUTO_INCREMENT; |
การตั่งค่าการเชื่อมต่อฐานข้อมูล
ต่อไปให้เราไปที่ไฟล์ database.php ในโฟลเดอร์ apps > config
และให้แก้ไขการเชื่อมต่อฐานข้อมูล ตามต้องการ ดังนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | $active_group = 'default' ; $query_builder = TRUE; $db [ 'default' ] = array ( 'dsn' => '' , 'hostname' => 'localhost' , 'username' => 'root' , 'password' => '' , 'database' => 'test' , 'dbdriver' => 'mysqli' , 'dbprefix' => '' , 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production' ), 'cache_on' => FALSE, 'cachedir' => '' , 'char_set' => 'utf8' , 'dbcollat' => 'utf8_general_ci' , 'swap_pre' => '' , 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array (), 'save_queries' => TRUE ); |
การใช้งานของเราในที่นี้จะมีแค่การปรับแต่ง 4 จุด
1 2 3 4 | 'hostname' => 'localhost' , 'username' => 'root' , 'password' => '' , 'database' => 'test' , |
สำหรับ hostname ใครใช้งาน port อื่นที่ไม่ใช่ 3306 ก็กำหนด port เข้าไปด้วย
เช่น
1 | 'hostname' => 'localhost:3316' , |
ส่วนค่าอื่นๆ ก็ตามแต่ละเครืองกำหนด ในที่นี้ username เป็น root password เป้น ค่าว่าง
และก็ชื่อฐานข้อมูล database เป็น test
การโหลด การเชื่อมต่อฐานข้อมูลอัตโนมัติ
ตามรูปแบบ project ของเรามีการใช้งานฐานข้อมูล และ session เสมอ ดังนั้น
เราจะทำการโหลด library ทั้งสองในอัตโนมัติ โดยจะทำที่ไฟล์
autoload.php ในโฟลเดอร์ apps > config
โดยให้ปรับเพิ่มค่า library ที่ต้องการ ตามนี้
1 | $autoload [ 'libraries' ] = array ( 'database' , 'session' ); |
เมื่อกำหนด และบันทึกแล้ว เราก็สามารถเรีรยกใช้งาน การเชื่อมต่อฐานข้อมูล และ
ตัวแปร session ได้โดยไม่ต้องทำการโหลดการใช้งานด้วยคำสั่ง
1 | $this ->load->library( 'session' ); // เรียกใช้งาน session |
ดังนั้นในไฟล์ Admin,php เราสามารถ ปิดคำสั่งการโหลด session นี้ออกไปได้
1 2 3 4 5 6 7 8 9 10 11 12 | <?php defined( 'BASEPATH' ) OR exit ( 'No direct script access allowed' ); class Admin extends CI_Controller { public function __construct() { parent::__construct(); // $this->load->library('session'); // เรียกใช้งาน session } .......... ..... |
การแสดงรายการจากฐานข้อมูลด้วย Standard Query
เมื่อเราทำการเชื่อมต่อกับฐานข้อมูลและตั้งค่าต่างๆ เรียบร้อยแล้ว ต่อไปเราจะมาที่ไฟล์
admin_user.php ในโฟลเดอร์ apps > views > admin
ดูในส่วนของ $action เท่ากับ null หรือก็คือหน้าที่แสดงรายการชื่อในตาราง tbl_admin
มาแสดงในตาราง โดยรูปแบบ Standard Query ใช้งานแบบ array จะเป็นดังนี้
1 2 3 4 5 6 7 8 9 | <?php $query = $this ->db->query( "SELECT * FROM tbl_admin" ); foreach ( $query ->result_array() as $row ) { echo $row [ 'admin_id' ]. "<br>" ; echo $row [ 'admin_name' ]. "<br>" ; echo $row [ 'admin_adddate' ]. "<br>" ; } ?> |
ทีนี้เรามาแทรกเข้าไปในโครงสร้างตารางที่เราจัดรูปแบบการแสดงผลเอาไว้ จะได้เป้น
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 | <?php if ( $action ==null){?> <a href= "<?=base_url('admin/user/create')?>" class = "btn btn-primary btn-sm" >Create</a> <br><br> <table class = "table table-striped table-bordered table-condensed" > <thead> <tr> <th width= "50" class = "text-center" >#</th> <th>Name</th> <th width= "150" class = "text-center" >Last Login</th> <th width= "150" class = "text-center" >Manage</th> </tr> </thead> <tbody> <?php $i_num =0; $query = $this ->db->query( "SELECT * FROM tbl_admin" ); foreach ( $query ->result_array() as $row ){ $i_num ++; ?> <tr> <td class = "text-center" ><?= $i_num ?></td> <td><?= $row [ 'admin_name' ]?></td> <td class = "text-center" ><?= $row [ 'admin_adddate' ]?></td> <td class = "text-center" > <a href= "<?=base_url('admin/user/edit/'.$row['admin_id'])?>" class = "btn btn-success btn-sm" > <span class = "glyphicon glyphicon-pencil" aria-hidden= "true" ></span> </a> <a href= "<?=base_url('admin/user/delete/'.$row['admin_id'])?>" class = "btn btn-danger btn-sm" > <span class = "glyphicon glyphicon-remove" aria-hidden= "true" ></span> </a> </td> </tr> <?php } ?> </tbody> </table> <?php } ?> |
การเพิ่มข้อมูลลงฐานข้อมูลด้วย Standard Insert
ให้มาดูในส่วนของ $action เท่ากับ create หรือเงื่อนไขสำหรับการเพิ่มข้อมูลใหม่
รูปแบบการทำงานจะเป็นประมาณนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php if (isset( $_POST [ 'btn_add' ]) && $_POST [ 'btn_add' ]!= "" ){ $p_username = $this ->input->post( 'username' ); $p_password = $this ->input->post( 'password' ); $v_adddate = date ( "Y-m-d H:i:s" ); $sql = "INSERT INTO tbl_admin ( admin_id, admin_name, admin_pass, admin_adddate ) VALUES ( NULL, ".$this->db->escape($p_username)." , ".$this->db->escape($p_password)." , ".$this->db->escape($v_adddate)." )"; if ( $this ->db->query( $sql )){ // เมื่อเพิ่มข้อมูลแล้ว redirect( 'admin/user' ); // ไปหน้า user } } ?> |
ให้เราแทรกโค้ดดังกล่าวไปที่ส่วนของ $action เท่ากับ create ดังนี้
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 | <?php if ( $action == "create" ){?> <?php if (isset( $_POST [ 'btn_add' ]) && $_POST [ 'btn_add' ]!= "" ){ $p_username = $this ->input->post( 'username' ); $p_password = $this ->input->post( 'password' ); $v_adddate = date ( "Y-m-d H:i:s" ); $sql = "INSERT INTO tbl_admin ( admin_id, admin_name, admin_pass, admin_adddate ) VALUES ( NULL, ".$this->db->escape($p_username)." , ".$this->db->escape($p_password)." , ".$this->db->escape($v_adddate)." )"; if ( $this ->db->query( $sql )){ // เมื่อเพิ่มข้อมูลแล้ว redirect( 'admin/user' ); // ไปหน้า user } } ?> <a href= "<?=base_url('admin/user')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <form action= "<?=base_url('admin/user/create')?>" method= "post" > <table class = "table table-bordered" > <thead> <tr class = "active" > <th colspan= "2" >Create User</th> </tr> </thead> <tbody> <tr > <th width= "120" >Username:</th> <td> <input type= "text" name= "username" > </td> </tr> <tr> <th width= "120" >Password:</th> <td> <input type= "password" name= "password" > </td> </tr> <tr> <th></th> <td> <input type= "submit" class = "btn btn-success btn-sm" name= "btn_add" value= "Add User" > </td> </tr> </tbody> </table> </form> <?php } ?> |
การแก้ไขข้อมูลในฐานข้อมูลด้วย Standard Update
สำหรับการแก้ไขข้อมูล จะอยู่ในส่วน $action เท่ากับ edit ในส่วนนี้ เราจะมีทำงานอยู่ 2 อย่าง
คือ ดึงข้อมูลบางข้อมูลมาแสดงในฟอร์ม อ้างอิงจาก $id ของข้อมูลที่ต้องการ หลังจากนั้น
เมื่อทำการกดปุ่มเพื่อบันทึกข้อมูล ก็จะมีการใช้งานการอัพเดท แก้ไขข้อมูลในฐานข้อมูลด้วย
รูปแบบ standard update จะได้โค้ดประมาณนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php // เมื่อส่งข้อมูลฟอร์มเพื่อแก้ไขข้อมูล if (isset( $_POST [ 'btn_edit' ]) && $_POST [ 'btn_edit' ]!= "" ){ $p_username = $this ->input->post( 'username' ); $p_password = $this ->input->post( 'password' ); $sql = "UPDATE tbl_admin SET admin_name = ".$this->db->escape($p_username)." , admin_pass = ".$this->db->escape($p_password)." WHERE admin_id=". $this ->db->escape( $id ); if ( $this ->db->query( $sql )){ // เมื่อแก้ไขข้อมูลเรียบร้อยแล้ว redirect( 'admin/user' ); // ไปหน้า user } } // แสดงข้อมูลของสมาชิกนั้นๆ ก่อนแก้ไข อ้างอิงจากตัวแปร $id $query = $this ->db->query( "SELECT * FROM tbl_admin WHERE admin_id=" . $this ->db->escape( $id )); $row = $query ->row_array(); ?> |
ให้เราปรับแก้ในส่วนของ $action เท่ากับ edit ดังนี้
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 | <?php if ( $action == "edit" ){?> <?php // เมื่อส่งข้อมูลฟอร์มเพื่อแก้ไขข้อมูล if (isset( $_POST [ 'btn_edit' ]) && $_POST [ 'btn_edit' ]!= "" ){ $p_username = $this ->input->post( 'username' ); $p_password = $this ->input->post( 'password' ); $sql = "UPDATE tbl_admin SET admin_name = ".$this->db->escape($p_username)." , admin_pass = ".$this->db->escape($p_password)." WHERE admin_id=". $this ->db->escape( $id ); if ( $this ->db->query( $sql )){ // เมื่อเพิ่มข้อมูลแล้ว redirect( 'admin/user' ); // ไปหน้า user } } // แสดงข้อมูลของสมาชิกนั้นๆ ก่อนแก้ไข อ้างอิงจากตัวแปร $id $query = $this ->db->query( "SELECT * FROM tbl_admin WHERE admin_id=" . $this ->db->escape( $id )); $row = $query ->row_array(); ?> <a href= "<?=base_url('admin/user')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <form action= "<?=base_url('admin/user/edit/'.$id)?>" method= "post" > <table class = "table table-bordered" > <thead> <tr class = "active" > <th colspan= "2" >Edit User</th> </tr> </thead> <tbody> <tr > <th width= "120" >Username:</th> <td> <input type= "text" name= "username" value= "<?=$row['admin_name']?>" > </td> </tr> <tr> <th width= "120" >Password:</th> <td> <input type= "password" name= "password" value= "<?=$row['admin_pass']?>" > </td> </tr> <tr> <th></th> <td> <input type= "submit" class = "btn btn-success btn-sm" name= "btn_edit" value= "Edit User" > </td> </tr> </tbody> </table> </form> <?php } ?> |
การลบข้อมูลจากฐานข้อมูลด้วย Standard Delete
ส่วนสุดท้ายคือการลบข้อมูล อยู่ในเงื่อนไข $action เท่ากับ delete โค้ดจะเป็นประมาณนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php if ( $action == "delete" ){?> <a href= "<?=base_url('admin/user')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <?php if ( $id ){ $sql = "DELETE FROM tbl_admin WHERE admin_id='" . $this ->db->escape( $id ). "' " ; if ( $this ->db->query( $sql )){ } ?> <div class = "bg-success text-center" style= "padding:10px;" > <p class = "text-success" > Delete data completed</p> <a href= "<?=base_url('admin/user')?>" class = "text-success" >< Back > </a> </div> <?php } ?> <?php } ?> </div> |
ไฟล์ admin_user.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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | <div class = "container" > Admin User <br><br> <?php if ( $action ==null){?> <a href= "<?=base_url('admin/user/create')?>" class = "btn btn-primary btn-sm" >Create</a> <br><br> <table class = "table table-striped table-bordered table-condensed" > <thead> <tr> <th width= "50" class = "text-center" >#</th> <th>Name</th> <th width= "150" class = "text-center" >Last Login</th> <th width= "150" class = "text-center" >Manage</th> </tr> </thead> <tbody> <?php $i_num =0; $query = $this ->db->query( "SELECT * FROM tbl_admin" ); foreach ( $query ->result_array() as $row ){ $i_num ++; ?> <tr> <td class = "text-center" ><?= $i_num ?></td> <td><?= $row [ 'admin_name' ]?></td> <td class = "text-center" ><?= $row [ 'admin_adddate' ]?></td> <td class = "text-center" > <a href= "<?=base_url('admin/user/edit/'.$row['admin_id'])?>" class = "btn btn-success btn-sm" > <span class = "glyphicon glyphicon-pencil" aria-hidden= "true" ></span> </a> <a href= "<?=base_url('admin/user/delete/'.$row['admin_id'])?>" class = "btn btn-danger btn-sm" > <span class = "glyphicon glyphicon-remove" aria-hidden= "true" ></span> </a> </td> </tr> <?php } ?> </tbody> </table> <?php } ?> <?php if ( $action == "create" ){?> <?php if (isset( $_POST [ 'btn_add' ]) && $_POST [ 'btn_add' ]!= "" ){ $p_username = $this ->input->post( 'username' ); $p_password = $this ->input->post( 'password' ); $v_adddate = date ( "Y-m-d H:i:s" ); $sql = "INSERT INTO tbl_admin ( admin_id, admin_name, admin_pass, admin_adddate ) VALUES ( NULL, ".$this->db->escape($p_username)." , ".$this->db->escape($p_password)." , ".$this->db->escape($v_adddate)." )"; if ( $this ->db->query( $sql )){ // เมื่อเพิ่มข้อมูลแล้ว redirect( 'admin/user' ); // ไปหน้า user } } ?> <a href= "<?=base_url('admin/user')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <form action= "<?=base_url('admin/user/create')?>" method= "post" > <table class = "table table-bordered" > <thead> <tr class = "active" > <th colspan= "2" >Create User</th> </tr> </thead> <tbody> <tr > <th width= "120" >Username:</th> <td> <input type= "text" name= "username" > </td> </tr> <tr> <th width= "120" >Password:</th> <td> <input type= "password" name= "password" > </td> </tr> <tr> <th></th> <td> <input type= "submit" class = "btn btn-success btn-sm" name= "btn_add" value= "Add User" > </td> </tr> </tbody> </table> </form> <?php } ?> <?php if ( $action == "edit" ){?> <?php // เมื่อส่งข้อมูลฟอร์มเพื่อแก้ไขข้อมูล if (isset( $_POST [ 'btn_edit' ]) && $_POST [ 'btn_edit' ]!= "" ){ $p_username = $this ->input->post( 'username' ); $p_password = $this ->input->post( 'password' ); $sql = "UPDATE tbl_admin SET admin_name = ".$this->db->escape($p_username)." , admin_pass = ".$this->db->escape($p_password)." WHERE admin_id=". $this ->db->escape( $id ); if ( $this ->db->query( $sql )){ // เมื่อเพิ่มข้อมูลแล้ว redirect( 'admin/user' ); // ไปหน้า user } } // แสดงข้อมูลของสมาชิกนั้นๆ ก่อนแก้ไข อ้างอิงจากตัวแปร $id $query = $this ->db->query( "SELECT * FROM tbl_admin WHERE admin_id=" . $this ->db->escape( $id )); $row = $query ->row_array(); ?> <a href= "<?=base_url('admin/user')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <form action= "<?=base_url('admin/user/edit/'.$id)?>" method= "post" > <table class = "table table-bordered" > <thead> <tr class = "active" > <th colspan= "2" >Edit User</th> </tr> </thead> <tbody> <tr > <th width= "120" >Username:</th> <td> <input type= "text" name= "username" value= "<?=$row['admin_name']?>" > </td> </tr> <tr> <th width= "120" >Password:</th> <td> <input type= "password" name= "password" value= "<?=$row['admin_pass']?>" > </td> </tr> <tr> <th></th> <td> <input type= "submit" class = "btn btn-success btn-sm" name= "btn_edit" value= "Edit User" > </td> </tr> </tbody> </table> </form> <?php } ?> <?php if ( $action == "delete" ){?> <a href= "<?=base_url('admin/user')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <?php if ( $id ){ $sql = "DELETE FROM tbl_admin WHERE admin_id=" . $this ->db->escape( $id ); if ( $this ->db->query( $sql )){ } ?> <div class = "bg-success text-center" style= "padding:10px;" > <p class = "text-success" > Delete data completed</p> <a href= "<?=base_url('admin/user')?>" class = "text-success" >< Back > </a> </div> <?php } ?> <?php } ?> </div> |
การใช้งานฐานข้อมูล และการจัดการข้อมูล ทั้งการแสดง การเพิ่มข้อมูล การแก้ไขข้อมูล และ
การลบข้อมูล ด้วยรูปแบบ standard คร่าวๆ อย่างง่าย ก็จะเป็นไปประมาณนี้ เนื้อหาข้างต้นเป็นแนวทาง
สำหรับประยุกต์และศึกษาเพิ่มเติม
สำหรับการใช้งานคำสั่งหรือฟังก์ชั่นในรูปแบบของ codeigniter คงได้พูดถึงในลำดับต่อๆ ไป