CI框架(CodeIgniter)实现的数据库增删改查操作总结
程序员文章站
2022-03-04 16:09:57
本文实例讲述了ci框架(codeigniter)实现的数据库增删改查操作。分享给大家供大家参考,具体如下:
controllers下的 cquery.php文件...
本文实例讲述了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."; } } }
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>
更多关于codeigniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php优秀开发框架总结》、《thinkphp入门教程》、《thinkphp常用方法总结》、《zend framework框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于codeigniter框架的php程序设计有所帮助。
下一篇: 南海三成五金企业使用机器人 实现智能化