First, the topic requirements
Two, the topic procedure
1. Database creation
2. Encapsulation class file
<?phpclass dbda{public $fuwuqi = "localhost"; Server address Public $yonghuming = "root";//user name public $mima = "";//password public $dbconnect;//The Connection object//Operation database Method// $SQL represents the SQL statement that needs to be executed//$type represents the type of SQL statement, 1 for the query, 2 for the delete//$shujukuming for the name of the database///For a query, returns a two-dimensional array//if it is deleted or modified, returns TRUE or False function Query ($sql, $type =1, $shujukuming = "House") {//Build Connection object $this->dbconnect = new Mysqli ($this-& Gt;fuwuqi, $this->yonghuming, $this->mima, $shujukuming); Determine if error if (!mysqli_connect_error ()) {//If the connection is successful, execute SQL statement $result = $this->dbconnect- >query ($sql); Judging if ($type ==1) {//If it is a query statement, returns a two-dimensional array return $result->fetch_all ( ); } else {//If it is a different statement, return TRUE or false to return $result; }} else {return "Connection failed"; } }}?>
3. Rent House Home
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<?phpinclude ("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]} ' > Edit </a></td><td><a href= ' shanchuchuli.php?id={$v [0]} ' onclick=\ ' return confirm (' OK delete ') \ "> Delete </a></td></tr>";}? >
</table><br/><br/><a href= "tianjiashuju.php" ><input type= "button" value= "Add Data"/> </a><a href= "duotiaojianchaxun.php" ><input type= "button" value= "Search query"/></a></title> </body>
(4) Delete Data processing page
<?php$id = $_get["id"];var_dump ($newsid); include ("DBDA.class.php"); $dx =new Dbda (); $sql = "Delete from house where ID = ' {$id} '; $r = $dx->query ($sql, 2); if ($r) { header ("location:liebiaoyemian.php");} else{ echo "Delete failed! ";}
5. Edit Page
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<?php$id = $_get["id"];include ("DBDA.class.php"), $dx =new Dbda ();//echo "id";//var_dump ($id); $sql = "SELECT * FROM house where id= ' {$id} '; $r = $dx->query ($sql);? >
<form action= "bianjichuli.php" method= "POST" ><input type= "hidden" name= "id" value= "<?php echo $r [0][ 0];? > "/><!--ID passed id--><div> keyword: <input type=" text "name=" KeyWord "value=" <?php echo $r [0][1];? > "/></div><div> zone: <input type=" text "name=" area "value=" <?php echo $r [0][2];? > "/></div><div> area: <input type=" text "name=" Squaremeter "value=" <?php echo $r [0][3];? > "/></div><div> Rent: <input type=" text "name=" Rent "value=" <?php echo $r [0][4];? > "/></div><div> loan Type: <input type=" text "name=" Renttype "value=" <?php echo $r [0][5];? > "/></div><div> House Type: <input type=" text "name=" Housetype "value=" <?php echo $r [0][6];? > "/></div><div><input type=" Submit "value=" Update "/></div></form><!--<a href= "chakan.php" ><input type= "button" value= "View" ></a>--></center></body>
6. Edit Processing page
<?php//using the Load class include ("DBDA.class.php"); $db = new Dbda (); $id =$_post["id"];//Pass id $KeyWord = $_post["KeyWord"]; $Area = $_post["area"]; $SquareMeter = $_post["Squaremeter"]; $Rent = $_post["Rent"]; $RentType = $_post["Renttype"]; $HouseType = $_post["Housetype"]; $sql = "Update house set keyword= ' {$KeyWord} ', Area= ' {$Area} ', squaremeter= ' {$ Squaremeter} ', rent= ' {$Rent} ', renttype= ' {$RentType} ', housetype= ' {$HouseType} ' where id= ' {$id} ';//Where id= ' {$id}/ /echo $sql; $attr = $db->query ($sql, 2); Var_dump ($attr); if ($attr) { header ("location:liebiaoyemian.php"); } else { echo "modification failed"; }
7. Add Data page
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">8. Add Data processing page
<?php//$id = $_post["id"]; $KeyWord = $_post["KeyWord"]; $Area = $_post["area"]; $SquareMeter = $_post["Squaremeter"]; $Rent = $_post["Rent"]; $RentType = $_post["Renttype"]; $HouseType = $_post["Housetype"];//made Connection object include (" DBDA.class.php "); $db =new Dbda ();//write SQL statement $sql=" INSERT into house values (', ' {$KeyWord} ', ' {$Area} ', ' {$SquareMeter} ', ' {$Rent} ', ' {$RentType} ', ' {$HouseType} ') ';//EXECUTE Statement $r= $db->query ($sql, 2);//($sql, 2) 2 for adding or deleting errors on 2 if ($r) { header ("location:liebiaoyemian.php");} else{ echo "Execution failed! ";} ? >
9. Search page (multi-criteria query)
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<?phpinclude ("DBDA.class.php"); $db = new Dbda (); $sqlqy = "SELECT distinct area from house"; $attrqy = $db->query ($sqlqy);//var_dump ($attrqy); foreach ($attrqy as $v) { echo "<input class= ' qy ' type= ' checkbox ' value= ' {$v [0]} ' name= ' qy[] '/>{$v [0]}"; }? >
</div><br/> <div> Leasing Type: <input type= "checkbox" onclick= "Checkall (This, ' Zl ')"/> Select all </ Div><div>
<?php $sqlzl = "SELECT distinct Renttype from house"; $attrzl = $db->query ($SQLZL); foreach ($attrzl as $v) { echo "<input Class= ' Zl ' type= ' checkbox ' value= ' {$v [0]} ' name= ' zl[] '/>{$v [0]}"; }? >
</div><br/> <div> House type: <input type= "checkbox" onclick= "Checkall (This, ' FW ')" /> Select all </ Div><div>
<?php $SQLFW = "SELECT distinct housetype from house"; $ATTRFW = $db->query ($SQLFW);//var_dump ($attrqy); foreach ($ATTRFW as $v) { echo "<input class= ' fw ' type= ' checkbox ' value= ' {$v [0]} ' name= ' fw[] '/>{$v [0]}"; }? >
</div><br/> <div> Keywords: <input type= "text" name= "keyword" id= "key"/></form><br/> <input type= "Submit" value= "search"/> <br/><br/><br/> <table cellpadding= "1" cellspacing= "1" b Order= "1" width= "100%" > <tr> <td> keywords </td> <td> zone </td> <td > Area </td> <td> rent </td> <td> lease type </td> <td> House type </td> </tr>
<?php $TJ = ""; $TJ 1 = "1=1"; $TJ 2 = "1=1"; $TJ 3 = "1=1"; $TJ 4 = "1=1"; if (!empty ($_post["qy")) {$attr = $_post["qy"]; $str = Implode ("', '", $attr); $TJ 1 = "area in (' {$str} ')"; } if (!empty ($_post["ZL"])) {$attr = $_post["ZL"]; $str = Implode ("', '", $attr); $TJ 2 = "Renttype in (' {$str} ')"; } if (!empty ($_post["FW"])) {$attr = $_post["FW"]; $str = Implode ("', '", $attr); $TJ 3 = "Housetype in (' {$str} ')"; } if (!empty ($_post["keyword")) {$attr = $_post["keyword"]; $TJ 3 = "keyword like '%{$attr}% '";} $TJ = "Where {$TJ 1} and {$TJ 2} and {$TJ 3} and {$TJ 4}";//where with a space $sql = "SELECT * from House". $tj; $attrall = $db->q Uery ($sql);//var_dump ($attrall); foreach ($attrall as $v) {echo <tr> <td>{$v [1]}</td> <td>{$v [2]}</TD&G T <td>{$v [3]}</td> <td>{$v [4]}</td> <td>{$v [5]}</td> &L t;td>{$v [6]}</td> </tr> "; } ?>
</table> </body><script type= "Text/javascript" >function Checkall (A, B)//this indicates that the { var qx = a.checked; var ck = Document.getelementsbyclassname (b); for (var i =0;i<ck.length;i++) { ck[i].checked = QX; }} </script>
PHP Rent House Practice