CI架構(CodeIgniter)實現的資料庫增刪改查操作

來源:互聯網
上載者:User
這篇文章主要介紹了CI架構(CodeIgniter)實現的資料庫增刪改查操作,結合執行個體形式總結分析了CI架構針對mysql資料庫增刪改查操作的模型、控制器及視圖相關定義與提示,需要的朋友可以參考下

本文執行個體講述了CI架構(CodeIgniter)實現的資料庫增刪改查操作。分享給大家供大家參考,具體如下:

controllers下的 cquery.php檔案

<?phpclass 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 檔案

<?phpclass 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>

您可能感興趣的文章:

PHP實現一維數組與二維數組去重功能樣本

CI架構(CodeIgniter)實現的匯入、匯出資料操作樣本

ThinkPHP架構實現的MySQLDatabase Backup功能樣本

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.