标签:
练习-------租房子-----增删改查,多条件
一 、题目要求:
二 、做法:
【1】建立数据库
【2】封装类文件------DBDA.class.php
1 <?php 2 class DBDA 3 { 4 public $fuwuqi="localhost"; //服务器地址 5 public $yonghuming="root";//用户名 6 public $mima="";//密码 7 8 public $dbconnect;//连接对象 9 10 //操作数据库的方法 11 12 //$sql代表需要执行的SQL语句 13 //$type代表SQL语句的类型,1代表查询,2代表增删改 14 //$shujukuming代表数据库的名称 15 //如果是查询,返回二维数组 16 //如果是增删改,返回true或false 17 18 function Query($sql,$type=1,$shujukuming="house") 19 { 20 //造连接对象 21 $this->dbconnect = new MySQLi($this->fuwuqi,$this->yonghuming,$this->mima,$shujukuming); 22 23 //判断是否出错 24 if(!mysqli_connect_error()) 25 { 26 //如果连接成功,执行SQL语句 27 $result = $this->dbconnect->query($sql); 28 29 //根据语句类型判断 30 if($type==1) 31 { 32 //如果是查询语句,返回二维数组 33 return $result->fetch_all(); 34 } 35 else 36 { 37 //如果是其他语句,返回true或false 38 return $result; 39 } 40 41 } 42 else 43 { 44 return"连接失败"; 45 46 } 47 48 49 } 50 51 } 52 53 54 55 56 ?>
【3】租房子首页-----liebiaoyemian.php
<!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>租房子---首页</title> </head> <body> <table width="1000px" cellpadding="1" border="1" ellspacing="1"> <tr> <td>关键字</td> <td>区域</td> <td>使用面积</td> <td>租金</td> <td>租贷类型</td> <td>房屋类型</td> <td></td> <td></td> </tr> <?php include("DBDA.class.php"); $dx=new DBDA(); $sql="select * from house"; $r = $dx->Query($sql,1); //$attr=$result->fetch_all(); foreach($r as $v) { echo "<tr> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> <td>{$v[6]}</td> <td><a href=‘bianji.php?id={$v[0]}‘>编辑</a></td> <td><a href=‘shanchuchuli.php?id={$v[0]}‘ onclick=\"return confirm(‘确定删除吗‘)\">删除</a></td> </tr>"; } ?> </table> <br /> <br /> <a href="tianjiashuju.php"><input type="button" value="添加数据"/></a> <a href="duotiaojianchaxun.php"><input type="button" value="搜索查询" /></a> </title> </body> </html><!--id={$v[0]} 主键-->
【4】删除数据-----shanchuchuli.php
1 <?php 2 $id = $_GET["id"]; 3 var_dump($newsid); 4 5 include("DBDA.class.php"); 6 7 $dx=new DBDA(); 8 9 $sql = "delete from House where id=‘{$id}‘"; 10 $r = $dx->Query($sql,2); 11 if($r) 12 { 13 header("location:liebiaoyemian.php"); 14 } 15 else 16 { 17 echo "删除失败!"; 18 }
【5】编辑页面-------bianji.php
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>修改房屋数据</title> 6 </head> 7 <body> 8 <!--newsid--><center> 9 <h1>修改房屋数据</h1> 10 <?php 11 12 $id = $_GET["id"]; 13 14 include("DBDA.class.php"); 15 16 $dx=new DBDA(); 17 18 //echo "id"; 19 //var_dump($id); 20 21 $sql="select * from house where id=‘{$id}‘"; 22 $r=$dx->Query($sql); 23 24 ?> 25 26 27 <form action="bianjichuli.php" method="post"> 28 29 <input type="hidden" name="id" value="<?php echo $r[0][0];?>"/><!--id传过的ID--> 30 31 <div>关键字:<input type="text" name="KeyWord" value="<?php echo $r[0][1];?>"/></div> 32 <div>区域:<input type="text" name="Area" value="<?php echo $r[0][2];?>"/></div> 33 <div>使用面积:<input type="text" name="SquareMeter" value="<?php echo $r[0][3];?>"/></div> 34 <div>租金:<input type="text" name="Rent" value="<?php echo $r[0][4];?>"/></div> 35 <div>租贷类型:<input type="text" name="RentType" value="<?php echo $r[0][5];?>"/></div> 36 <div>房屋类型:<input type="text" name="HouseType" value="<?php echo $r[0][6];?>"/></div> 37 38 39 <div><input type="submit" value="更新"/></div> 40 41 42 </form> 43 <!--<a href="chakan.php"><input type="button" value="查看"></a>--> 44 </center> 45 </body> 46 </html>
【6】编辑的处理页面-------bianjichuli.php
1 <?php 2 3 //使用加载类 4 5 include("DBDA.class.php"); 6 $db = new DBDA(); 7 $id=$_POST["id"];//传ID 8 $KeyWord = $_POST["KeyWord"]; 9 $Area = $_POST["Area"]; 10 $SquareMeter = $_POST["SquareMeter"]; 11 $Rent = $_POST["Rent"]; 12 $RentType = $_POST["RentType"]; 13 $HouseType = $_POST["HouseType"]; 14 $sql="update house set KeyWord=‘{$KeyWord}‘,Area=‘{$Area}‘,SquareMeter=‘{$SquareMeter}‘,Rent=‘{$Rent}‘,RentType=‘{$RentType}‘,HouseType=‘{$HouseType}‘ where id=‘{$id}‘";// where id=‘{$id} 15 16 //echo $sql; 17 18 $attr = $db->Query($sql,2); 19 //var_dump($attr); 20 if($attr) 21 { 22 header("location:liebiaoyemian.php"); 23 } 24 else 25 { 26 echo "修改失败"; 27 }
【7】添加数据页面--------tianjiashuju.php
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>添加页面</title> 6 7 8 </head> 9 <style> 10 .kong 11 { 12 margin:10px 0px 10px 0px; 13 vertical-align: 14 } 15 </style> 16 <body> 17 <form action="tianjiachili.php" method="post"> 18 19 <h3>添加房屋信息页面</h3> 20 21 <div class="kong"> 22 关键字: 23 <input type="text" name="KeyWord"/> 24 </div> 25 <div class="kong"> 26 区域: 27 <input type="text" name="Area"/> 28 </div> 29 <div class="kong"> 30 使用面积: 31 <input type="text" name="SquareMeter"/> 32 </div> 33 <div class="kong"> 34 租金: 35 <input type="text" name="Rent"> 36 </div> 37 <div class="kong"> 38 租赁类型: 39 <input type="text" name="RentType"/> 40 </div> 41 <div class="kong"> 42 房屋类型 43 <input type="text" name="HouseType"/> 44 </div> 45 <div> 46 <input type="submit" value="确定"/><!--插入信息--> 47 <a href="liebiaoyemian.php"><input type="button" value="返回主页" /></a> 48 </div> 49 </form> 50 51 </body> 52 </html>
【8】添加的处理 --------tianjiachili.php
1 <?php 2 //$id = $_POST["id"]; 3 $KeyWord = $_POST["KeyWord"]; 4 $Area = $_POST["Area"]; 5 $SquareMeter = $_POST["SquareMeter"]; 6 $Rent = $_POST["Rent"]; 7 $RentType = $_POST["RentType"]; 8 $HouseType = $_POST["HouseType"]; 9 10 //造连接对象 11 include("DBDA.class.php"); 12 $db=new DBDA(); 13 14 //写sql语句 15 $sql="insert into house values(‘‘,‘{$KeyWord}‘,‘{$Area}‘,‘{$SquareMeter}‘,‘{$Rent}‘,‘{$RentType}‘,‘{$HouseType}‘)"; 16 //执行语句 17 $r=$db->Query($sql,2);//($sql,2) 2代表增删改 错在了2上 18 19 if($r) 20 { 21 header("location:liebiaoyemian.php"); 22 } 23 else{ 24 echo "执行失败!"; 25 } 26 ?>
【9】多条件查询------duotiaojianchaxun.php
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>租房子----多条件查询</title> 6 </head> 7 8 <body> 9 10 <body> 11 <form action="duotiaojianchaxun.php" method="post"> 12 <div>区域:<input type="checkbox" onclick="CheckAll(this,‘qy‘)" />全选</div> 13 <div> 14 <?php 15 include("DBDA.class.php"); 16 $db = new DBDA(); 17 18 $sqlqy = "select distinct Area from house"; 19 $attrqy = $db->Query($sqlqy); 20 //var_dump($attrqy); 21 22 foreach($attrqy as $v) 23 { 24 echo "<input class=‘qy‘ type=‘checkbox‘ value=‘{$v[0]}‘ name=‘qy[]‘/>{$v[0]} "; 25 } 26 ?> 27 </div><br /> 28 29 <div>租赁类型:<input type="checkbox" onclick="CheckAll(this,‘zl‘)" />全选</div> 30 <div> 31 <?php 32 33 $sqlzl = "select distinct RentType from House"; 34 $attrzl = $db->Query($sqlzl); 35 36 foreach($attrzl as $v) 37 { 38 echo "<input class=‘zl‘ type=‘checkbox‘ value=‘{$v[0]}‘ name=‘zl[]‘/>{$v[0]} "; 39 } 40 ?> 41 </div><br /> 42 43 <div>房屋类型:<input type="checkbox" onclick="CheckAll(this,‘fw‘)" />全选</div> 44 <div> 45 <?php 46 47 $sqlfw = "select distinct HouseType from House"; 48 $attrfw = $db->Query($sqlfw); 49 //var_dump($attrqy); 50 51 foreach($attrfw as $v) 52 { 53 echo "<input class=‘fw‘ type=‘checkbox‘ value=‘{$v[0]}‘ name=‘fw[]‘/>{$v[0]} "; 54 } 55 ?> 56 </div><br /> 57 58 <div>关键字:<input type="text" name="keyword" id="key" /> 59 </form> 60 <br /> 61 <input type="submit" value="搜索" /> 62 63 <br /> 64 <br /> 65 <br /> 66 67 <table cellpadding="1" cellspacing="1" border="1" width="100%"> 68 <tr> 69 <td>关键字</td> 70 <td>区域</td> 71 <td>面积</td> 72 <td>租金</td> 73 <td>租赁类型</td> 74 <td>房屋类型</td> 75 </tr> 76 77 <?php 78 79 $tj = ""; 80 $tj1 = "1=1"; 81 $tj2 = "1=1"; 82 $tj3 = "1=1"; 83 $tj4 = "1=1"; 84 85 if(!empty($_POST["qy"])) 86 { 87 $attr = $_POST["qy"]; 88 $str = implode("‘,‘",$attr); 89 $tj1 = " Area in (‘{$str}‘)"; 90 } 91 92 if(!empty($_POST["zl"])) 93 { 94 $attr = $_POST["zl"]; 95 $str = implode("‘,‘",$attr); 96 $tj2 = " RentType in (‘{$str}‘)"; 97 } 98 99 if(!empty($_POST["fw"])) 100 { 101 $attr = $_POST["fw"]; 102 $str = implode("‘,‘",$attr); 103 $tj3 = " HouseType in (‘{$str}‘)"; 104 } 105 106 if(!empty($_POST["keyword"])) 107 { 108 $attr = $_POST["keyword"]; 109 $tj3 = " keyword like ‘%{$attr}%‘"; 110 } 111 112 $tj = " Where {$tj1} and {$tj2} and {$tj3} and {$tj4}";//Where 前加空格 113 114 $sql = "select * from House".$tj; 115 $attrall = $db->Query($sql); 116 //var_dump($attrall); 117 118 foreach($attrall as $v) 119 { 120 echo "<tr> 121 <td>{$v[1]}</td> 122 <td>{$v[2]}</td> 123 <td>{$v[3]}</td> 124 <td>{$v[4]}</td> 125 <td>{$v[5]}</td> 126 <td>{$v[6]}</td> 127 </tr>"; 128 } 129 130 ?> 131 132 </table> 133 134 </body> 135 <script type="text/javascript"> 136 function CheckAll(a,b)//this表示该 137 { 138 var qx = a.checked; 139 var ck = document.getElementsByClassName(b); 140 for(var i =0;i<ck.length;i++) 141 { 142 ck[i].checked = qx; 143 } 144 } 145 146 </script> 147 148 </html> 149 </body> 150 </html>
PHP-----练习-------租房子-----增删改查,多条件查询
标签:
原文地址:http://www.cnblogs.com/yuyu1993/p/5598797.html