CI框架(CodeIgniter)实现的数据库增删改查操作总结
发布:smiling 来源: PHP粉丝网 添加日期:2021-09-17 19:18:18 浏览: 评论:0
本文实例讲述了CI框架(CodeIgniter)实现的数据库增删改查操作,分享给大家供大家参考,具体如下:
controllers下的 cquery.php文件
- <?php
- class CQuery extends Controller {
- //构造函数
- function CQuery() {
- parent::Controller();
- // $this->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,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
- //判断返回的结果,如果返回true,则调用本页的index方法,不要写 $result == false 因为返回的值未必是false 也有可能是""
- if ($result) {
- $this->index();
- } else {
- echo "change failed.";
- }
- }
- }
- <?php
- class CQuery extends Controller {
- //构造函数
- function CQuery() {
- parent::Controller();
- // $this->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,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
- //判断返回的结果,如果返回true,则调用本页的index方法,不要写 $result == false 因为返回的值未必是false 也有可能是""
- if ($result) {
- $this->index();
- } else {
- echo "change failed.";
- }
- }
- }
models中的 mquery.php 文件
- <?php
- class MQuery extends Model {
- //构造函数
- function MQuery() {
- parent::Model();
- //连接数据库
- $this->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,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
- //防止select出的数据存在乱码问题
- //mysql_query("SET NAMES GBK");
- //SQL语句
- $sql = "INSERT INTO user_info_t (member_name,password,user_real_name,sex,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,member_name,password,user_real_name,sex,born_day,e_mail,question,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,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$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文件
- <html>
- <head>
- </head>
- <body><form action="{{site_url url='train/cquery/add'}}" method="post">
- <table border='1'>
- <tr>
- <td>用户名</td>
- <td><input type="text" class="text" name="memberName" id="memberName"/></td>
- </tr>
- <tr>
- <td>密码</td>
- <td><input type="text" class="text" name="password" id="password"/></td>
- </tr>
- <tr>
- <td>真实姓名</td>
- <td><input type="text" class="text" name="userRealName" id="userRealName"/></td>
- </tr>
- <tr>
- <td>性别</td>
- <td><input type="text" class="text" name="sex" id="sex"/></td>
- </tr>
- <tr>
- <td>出生日期</td>
- <td><input type="text" class="text" name="bornDay" id="bornDay"/></td>
- </tr>
- <tr>
- <td>e_mail</td>
- <td><input type="text" class="text" name="eMail" id="eMail"/></td>
- </tr>
- <tr>
- <td>密码问题</td>
- <td><input type="text" class="text" name="question" id="question"/></td>
- </tr>
- <tr>
- <td>密码答案</td>
- <td><input type="text" class="text" name="answer" id="answer"/></td>
- </tr>
- </table>
- <table>
- <tr>
- <td><input type="submit" class="button" name="OK" value="提交" />
- </td>
- </tr>
- </table></form>
- </body>
- </html>
changePage.tpl 文件
- <html>
- <head>
- </head>
- <body><form action="{{site_url url='train/cquery/change'}}" method="post">
- <table border='1'><input type="hidden" name="id" value="{{$res->ID}}" />
- <tr>
- <td>用户名</td>
- <td><input type="text" class="text" name="memberName" id="memberName" value="{{$res->member_name}}" /></td>
- </tr>
- <tr>
- <td>密码</td>
- <td><input type="text" class="text" name="password" id="password" value="{{$res->password}}" /></td>
- </tr>
- <tr>
- <td>真实姓名</td>
- <td><input type="text" class="text" name="userRealName" id="userRealName" value="{{$res->user_real_name}}"/></td>
- </tr>
- <tr>
- <td>性别</td>
- <td><input type="text" class="text" name="sex" id="sex" value="{{$res->sex}}"/></td>
- </tr>
- <tr>
- <td>出生日期</td>
- <td><input type="text" class="text" name="bornDay" id="bornDay" value="{{$res->born_day}}"/></td>
- </tr>
- <tr>
- <td>e_mail</td>
- <td><input type="text" class="text" name="eMail" id="eMail" value="{{$res->e_mail}}"/></td>
- </tr>
- <tr>
- <td>密码问题</td>
- <td><input type="text" class="text" name="question" id="question" value="{{$res->question}}"/></td>
- </tr>
- <tr>
- <td>密码答案</td>
- <td><input type="text" class="text" name="answer" id="answer" value="{{$res->answer}}"/></td>
- </tr>
- </table>
- <table>
- <tr>
- <td><input type="submit" class="button" name="OK" value="提交" />
- </td>
- </tr>
- </table></form>
- </body>
- </html>
vquery.tpl 文件
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <table border='1'>
- <tr>
- <td>用户名</td>
- <td>性别</td>
- <td>e_mail</td>
- <td>操作</td>
- </tr>
- {{foreach from=$res item=row}}
- <tr>
- <input type="hidden" value={{$row->ID}}>
- <td>{{$row->member_name}}</td>
- <td>{{$row->sex}}</td>
- <td>{{$row->e_mail}}</td>
- <td><a href="{{site_url url='train/cquery/deletePage'}}/{{$row->ID}}" rel="external nofollow" >删除</a><a href="{{site_url url='train/cquery/changePage'}}/{{$row->ID}}" rel="external nofollow" >修改</a></td>
- </tr>
- {{/foreach}}
- </table>
- <a href="{{site_url url='train/cquery/addPage'}}" rel="external nofollow" rel="external nofollow" mce_href="{{site_url url='train/cquery/addPage'}}" rel="external nofollow" rel="external nofollow" >add</a>
- </body>
- </html>
Tags: CodeIgniter CI增删改查
相关文章
- ·CodeIgniter3.0+框架自定义异常处理的方法介绍(2020-02-08)
- ·解决Codeigniter不能上传rar和zip压缩包问题(2020-10-19)
- ·CodeIgniter框架中_remap()使用方法2例(2020-10-20)
- ·CI(CodeIgniter)框架中的增删改查操作(2021-02-11)
- ·CodeIgniter启用缓存和清除缓存的方法(2021-02-13)
- ·让CodeIgniter数据库缓存自动过期的处理的方法(2021-02-13)
- ·Codeigniter生成Excel文档的简单方法(2021-02-13)
- ·Codeigniter+PHPExcel实现导出数据到Excel文件(2021-02-13)
- ·Codeigniter实现智能裁剪图片的方法(2021-02-13)
- ·Codeigniter整合Tank Auth权限类库详解(2021-02-18)
- ·新浪SAE云平台下使用codeigniter的数据库配置(2021-02-18)
- ·Codeigniter实现处理用户登录验证后的URL跳转(2021-02-18)
- ·让codeigniter与swfupload整合的最佳解决方案(2021-02-18)
- ·Codeigniter实现多文件上传并创建多个缩略图(2021-02-18)
- ·让CodeIgniter的ellipsize()支持中文截断的方法(2021-02-18)
- ·CodeIgniter框架过滤HTML危险代码(2021-02-18)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)