首頁 > 後端開發 > php教程 > CI框架(CodeIgniter)實作的資料庫增刪改查操作總結

CI框架(CodeIgniter)實作的資料庫增刪改查操作總結

不言
發布: 2023-03-28 22:02:05
原創
1826 人瀏覽過

這篇文章主要介紹了CI框架(CodeIgniter)實現的資料庫增刪改查操作,結合實例形式總結分析了CI框架針對mysql資料庫增刪改查操作的模型、控制器及視圖相關定義與使用技巧,需要的朋友可以參考下

本文實例講述了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(&#39;train/MQuery&#39;,&#39;queryList&#39;);
    //获得返回的结果集  这里确定调用model中的哪个方法
    $result = $this->queryList->queryList();
    //将结果集赋给res
    $this->smarty->assign(&#39;res&#39;,$result);
    //跳转到显示页面
    $this->smarty->view(&#39;train/vquery.tpl&#39;);
  }
  //进入新增页面
  function addPage() {
    $this->smarty->view(&#39;train/addPage.tpl&#39;);
  }
  //新增
  function add() {
    //获得前台数据
    //用户名
    $memberName = $this->input->post(&#39;memberName&#39;);
    //密码
    $password = $this->input->post(&#39;password&#39;);
    //真实姓名
    $userRealName = $this->input->post(&#39;userRealName&#39;);
    //性别
    $sex = $this->input->post(&#39;sex&#39;);
    //出生日期
    $bornDay = $this->input->post(&#39;bornDay&#39;);
    //e_mail
    $eMail = $this->input->post(&#39;eMail&#39;);
    //密码问题
    $question = $this->input->post(&#39;question&#39;);
    //密码答案
    $answer = $this->input->post(&#39;answer&#39;);
    //调用model
    $this->load->model(&#39;train/MQuery&#39;,&#39;addRecord&#39;);
    //向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(&#39;train/MQuery&#39;,&#39;delRecord&#39;);
    //将值传入到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(&#39;train/MQuery&#39;,&#39;changeRecord&#39;);
    $result = $this->changeRecord->changeRecord($changeID);
    //将结果集赋给res
    $this->smarty->assign(&#39;res&#39;,$result);
    //跳转到显示页面
    $this->smarty->view(&#39;train/changePage.tpl&#39;);
  }
  //修改
  function change() {
    //获得前台数据
    //ID
    $ID = $this->input->post(&#39;id&#39;);
    //用户名
    $memberName = $this->input->post(&#39;memberName&#39;);
    //密码
    $password = $this->input->post(&#39;password&#39;);
    //真实姓名
    $userRealName = $this->input->post(&#39;userRealName&#39;);
    //性别
    $sex = $this->input->post(&#39;sex&#39;);
    //出生日期
    $bornDay = $this->input->post(&#39;bornDay&#39;);
    //e_mail
    $eMail = $this->input->post(&#39;eMail&#39;);
    //密码问题
    $question = $this->input->post(&#39;question&#39;);
    //密码答案
    $answer = $this->input->post(&#39;answer&#39;);
    //调用model
    $this->load->model(&#39;train/MQuery&#39;,&#39;change&#39;);
    //向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 (&#39;$memberName&#39;,&#39;$password&#39;,&#39;$userRealName&#39;,&#39;$sex&#39;,&#39;$bornDay&#39;,&#39;$eMail&#39;,&#39;$question&#39;,&#39;$answer&#39;)";
    //执行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 = &#39;$memberName&#39;,password = &#39;$password&#39;, user_real_name = &#39;$userRealName&#39;," .
        "sex = &#39;$sex&#39;,born_day = &#39;$bornDay&#39;,e_mail = &#39;$eMail&#39;,question = &#39;$question&#39;,answer = &#39;$answer&#39;" .
        "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=&#39;train/cquery/add&#39;}}" method="post">
    <table border=&#39;1&#39;>
      <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=&#39;train/cquery/change&#39;}}" method="post">
    <table border=&#39;1&#39;><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=&#39;1&#39;>
      <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=&#39;train/cquery/deletePage&#39;}}/{{$row->ID}}" rel="external nofollow" >删除</a><a href="{{site_url url=&#39;train/cquery/changePage&#39;}}/{{$row->ID}}" rel="external nofollow" >修改</a></td>
      </tr>
      {{/foreach}}
    </table>
    <a href="{{site_url url=&#39;train/cquery/addPage&#39;}}" rel="external nofollow" rel="external nofollow" mce_href="{{site_url url=&#39;train/cquery/addPage&#39;}}" rel="external nofollow" rel="external nofollow" >add</a>
  </body>
</html>
登入後複製

相關推薦:

CI框架(CodeIgniter )實作的導入、匯出資料操作


###########################################################################

以上是CI框架(CodeIgniter)實作的資料庫增刪改查操作總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板