CI框架(CodeIgniter)实现的数据库增删改查操作总结

本文实例讲述了CI框架(CodeIgniter)实现的数据库增删改查操作。分享给大家供大家参考,具体如下:

controllers下的 cquery.PHP文件

rush:PHP;"> load->database(); } function index() { //调用model 其中train为外层文件夹 MQuery为model名称 queryList为重命名 $this->load->model('train/MQuery','queryList'); //获得返回的结果集 这里确定调用model中的哪个方法 $result = $this->queryList->queryList(); //将结果集赋给res $this->smarty->assign('res',$result); //跳转显示页面 $this->smarty->view('train/vquery.tpl'); } //进入新增页面 function addPage() { $this->smarty->view('train/addPage.tpl'); } //新增 function add() { //获得前台数据 //用户名 $memberName = $this->input->post('memberName'); //密码 $password = $this->input->post('password'); //真实姓名 $userRealName = $this->input->post('userRealName'); //性别 $sex = $this->input->post('sex'); //出生日期 $bornDay = $this->input->post('bornDay'); //e_mail $eMail = $this->input->post('eMail'); //密码问题 $question = $this->input->post('question'); //密码答案 $answer = $this->input->post('answer'); //调用model $this->load->model('train/MQuery','addRecord'); //向model中的addRecord传值 $result = $this->addRecord->addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer); //判断返回的结果,如果返回true,则调用本页的index方法,不要写 $result == false 因为返回的值未必是false 也有可能是"" if ($result) { $this->index(); } else { echo "add Failed."; } } //删除 function deletePage() { //获得ID $deleteID = $this->uri->segment(4); //调用model $this->load->model('train/MQuery','delRecord'); //将值传入到model的delRecord方法中 $result = $this->delRecord->delRecord($deleteID); //判断返回值 if ($result) { $this->index(); } else { echo "delect Failed."; } } //修改查询 function changePage() { $changeID = $this->uri->segment(4); $this->load->model('train/MQuery','changeRecord'); $result = $this->changeRecord->changeRecord($changeID); //将结果集赋给res $this->smarty->assign('res',$result); //跳转显示页面 $this->smarty->view('train/changePage.tpl'); } //修改 function change() { //获得前台数据 //ID $ID = $this->input->post('id'); //用户名 $memberName = $this->input->post('memberName'); //密码 $password = $this->input->post('password'); //真实姓名 $userRealName = $this->input->post('userRealName'); //性别 $sex = $this->input->post('sex'); //出生日期 $bornDay = $this->input->post('bornDay'); //e_mail $eMail = $this->input->post('eMail'); //密码问题 $question = $this->input->post('question'); //密码答案 $answer = $this->input->post('answer'); //调用model $this->load->model('train/MQuery','change'); //向model中的change传值 $result = $this->change->change($ID,$memberName,不要写 $result == false 因为返回的值未必是false 也有可能是"" if ($result) { $this->index(); } else { echo "change Failed."; } } }

models中的 mquery.PHP 文件

rush:PHP;"> load->database(); } //查询列表 function queryList() { //防止select出的数据存在乱码问题 //MysqL_query("SET NAMES GBK"); //sql语句 $sql = "SELECT ID,member_name,sex,e_mail FROM user_info_t"; //执行sql $rs = $this->db->query($sql); //将查询结果放入到结果集中 $result = $rs->result(); //关闭数据库 $this->db->close(); //将结果集返回 return $result; } //新增 function addRecord($memberName,$answer) { //防止select出的数据存在乱码问题 //MysqL_query("SET NAMES GBK"); //sql语句 $sql = "INSERT INTO user_info_t (member_name,password,user_real_name,born_day,e_mail,question,answer) " . "VALUES ('$memberName','$password','$userRealName','$sex','$bornDay','$eMail','$question','$answer')"; //执行sql $result = $this->db->query($sql); //关闭数据库 $this->db->close(); //返回值 return $result; } //删除 function delRecord($deleteID) { //防止select出的数据存在乱码问题 //MysqL_query("SET NAMES GBK"); $sql = "DELETE FROM user_info_t WHERE ID = $deleteID"; $result = $this->db->query($sql); $this->db->close(); return $result; } //修改查询 function changeRecord($changeID) { //防止select出的数据存在乱码问题 //MysqL_query("SET NAMES GBK"); $sql = "SELECT ID,answer FROM user_info_t WHERE ID = $changeID"; //执行sql $rs = $this->db->query($sql); $result = $rs->row();//$result = $rs[0] //关闭数据库 $this->db->close(); //将结果集返回 return $result; } //修改 function change($ID,$answer) { //防止select出的数据存在乱码问题 //MysqL_query("SET NAMES GBK"); //sql语句 $sql = "update user_info_t set member_name = '$memberName',password = '$password',user_real_name = '$userRealName'," . "sex = '$sex',born_day = '$bornDay',e_mail = '$eMail',question = '$question',answer = '$answer'" . "where ID = $ID"; //执行sql $result = $this->db->query($sql); //关闭数据库 $this->db->close(); //返回值 return $result; } }

views 下的 addPage.tpl文件

rush:xhtml;">
用户名

changePage.tpl 文件

rush:xhtml;"> 用户名

vquery.tpl 文件

rush:xhtml;"> 用户名ID}}> member_name}}sex}}e_mail}}删除修改add

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用