欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

CI框架(CodeIgniter)實(shí)現(xiàn)的數(shù)據(jù)庫增刪改查操作總結(jié)

 更新時間:2018年05月23日 11:39:02   作者:cindylu520  
這篇文章主要介紹了CI框架(CodeIgniter)實(shí)現(xiàn)的數(shù)據(jù)庫增刪改查操作,結(jié)合實(shí)例形式總結(jié)分析了CI框架針對mysql數(shù)據(jù)庫增刪改查操作的模型、控制器及視圖相關(guān)定義與使用技巧,需要的朋友可以參考下

本文實(shí)例講述了CI框架(CodeIgniter)實(shí)現(xiàn)的數(shù)據(jù)庫增刪改查操作。分享給大家供大家參考,具體如下:

controllers下的 cquery.php文件

<?php
class CQuery extends Controller {
  //構(gòu)造函數(shù)
  function CQuery() {
    parent::Controller();
//   $this->load->database();
  }
  function index() {
    //調(diào)用model 其中train為外層文件夾  MQuery為model名稱 queryList為重命名
    $this->load->model('train/MQuery','queryList');
    //獲得返回的結(jié)果集  這里確定調(diào)用model中的哪個方法
    $result = $this->queryList->queryList();
    //將結(jié)果集賦給res
    $this->smarty->assign('res',$result);
    //跳轉(zhuǎn)到顯示頁面
    $this->smarty->view('train/vquery.tpl');
  }
  //進(jìn)入新增頁面
  function addPage() {
    $this->smarty->view('train/addPage.tpl');
  }
  //新增
  function add() {
    //獲得前臺數(shù)據(jù)
    //用戶名
    $memberName = $this->input->post('memberName');
    //密碼
    $password = $this->input->post('password');
    //真實(shí)姓名
    $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');
    //調(diào)用model
    $this->load->model('train/MQuery','addRecord');
    //向model中的addRecord傳值
    $result = $this->addRecord->addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
    //判斷返回的結(jié)果,如果返回true,則調(diào)用本頁的index方法,不要寫 $result == false 因?yàn)榉祷氐闹滴幢厥莊alse 也有可能是""
    if ($result) {
      $this->index();
    } else {
      echo "add failed.";
    }
  }
  //刪除
  function deletePage() {
    //獲得ID
    $deleteID = $this->uri->segment(4);
    //調(diào)用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);
    //將結(jié)果集賦給res
    $this->smarty->assign('res',$result);
    //跳轉(zhuǎn)到顯示頁面
    $this->smarty->view('train/changePage.tpl');
  }
  //修改
  function change() {
    //獲得前臺數(shù)據(jù)
    //ID
    $ID = $this->input->post('id');
    //用戶名
    $memberName = $this->input->post('memberName');
    //密碼
    $password = $this->input->post('password');
    //真實(shí)姓名
    $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');
    //調(diào)用model
    $this->load->model('train/MQuery','change');
    //向model中的change傳值
    $result = $this->change->change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
    //判斷返回的結(jié)果,如果返回true,則調(diào)用本頁的index方法,不要寫 $result == false 因?yàn)榉祷氐闹滴幢厥莊alse 也有可能是""
    if ($result) {
      $this->index();
    } else {
      echo "change failed.";
    }
  }
}

models中的 mquery.php 文件

<?php
class MQuery extends Model {
  //構(gòu)造函數(shù)
  function MQuery() {
    parent::Model();
    //連接數(shù)據(jù)庫
    $this->load->database();
  }
  //查詢列表
  function queryList() {
    //防止select出的數(shù)據(jù)存在亂碼問題
    //mysql_query("SET NAMES GBK");
    //SQL語句
    $sql = "SELECT ID,member_name,sex,e_mail FROM user_info_t";
    //執(zhí)行SQL
    $rs = $this->db->query($sql);
    //將查詢結(jié)果放入到結(jié)果集中
    $result = $rs->result();
    //關(guān)閉數(shù)據(jù)庫
    $this->db->close();
    //將結(jié)果集返回
    return $result;
  }
  //新增
  function addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
    //防止select出的數(shù)據(jù)存在亂碼問題
    //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')";
    //執(zhí)行SQL
    $result = $this->db->query($sql);
    //關(guān)閉數(shù)據(jù)庫
    $this->db->close();
    //返回值
    return $result;
  }
  //刪除
  function delRecord($deleteID) {
    //防止select出的數(shù)據(jù)存在亂碼問題
    //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出的數(shù)據(jù)存在亂碼問題
    //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";
    //執(zhí)行SQL
    $rs = $this->db->query($sql);
    $result = $rs->row();//$result = $rs[0]
    //關(guān)閉數(shù)據(jù)庫
    $this->db->close();
    //將結(jié)果集返回
    return $result;
  }
  //修改
  function change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
    //防止select出的數(shù)據(jù)存在亂碼問題
    //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";
    //執(zhí)行SQL
    $result = $this->db->query($sql);
    //關(guān)閉數(shù)據(jù)庫
    $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>真實(shí)姓名</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>真實(shí)姓名</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>

更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。

相關(guān)文章

  • PHP仿盜鏈代碼

    PHP仿盜鏈代碼

    幫一朋友修改的PHP仿盜鏈代碼,這里分享下,方便需要的朋友
    2012-06-06
  • yii gridview實(shí)現(xiàn)時間段篩選功能

    yii gridview實(shí)現(xiàn)時間段篩選功能

    這篇文章主要為大家詳細(xì)介紹了yii gridview實(shí)現(xiàn)時間段篩選功能,一個輸入框,自動提交功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 對淘寶URL中ID提取的PHP代碼

    對淘寶URL中ID提取的PHP代碼

    這篇文章介紹了對淘寶URL中ID提取的PHP代碼,有需要的朋友可以參考一下
    2013-09-09
  • 淺談PHP array_search 和 in_array 函數(shù)效率問題

    淺談PHP array_search 和 in_array 函數(shù)效率問題

    這篇文章主要介紹了淺談PHP array_search 和 in_array 函數(shù)效率問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • php實(shí)現(xiàn)商城購物車的思路和源碼分析

    php實(shí)現(xiàn)商城購物車的思路和源碼分析

    這篇文章主要介紹了php實(shí)現(xiàn)商城購物車的思路和源碼分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 3款值得推薦的微信開發(fā)開源框架

    3款值得推薦的微信開發(fā)開源框架

    下面向大家介紹的是微信開發(fā)項目里使用的3款基于PHP的開發(fā)框架,相信一定對你的開發(fā)工作有所幫助。
    2014-10-10
  • 使用php完成常見的文件上傳功能(推薦)

    使用php完成常見的文件上傳功能(推薦)

    文件上傳功能在開發(fā)過程中是非常常見的功能,可以上傳文件也可以上傳頭像等,不同的瀏覽器文件上傳有不同的效果,下面通過本文給大家分享使用php完成常見的文件上傳功能,需要的朋友參考下吧
    2017-01-01
  • php定界符<<<使用技巧和實(shí)例

    php定界符<<<使用技巧和實(shí)例

    這篇文章主要介紹了php定界符<<<使用技巧和實(shí)例,講解了定界符的功能和使用注意事項等,需要的朋友可以參考下
    2014-06-06
  • php GUID生成函數(shù)和類

    php GUID生成函數(shù)和類

    這篇文章主要介紹了使用php生成GUID的方法,分別使用了函數(shù)和類的方式生成GUID,詳細(xì)介紹了什么是GUID、GUID的優(yōu)點(diǎn)等,需要的朋友可以參考下
    2014-03-03
  • php實(shí)現(xiàn)頭像上傳預(yù)覽功能

    php實(shí)現(xiàn)頭像上傳預(yù)覽功能

    這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)頭像上傳預(yù)覽功能的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04

最新評論