Làm cách nào để cập nhật dữ liệu CI?
Tôi có máy chủ ảo trong CMDB của mình và tôi cần cập nhật một số thông tin trong đó. Có cách nào để tôi có thể thực hiện Nhập từ excel thay vì tạo Cis mới sẽ tìm tên của máy chủ và thêm dữ liệu mới vào các trường cho chúng Show
Làm cách nào để cập nhật dữ liệu trong cơ sở dữ liệu bằng id trong codeigniter?. Sử dụng truy vấn cập nhật codeigniter sẽ cho phép bạn thực hiện thao tác cập nhật trên cơ sở dữ liệu. Ở bài trước mình đã hướng dẫn các bạn cách chèn dữ liệu vào cơ sở dữ liệu bằng codeigniter và bootstrap. Hướng dẫn này sẽ chỉ cho bạn cách cập nhật bản ghi cơ sở dữ liệu trong codeigniter Cập nhật Cơ sở dữ liệu CodeIgniter sẽ rất dễ dàng vì chúng tôi đã thực hiện mã hóa cơ sở bắt buộc trong hướng dẫn Chèn cơ sở dữ liệu CodeIgniter. Mô hình cập nhật, bộ điều khiển và tệp xem sẽ chia sẻ một số tính năng giống nhau như chèn cùng với ít mã hóa hơn của riêng nó Cập nhật dữ liệu trong cơ sở dữ liệu bằng ID trong CodeIgniterNếu bạn là độc giả thường xuyên của Koding Made Simple, thì bạn sẽ biết, trong hầu hết các hướng dẫn của tôi, tôi sử dụng Twitter Bootstrap CSS Framework để thiết kế giao diện người dùng. Trong hướng dẫn cập nhật cơ sở dữ liệu codeigniter này, tôi sẽ kết hợp bootstrap với codeigniter để thiết kế biểu mẫu cập nhật Thiết lập cơ sở dữ liệu MySQLĐối với DB, tôi sẽ sử dụng cùng một cơ sở dữ liệu nhân viên được sử dụng trong hướng dẫn chèn codeigniter. Chạy tệp sql này trong MySQL để tạo DB SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; CREATE TABLE IF NOT EXISTS `tbl_department` ( `department_id` int(4) NOT NULL AUTO_INCREMENT, `department_name` varchar(80) NOT NULL, PRIMARY KEY (`department_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; INSERT INTO `tbl_department` (`department_id`, `department_name`) VALUES (1, 'Finance'), (2, 'HQ'), (3, 'Operations'), (4, 'Marketing'), (5, 'Sales'); CREATE TABLE IF NOT EXISTS `tbl_designation` ( `designation_id` int(4) NOT NULL AUTO_INCREMENT, `designation_name` varchar(50) NOT NULL, PRIMARY KEY (`designation_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; INSERT INTO `tbl_designation` (`designation_id`, `designation_name`) VALUES (1, 'VP'), (2, 'Manager'), (3, 'Executive'), (4, 'Trainee'), (5, 'Senior Executive'); CREATE TABLE IF NOT EXISTS `tbl_employee` ( `employee_id` int(4) NOT NULL AUTO_INCREMENT, `employee_no` int(6) NOT NULL, `employee_name` varchar(60) NOT NULL, `department_id` int(4) NOT NULL, `designation_id` int(4) NOT NULL, `hired_date` date NOT NULL, `salary` int(10) NOT NULL, PRIMARY KEY (`employee_id`), UNIQUE KEY `employee_no` (`employee_no`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; INSERT INTO `tbl_employee` (`employee_id`, `employee_no`, `employee_name`, `department_id`, `designation_id`, `hired_date`, `salary`) VALUES (1, 1001, 'Steve John', 1, 2, '2013-08-01', 60000); Tệp Mô hình ('models/employee_model. php')db->where('employee_no', $empno); $this->db->from('tbl_employee'); $query = $this->db->get(); return $query->result(); } //get department table to populate the department name dropdown function get_department() { $this->db->select('department_id'); $this->db->select('department_name'); $this->db->from('tbl_department'); $query = $this->db->get(); $result = $query->result(); //array to store department id & department name $dept_id = array('-SELECT-'); $dept_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($dept_id, $result[$i]->department_id); array_push($dept_name, $result[$i]->department_name); } return $department_result = array_combine($dept_id, $dept_name); } //get designation table to populate the designation dropdown function get_designation() { $this->db->select('designation_id'); $this->db->select('designation_name'); $this->db->from('tbl_designation'); $query = $this->db->get(); $result = $query->result(); $designation_id = array('-SELECT-'); $designation_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($designation_id, $result[$i]->designation_id); array_push($designation_name, $result[$i]->designation_name); } return $designation_result = array_combine($designation_id, $designation_name); } } ?> Đọc. Cách tích hợp Twitter Bootstrap 3 với PHP CodeIgniter Framework Như đã nói trước đó, tệp mô hình sẽ tương tự như ví dụ về Codeigniter Bootstrap Insert nhưng có thêm một phương thức để tìm nạp bản ghi nhân viên cho mã số nhân viên đã cho. Phần còn lại của hai phương thức Tệp điều khiển ('controllers/updateEmployee. php')load->library('session'); $this->load->helper('form'); $this->load->helper('url'); $this->load->database(); $this->load->library('form_validation'); //load the employee model $this->load->model('employee_model'); } //index function function index($empno) { $data['empno'] = $empno; //fetch data from department and designation tables $data['department'] = $this->employee_model->get_department(); $data['designation'] = $this->employee_model->get_designation(); //fetch employee record for the given employee no $data['emprecord'] = $this->employee_model->get_employee_record($empno); //set validation rules $this->form_validation->set_rules('employeename', 'Employee Name', 'trim|required|xss_clean|callback_alpha_only_space'); $this->form_validation->set_rules('department', 'Department', 'callback_combo_check'); $this->form_validation->set_rules('designation', 'Designation', 'callback_combo_check'); $this->form_validation->set_rules('hireddate', 'Hired Date', 'required'); $this->form_validation->set_rules('salary', 'Salary', 'required|numeric'); if ($this->form_validation->run() == FALSE) { //fail validation $this->load->view('update_employee_view', $data); } else { //pass validation $data = array( 'employee_name' => $this->input->post('employeename'), 'department_id' => $this->input->post('department'), 'designation_id' => $this->input->post('designation'), 'hired_date' => @date('Y-m-d', @strtotime($this->input->post('hireddate'))), 'salary' => $this->input->post('salary'), ); //update employee record $this->db->where('employee_no', $empno); $this->db->update('tbl_employee', $data); //display success message $this->session->set_flashdata('msg', ' Đọc. Cách tạo biểu mẫu đăng nhập trong CodeIgniter, MySQL và Twitter Bootstrap Tạo tệp điều khiển và tải các thư viện codeigniter cần thiết cùng với mô hình nhân viên. Ở đây chúng tôi cập nhật hồ sơ nhân viên dựa trên trường mã số nhân viên. Truyền nó dưới dạng tham số url khi chúng ta gọi bộ điều khiển như thế này, db->where('employee_no', $empno); $this->db->from('tbl_employee'); $query = $this->db->get(); return $query->result(); } //get department table to populate the department name dropdown function get_department() { $this->db->select('department_id'); $this->db->select('department_name'); $this->db->from('tbl_department'); $query = $this->db->get(); $result = $query->result(); //array to store department id & department name $dept_id = array('-SELECT-'); $dept_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($dept_id, $result[$i]->department_id); array_push($dept_name, $result[$i]->department_name); } return $department_result = array_combine($dept_id, $dept_name); } //get designation table to populate the designation dropdown function get_designation() { $this->db->select('designation_id'); $this->db->select('designation_name'); $this->db->from('tbl_designation'); $query = $this->db->get(); $result = $query->result(); $designation_id = array('-SELECT-'); $designation_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($designation_id, $result[$i]->designation_id); array_push($designation_name, $result[$i]->designation_name); } return $designation_result = array_combine($designation_id, $designation_name); } } ?>0 Đoạn uri cuối cùng '1001' là đối số cho hàm index() của bộ điều khiển Điền trước biểu mẫu cập nhật CodeIgniter với dữ liệu cơ sở dữ liệuTiếp theo gọi lại tất cả ba phương thức của mô hình db->where('employee_no', $empno); $this->db->from('tbl_employee'); $query = $this->db->get(); return $query->result(); } //get department table to populate the department name dropdown function get_department() { $this->db->select('department_id'); $this->db->select('department_name'); $this->db->from('tbl_department'); $query = $this->db->get(); $result = $query->result(); //array to store department id & department name $dept_id = array('-SELECT-'); $dept_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($dept_id, $result[$i]->department_id); array_push($dept_name, $result[$i]->department_name); } return $department_result = array_combine($dept_id, $dept_name); } //get designation table to populate the designation dropdown function get_designation() { $this->db->select('designation_id'); $this->db->select('designation_name'); $this->db->from('tbl_designation'); $query = $this->db->get(); $result = $query->result(); $designation_id = array('-SELECT-'); $designation_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($designation_id, $result[$i]->designation_id); array_push($designation_name, $result[$i]->designation_name); } return $designation_result = array_combine($designation_id, $designation_name); } } ?>3 và lưu trữ chúng trong mảng dữ liệu để chuyển nó vào tệp xem Thêm Xác thực Biểu mẫu vào Biểu mẫu Cập nhật CodeIgniterTiếp theo đặt quy tắc xác thực cho các trường nhập biểu mẫu. Tôi đã tắt trường mã số nhân viên để hạn chế người dùng chỉnh sửa nó. Vì vậy, không có xác nhận cho lĩnh vực đó Sử dụng Truy vấn cập nhật CodeIgniter để chỉnh sửa bản ghi cơ sở dữ liệuTiếp theo chạy quy tắc xác thực trên các giá trị đã gửi và hiển thị thông báo lỗi khi xác thực không thành công. Nếu không, hãy di chuyển dữ liệu bài đăng vào một mảng và sử dụng câu lệnh truy vấn cập nhật codeigniter, db->where('employee_no', $empno); $this->db->from('tbl_employee'); $query = $this->db->get(); return $query->result(); } //get department table to populate the department name dropdown function get_department() { $this->db->select('department_id'); $this->db->select('department_name'); $this->db->from('tbl_department'); $query = $this->db->get(); $result = $query->result(); //array to store department id & department name $dept_id = array('-SELECT-'); $dept_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($dept_id, $result[$i]->department_id); array_push($dept_name, $result[$i]->department_name); } return $department_result = array_combine($dept_id, $dept_name); } //get designation table to populate the designation dropdown function get_designation() { $this->db->select('designation_id'); $this->db->select('designation_name'); $this->db->from('tbl_designation'); $query = $this->db->get(); $result = $query->result(); $designation_id = array('-SELECT-'); $designation_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($designation_id, $result[$i]->designation_id); array_push($designation_name, $result[$i]->designation_name); } return $designation_result = array_combine($designation_id, $designation_name); } } ?>0 để cập nhật bản ghi nhân viên cụ thể được cung cấp bởi câu lệnh db->where('employee_no', $empno); $this->db->from('tbl_employee'); $query = $this->db->get(); return $query->result(); } //get department table to populate the department name dropdown function get_department() { $this->db->select('department_id'); $this->db->select('department_name'); $this->db->from('tbl_department'); $query = $this->db->get(); $result = $query->result(); //array to store department id & department name $dept_id = array('-SELECT-'); $dept_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($dept_id, $result[$i]->department_id); array_push($dept_name, $result[$i]->department_name); } return $department_result = array_combine($dept_id, $dept_name); } //get designation table to populate the designation dropdown function get_designation() { $this->db->select('designation_id'); $this->db->select('designation_name'); $this->db->from('tbl_designation'); $query = $this->db->get(); $result = $query->result(); $designation_id = array('-SELECT-'); $designation_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($designation_id, $result[$i]->designation_id); array_push($designation_name, $result[$i]->designation_name); } return $designation_result = array_combine($designation_id, $designation_name); } } ?>1 Sau khi cập nhật db thành công, chúng tôi thông báo cho người dùng bằng cách hiển thị thông báo bằng phương thức db->where('employee_no', $empno); $this->db->from('tbl_employee'); $query = $this->db->get(); return $query->result(); } //get department table to populate the department name dropdown function get_department() { $this->db->select('department_id'); $this->db->select('department_name'); $this->db->from('tbl_department'); $query = $this->db->get(); $result = $query->result(); //array to store department id & department name $dept_id = array('-SELECT-'); $dept_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($dept_id, $result[$i]->department_id); array_push($dept_name, $result[$i]->department_name); } return $department_result = array_combine($dept_id, $dept_name); } //get designation table to populate the designation dropdown function get_designation() { $this->db->select('designation_id'); $this->db->select('designation_name'); $this->db->from('tbl_designation'); $query = $this->db->get(); $result = $query->result(); $designation_id = array('-SELECT-'); $designation_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($designation_id, $result[$i]->designation_id); array_push($designation_name, $result[$i]->designation_name); } return $designation_result = array_combine($designation_id, $designation_name); } } ?>2 Tệp Xem ('views/update_employee_view. php')
Chế độ xem codeigniter chứa biểu mẫu cập nhật. Tại đây, chúng tôi nhận được hồ sơ nhân viên được tìm nạp từ cơ sở dữ liệu và điền vào các trường biểu mẫu khi tải. Sau khi người dùng gửi biểu mẫu đã chỉnh sửa, khi xác thực trường biểu mẫu thành công, chúng tôi cập nhật bản ghi cơ sở dữ liệu bằng truy vấn cập nhật codeigniter Để điền các giá trị trường biểu mẫu cập nhật từ cơ sở dữ liệu khi tải, tôi đã chuyển chúng dưới dạng tham số thứ hai cho các hàm db->where('employee_no', $empno); $this->db->from('tbl_employee'); $query = $this->db->get(); return $query->result(); } //get department table to populate the department name dropdown function get_department() { $this->db->select('department_id'); $this->db->select('department_name'); $this->db->from('tbl_department'); $query = $this->db->get(); $result = $query->result(); //array to store department id & department name $dept_id = array('-SELECT-'); $dept_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($dept_id, $result[$i]->department_id); array_push($dept_name, $result[$i]->department_name); } return $department_result = array_combine($dept_id, $dept_name); } //get designation table to populate the designation dropdown function get_designation() { $this->db->select('designation_id'); $this->db->select('designation_name'); $this->db->from('tbl_designation'); $query = $this->db->get(); $result = $query->result(); $designation_id = array('-SELECT-'); $designation_name = array('-SELECT-'); for ($i = 0; $i < count($result); $i++) { array_push($designation_id, $result[$i]->designation_id); array_push($designation_name, $result[$i]->designation_name); } return $designation_result = array_combine($designation_id, $designation_name); } } ?>3. Ban đầu sẽ tải giá trị cơ sở dữ liệu và trong trường hợp có lỗi xác thực, nó sẽ điền lại các giá trị do người dùng gửi vào các trường biểu mẫuđọc thêm
Điều đó giải thích về việc cập nhật dữ liệu từ cơ sở dữ liệu bằng cách sử dụng id trong codeigniter và bootstrap framework. Tôi hy vọng bạn thấy Hướng dẫn cập nhật cơ sở dữ liệu PHP Codeigniter này hữu ích Làm cách nào để có được ngày và giờ hiện tại trong CI?Để lấy ngày giờ hiện tại, bạn phải sử dụng hàm mdate() trong codeigniter . Chức năng này tương tự như PHP date() nhưng cho phép bạn sử dụng mã ngày kiểu MySQL (%Y, %m, %d). Vì vậy, bạn không cần phải thoát nó trong khi xử lý ngày.
Làm cách nào để cập nhật một cột trong CodeIgniter?Nếu bạn chỉ muốn cập nhật dữ liệu cột thành 1 cho cuộc hẹn_id 0 thì hãy chuyển vào một mảng có dữ liệu cho khóa và 1 cho giá trị
Làm cách nào để chỉnh sửa và xóa dữ liệu trong CodeIgniter?Bước 1. Tạo bảng cơ sở dữ liệu MySQL. Vì chúng tôi sẽ triển khai DataTables với CodeIgniter để hiển thị dữ liệu nhân viên. . Bước 2. Triển khai mô hình. . Bước 3. Triển khai Bộ điều khiển. . Bước 4. Triển khai Chế độ xem. . Bước5. Triển khai bản ghi DataTables Thêm, chỉnh sửa, xóa. . 5 suy nghĩ về “DataTables Thêm Chỉnh sửa Xóa bằng CodeIgniter” |