1. 程式人生 > >原生程式碼封裝好增刪改查

原生程式碼封裝好增刪改查

封裝好增刪改查的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>";
  }
?>