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

不言
发布: 2023-03-28 22:02:05
原创
1643 人浏览过

这篇文章主要介绍了CI框架(CodeIgniter)实现的数据库增删改查操作,结合实例形式总结分析了CI框架针对mysql数据库增删改查操作的模型、控制器及视图相关定义与使用技巧,需要的朋友可以参考下

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

controllers下的 cquery.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,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
    //判断返回的结果,如果返回true,则调用本页的index方法,不要写 $result == false 因为返回的值未必是false 也有可能是""
    if ($result) {
      $this->index();
    } else {
      echo "change failed.";
    }
  }
}
登录后复制

models中的 mquery.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,$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文件


  
  
用户名
密码
真实姓名
性别
出生日期
e_mail
密码问题
密码答案
登录后复制

changePage.tpl 文件


  
  
用户名
密码
真实姓名
性别
出生日期
e_mail
密码问题
密码答案
登录后复制

vquery.tpl 文件


  
    
  
{{foreach from=$res item=row}} ID}}> {{/foreach}}
用户名 性别 e_mail 操作
{{$row->member_name}} {{$row->sex}} {{$row->e_mail}} 删除修改
add
登录后复制

相关推荐:

CI框架(CodeIgniter)实现的导入、导出数据操作


以上是CI框架(CodeIgniter)实现的数据库增删改查操作总结的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!