2018-2-8 租房信息的增刪改和搜索
阿新 • • 發佈:2018-02-08
prim sql語句 idt erro 獲取 aar 方法 文檔 htm
建立一個租房信息的增刪改和搜索:
首先在數據庫中建表zufang:
代碼如下:
create table zufang( id int auto_increment primary key, Keyword int comment "關鍵字", Area Varchar(50) comment"所屬區域", SquareMeter int comment"使用面積(平方米)", Rent float comment"租金(每月)", RenType Varchar(50) comment"租賃類型", HouseType varchar(50) comment"房屋類型" )charset utf8;
展示租房信息的頁面:
代碼:
<!--展示租房信息的頁面--> <?php //連接數據庫 $db = new MySQLi("localhost","root","","z_zf"); !mysqli_connect_error() or die("連接錯誤"); $db->query("set names utf8");//設置字符集 $sql = "select * from zufang";//sql語句,查詢zufang表的內容 $result = $db->query($sql);//執行sql語句,得到結果集 $arr = $result->fetch_all();//將結果集轉為數組 ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>無標題文檔</title> </head> <body> <table border="1" width="100%"> <tr> <th>關鍵字</th> <th>區域</th> <th>使用面積</th> <th>租金</th> <th>租賃類型</th> <th>房屋類型</th> <th>操作</th> </tr> <!--遍歷數組,插入數據庫中的信息--> <?php foreach($arr as $v){ ?> <tr> <td><?php echo $v[1] ?></td> <td><?php echo $v[2] ?></td> <td><?php echo $v[3] ?></td> <td><?php echo $v[4] ?></td> <td><?php echo $v[5] ?></td> <td><?php echo $v[6] ?></td> <td> <form action="adminChuli.php" method="post"> <!--添加屬性name,之後判斷執行刪除操作--> <input type="hidden" name="type" value="del"> <input type="hidden" name="id" value="<?php echo $v[0]; ?>"> <button>刪除</button> </form> <!--添加屬性name,之後判斷執行編輯操作--> <form action="add.php" method="post"> <input type="hidden" name="type" value="mod"> <input type="hidden" name="id" value="<?php echo $v[0]; ?>"> <button>編輯</button> </form> </td> </tr> <?php }?> </table> <a href="add.php"> <button >添加數據</button> </a> </body> </html>
添加租房信息的頁面:
代碼:
<!--添加租房信息的頁面--> <?php $db = new MySQLi("localhost","root","","z_zf"); !mysqli_connect_error() or die("連接錯誤"); $db->query("set names utf8"); $arr = array(); if($_POST){ $id = $_POST["id"]; $type = $_POST["type"]; $sql = "select id,keyword,area,squareMeter,rent,rentype,housetype from zufang where id = $id "; $result = $db->query($sql); $arr = $result->fetch_row(); } $areaArr = array("板井","三義廟","上上城","天安門東","中關村"); $zlTypeArr = array("床位","短租","整租"); $houseTypeArr = array("1室1廳","2室1廳","3室2廳","4室2廳","平房","筒子樓"); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>添加數據頁面</title> </head> <body> <fieldset> <legend>添加房屋租賃信息</legend> <form action="adminChuli.php" method="post"> <input type="hidden" name="type" value="<?php echo $type != "" ? $type : "add"?>"> <input type="hidden" name="id" value="<?php echo $id != "" ? $id : ""?>"> <table> <tr> <td>關鍵字</td> <td> <input type="text" name="kword" value="<?php echo $arr[1] != "" ? $arr[1] : ""?>"> </td> </tr> <tr> <td>區域</td> <td> <select name="area"> <?php foreach($areaArr as $v){ if($arr[2] == $v){ echo "<option selected>$v</option>"; }else{ echo "<option >$v</option>"; } } ?> </select> </td> </tr> <tr> <td>使用面積</td> <td> <input type="text" name="usearea" value="<?php echo $arr[3] != "" ? $arr[3] : ""?>"> </td> </tr> <tr> <td>租金</td> <td> <input type="text" name="rent" value="<?php echo $arr[4] != "" ? $arr[4] : ""?>"> </td> </tr> <tr> <td>租賃類型</td> <td> <select name="zltype" > <?php foreach($zlTypeArr as $v){ if($arr[5] == $v){ echo "<option selected>$v</option>"; }else{ echo "<option >$v</option>"; } } ?> </select> </td> </tr> <tr> <td>房屋類型</td> <td> <select name="housetype" > <?php foreach($houseTypeArr as $v){ if($arr[6] == $v){ echo "<option selected>$v</option>"; }else{ echo "<option >$v</option>"; } } ?> </select> </td> </tr> </table> <button>提交</button> </form> </fieldset> </body> </html>
信息的處理頁面:
<!--信息的處理頁面--> <?php //連接數據庫 $db = new MySQLi("localhost","root","","z_zf"); !mysqli_connect_error() or die("連接錯誤"); $db->query("set names utf8"); $type = $_POST["type"]; switch($type){ /*如果獲取到的name屬性值是add,執行添加數據的操作*/ case "add": $kword = $_POST["kword"]; //關鍵字 $area = $_POST["area"]; // 區域 $usearea = $_POST["usearea"]; //使用面積 $rent = $_POST["rent"]; //租金 $zltype = $_POST["zltype"]; //租賃類型 $housetype = $_POST["housetype"]; //房屋類型 //sql語句添加zufang表的信息 $sql = "insert into zufang(keyword,area,squareMeter,rent,rentype,housetype) values(‘$kword‘,‘$area‘,‘$usearea‘,‘$rent‘,‘$zltype‘,‘$housetype‘)"; $result = $db->query($sql); //執行sql語句,轉為結果集 header("location:show_admin.php"); //跳轉頁面,展示租房信息 break; /*如果獲取到的name屬性值是del,執行刪除數據的操作*/ case "del": $id = $_POST["id"]; $sql = "delete from zufang where id = $id "; //sql語句刪除zufang表的信息 $result = $db->query($sql); //執行sql語句,轉為結果集 header("location:show_admin.php"); //跳轉頁面,展示租房信息 break; /*如果獲取到的name屬性值是mod,執行編輯數據的操作*/ case "mod": $id = $_POST["id"]; $kword = $_POST["kword"]; //關鍵字 $area = $_POST["area"]; // 區域 $usearea = $_POST["usearea"]; //使用面積 $rent = $_POST["rent"]; //租金 $zltype = $_POST["zltype"]; //租賃類型 $housetype = $_POST["housetype"]; //房屋類型 $sql = "update zufang set ". //sql語句編輯zufang表的信息 "keyword=‘$kword‘,". "area=‘$area‘,". "squareMeter=$usearea,". "rent=$rent,". "rentype=‘$zltype‘,". "housetype=‘$housetype‘ where id = $id"; $result = $db->query($sql); //執行sql語句,轉為結果集 header("location:show_admin.php"); //跳轉頁面,展示租房信息 break; }
搜索租房信息頁面:
代碼:
<!--這是搜索租房信息的頁面--> <?php //連接數據庫 $db = new MySQLi("localhost","root","","z_zf"); !mysqli_connect_error() or die("連接錯誤"); $db->query("set names utf8");//設置字符集 if($_POST){ //判斷條件,如果接收到值 //var_dump($_POST); $str = " where 1 = 1 "; //相當於true,恒等 if(!empty($_POST["quyu"])){ //如果接收到的quyu 的值不為空值 $areaArr = implode("‘,‘",$_POST["quyu"]); //數組轉為字符串 $str .= " and area in(‘$areaArr‘) "; } if(!empty($_POST["zltype"])){ $zltypeArr = implode("‘,‘",$_POST["zltype"]); //數組轉為字符串 $str .= " and rentype in(‘$zltypeArr‘) "; } if(!empty($_POST["housetype"])){ $housetypeArr = implode("‘,‘",$_POST["housetype"]); //數組轉為字符串 $str .= " and housetype in(‘$housetypeArr‘) "; } if(!empty($_POST["kword"])){ $str .= " and keyword like ‘%".$_POST["kword"]."%‘ "; } $sql = "select * from zufang ".$str; /*sql語句後面接str*/ }else{ $sql = "select * from zufang"; } //var_dump($sql); $result=$db->query($sql);//執行sql語句,轉為結果集 $arr=$result->fetch_all(); $areaArr = array("板井","三義廟","上上城","天安門東","中關村"); //定義數組存區域內容 插入第48行 $zlTypeArr = array("床位","短租","整租"); //定義數組存租房類型內容 插入第55行 $houseTypeArr = array("1室1廳","2室1廳","3室2廳","4室2廳","平房","筒子樓");//定義數組存房屋類型內容 插入第62行 ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>無標題文檔</title> </head> <body> <form action="#" method="post"> 區域:<input type="checkbox" qxtype="quyu" onClick="qx(this)" >全選 <br> <?php foreach($areaArr as $v){ echo "<input class=‘quyu‘ type=‘checkbox‘ name=‘quyu[]‘ value=‘$v‘>$v "; //插入$areaArr數組內容 } ?> <br><br> 租賃類型:<input type="checkbox" qxtype="zityle" onClick="qx(this)" >全選 <br> <?php foreach($zlTypeArr as $v){ echo "<input class=‘zityle‘ type=‘checkbox‘ name=‘zltype[]‘ value=‘$v‘>$v "; //插入$zlTypeArr數組內容 } ?> <br><br> 房屋類型:<input type="checkbox" qxtype="housetype" onClick="qx(this)">全選 <br> <?php foreach($houseTypeArr as $v){ echo "<input class=‘housetype‘ type=‘checkbox‘ name=‘housetype[]‘ value=‘$v‘>$v "; //插入$houseTypeArr數組內容 } ?><br><br> 關鍵字 : <input type="text" name="kword"> <button>搜索</button> </form> <table border="1" width="100%"> <tr> <th>關鍵字</th> <th>區域</th> <th>使用面積</th> <th>租金</th> <th>租賃類型</th> <th>房屋類型</th> </tr> <?php foreach($arr as $v){ ?> <tr> <td><?php echo $v[1] ?></td> <td><?php echo $v[2] ?></td> <td><?php echo $v[3] ?></td> <td><?php echo $v[4] ?></td> <td><?php echo $v[5] ?></td> <td><?php echo $v[6] ?></td> </tr> <?php }?> </table> </body> </html> <script> /*定義方法,當點擊全選按鈕時,所有按鈕都被選中*/ function qx(obj){ var qxtype = obj.getAttribute("qxtype");//設置屬性 var inputArr = document.getElementsByClassName(qxtype);//定義變量,inputarr是所有帶有qxtype屬性的對象 //遍歷數組,點擊全選按鈕,全部選中 for(var i = 0;i<inputArr.length;i++){ inputArr[i].checked = obj.checked; } } </script>
2018-2-8 租房信息的增刪改和搜索