入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

原生代码封装好增删改查

创建时间:2016-05-31 投稿人: 浏览次数:3320

封装好增删改查的sql语句

1.建立一个DB.class.php

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

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

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

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

<?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.修改数据

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

<?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>";
  }
?>


声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。