原生代码封装好的增删改查
2016-07-29 19:47
344 查看
封装好增删改查的sql语句 1.建立一个DB.class.php 在CODE上查看代码片派生到我的代码片 <?php //解析头 header('content-type:text/html;charset=utf-8'); class DB{ function __construct($host,$user,$pass,$dbname){ $link=mysql_connect($host,$user,$pass); mysql_select_db($dbname,$link); mysql_query('set names utf8'); } //查询sql function select($table,$table1,$id){ //准备查询的sql语句 $sql="select * from $table inner join $table1 on $table.$id=$table1.$id"; //执行 $res=mysql_query($sql); //定义空数组 $arr=array(); while($row=mysql_fetch_assoc($res)){ $arr[]=$row; } return $arr; } function all($table){ $sql="select * from $table"; //echo $sql;die; $res=mysql_query($sql); //var_dump($res);die; $arr=array(); while($row=mysql_fetch_assoc($res)){ $arr[]=$row; } //var_dump($arr);die; return $arr; } //添加sql语句 function add($table,$arr){ $str=array_values($arr); $str=implode("','",$str); $sql="insert into $table values(null,'$str')"; //echo $sql;die; $res=mysql_query($sql); if($res&&mysql_affected_rows()>0){ return true; }else{ return false; } } //删出sql语句 function del($table,$where){ $sql="delete from $table where $where"; $res=mysql_query($sql); if($res&&mysql_affected_rows()>0){ return true; }else{ return false; } } //查询修改语句需要查询的语句 function update($table,$where){ $sql="select * from $table where $where"; //echo $sql;die; $res=mysql_query($sql); $row=mysql_fetch_assoc($res); //var_dump($row);die; return $row; } //改 function updateok($table,$update,$where){ $sql="update $table set $update where $where"; $res=mysql_query($sql); if($res&&mysql_affected_rows()>0){ return true; }else{ return false; } } } ?> 1.form表单页面form.php 在CODE上查看代码片派生到我的代码片 <?php include 'DB.class.php'; $db=new DB('127.0.0.1:3306','root','root','dongwu'); $arr=$db->all('nickname'); //var_dump($arr);die; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title></title> </head> <body> <center> <form action="add.php" method='post'> <table border=1> <tr> <td>请选择昵称:</td> <td> <select name="nid"> <?php foreach($arr as $v){?> <option value="<?php echo $v['nid'];?>"><?php echo $v['name'];?></option> <?php }?> </select> </td> </tr> <tr> <td>留言内容:</td> <td><textarea name="content" cols="20" rows="5"></textarea></td> </tr> <tr> <td><input type="submit" value='提交留言'/></td> <td></td> </tr> </table> </form> </center> </body> </html> 2.添加sql语句,直接引进DB.class.php add.php 在CODE上查看代码片派生到我的代码片 <?php include 'DB.class.php'; $db=new DB('127.0.0.1:3306','root','root','dongwu'); $arr=$_POST; $res=$db->add('message',$arr); if($res){ echo "<script>alert('添加成功');location.href='list.php'</script>"; }else{ echo "<script>alert('添加失败');location.href='form.php'</script>"; } ?> 3.删除sql语句,直接引进DB.class.php delete.php 在CODE上查看代码片派生到我的代码片 <?php include 'DB.class.php'; $db=new DB('127.0.0.1:3306','root','root','dongwu'); $id=$_GET['id']; $res=$db->del('message',"mid=$id"); if($res){ echo "<script>alert('删除成功');location.href='list.php'</script>"; }else{ echo "<script>alert('删除失败');location.href='list.php'</script>"; } ?> 4.显示表单list.php 在CODE上查看代码片派生到我的代码片 <?php include 'DB.class.php'; $db=new DB('127.0.0.1:3306','root','root','dongwu'); $arr=$db->select('message','nickname','nid'); ?> <center> <table border=1> <tr> <th>昵称</th> <th>留言内容</th> <th>操作</th> </tr> <?php foreach($arr as $v){?> <tr> <td><?php echo $v['name'];?></td> <td><?php echo $v['content'];?></td> <td><a href="delete.php?id=<?php echo $v['mid'];?>">删除</a>||<a href="update.php?id=<?php echo $v['mid'];?>">修改</a></td> </tr> <?php }?> </table> </center> 5.修改数据 在CODE上查看代码片派生到我的代码片 <?php include 'DB.class.php'; $db=new DB('127.0.0.1:3306','root','root','dongwu'); $id=$_GET['id']; $row=$db->update('message',"mid=$id"); //var_dump($row);die; $arr=$db->all('nickname'); ?> <center> <form action="updateok.php?id=<?php echo $row['mid'];?>" method='post'> <table border=1> <tr> <td>请选择昵称:</td> <td> <select name="nid"> <?php foreach($arr as $v){?> <option value="<?php echo $v['nid'];?>" <?php if($row['nid']=$v['nid']){ echo 'selected';}?>><?php echo $v['name'];?></option> <?php }?> </select> </td> </tr> <tr> <td>留言内容:</td> <td><textarea name="content" cols="20" rows="5"><?php echo $row['content'];?></textarea></td> </tr> <tr> <td><input type="submit" value='修改留言'/></td> <td></td> </tr> </table> </form> </center> 将修改的数据调到updateok.php中 在CODE上查看代码片派生到我的代码片 <?php include 'DB.class.php'; $nid=$_POST['nid']; $content=$_POST['content']; $id=$_GET['id']; $db=new DB('127.0.0.1:3306','root','root','dongwu'); $res=$db->updateok('message',"content='$content',nid=$nid","mid=$id"); if($res){ echo "<script>alert('修改成功');location.href='list.php'</script>"; }else{ echo "<script>alert('修改失败');location.href='list.php'</script>"; } ?>
相关文章推荐
- 原生代码封装好增删改查
- 后台代码对iBatis配置文件中具体的sql语句的调用实现(被封装的增删改查)
- 原生JDBCUtils工具类封装代码实现
- 封装的原生javascript弹出层代码
- 利用SpringMVC原生封装的对象实现图片上传及图片回显功能的实现及案例代码
- 原生js封装AJAX(代码+注释)
- 原生js封装二级城市下拉列表的实现代码
- 原生JS实现风箱式demo,并封装了一个运动框架(实例代码)
- 原生iOS封装代码访问QQ客服
- 原生js封装ajax代码
- Ajax实战总结——用原生JavaScript代码封装自己的Ajax核心对象
- 原生JS实现风箱式demo,并封装了一个运动框架(实例代码)
- (原理篇)基于SQLite3轻量级封装,一行代码实现增删改查
- 封装的原生javascript弹出层代码
- 使用Require.js封装原生js轮播图的实现代码
- javascript原生封装一个淡入淡出效果的函数测试实例代码
- 基于SQLite3轻量级封装,一行代码实现增删改查
- js原生代码封装$
- 原生JS封装class操作取增删