PHP實現MYSQL簡單的增刪改查功能
阿新 • • 發佈:2019-01-28
home.php | 查詢:serch.php | 查詢結果:search_result.php |
(page.inc) | 增加資料:release.php | 插入過程和展示:insert_book |
(database.inc) | 展示:managebook.php,刪除:deletebook.php | 更改:editbook.php更改過程:editto.php |
1.home.php
2.page.inc<? require("page.inc"); $homepage=new page(); $homepage->content="Welcome to my page."; $homepage->display(); ?>
3.database.inc<?php header("Content-type: text/html; charset=utf-8"); class page{ public $content; public $title="Luhao's test web."; public $keywords="luhao,test,handsome"; public $button=array("home"=>"home.php", "contact"=>"contact.php", "service"=>"services.php", "sitemap"=>"sitemap.php", "news"=>"news.php", "search"=>"search.php", "release"=>"release.php", "managebook"=>"managebook.php", ); public function __set($name,$value){ $this->attribute=$value; }//感覺這個可以省略 public function display(){ echo "<html>\n<head>\n"; $this->displaytitle(); $this->displaykeywords(); $this->displaystyle(); echo "</head>\n<body>\n"; $this->displayheader(); $this->displaymenu($this->button); echo $this->content; $this->displayfooter(); } public function displaytitle(){ echo "<title>".$this->title."</title>"; } public function displaykeywords(){ echo "<meta name=\"keywords\" content=\"".$this->keywords."\">"; } public function displaystyle(){ echo " <style> h1 { color:white; font-size:24pt; text-align:center; font-family:arial,sans-serif } .menu { color:white; font-size:12pt; text-align:center; font-family:arial,sans-serif; font-weight:bold } td { background:black } p { color:black; font-size:12pt; text-align:justify; font-family:arial,sans-serif } p.foot { color:white; font-size:9pt; text-align:center; font-family:arial,sans-serif; font-weight:bold } a:link,a:visited,a:active { color:white } .footer { bottom:0px; position:absolute } </style>"; } public function displayheader(){ ?> <table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr bgcolor="#000000"> <td align="left"><img src="img/logo.gif" /></td> <td><h1>Luhao's Test Web</h1></td> <td align="right"><img src="img/logo.gif" /></td> </tr> </table> <? } public function displaymenu($button){ echo "<table width=\"100%\" bgcolor=\"#FFFFFF\" cellpadding=\"4\" cellspacing=\"4\">\n<tr>\n"; $width=100/count($button); while (list($name,$url)=each($button)){ $this->displaybutton($width,$name,$url,$this->isurlcurrentpage($url)); } echo"</tr>\n</table>\n"; } public function isurlcurrentpage($url){//判斷是否是當前頁面 if(strpos($_SERVER['PHP_SELF'],$url)==false){ return false; }else{ return true; } } public function displaybutton($width,$name,$url,$active=false){ if($active==false){ echo "<td width=\"".$width."%\"><a href=\"".$url."\"><img src=\"img/s-logo.gif\" border=\"0\" /><span class=\"menu\">".$name."</span></a></td>"; }elseif($active==true){ echo "<td width=\"".$width."%\"><img src=\"img/side-logo.gif\" border=\"0\" /><span class=\"menu\">".$name."</span></td>"; } } public function displayfooter(){ ?> <table width="100%" bgcolor="black" cellpadding="12" border="0" class="footer"> <tr> <td> <p class="foot">© LuHao's WEB.</p> <p class="foot">Please see my <a href ="aboutme.html">legal information page</a></p> </td> </tr> </table> </body></html> <? } } class page2 extends page{ public function display(){ echo "<html>\n<head>\n"; $this->displaytitle(); $this->displaykeywords(); $this->displaystyle(); echo "</head>\n<body>\n"; $this->displayheader(); $this->displaymenu($this->button); echo $this->content; } } ?>
<?
global $lu_host;
global $lu_user;
global $lu_psw;
global $lu_db;
$lu_host='localhost';
$lu_user='lustudy_web';
$lu_psw='123456';
$lu_db='lustudy';
?>
4.搜尋頁
5.搜尋頁的處理和顯示搜尋結果<? require("page.inc"); $searcher=new page(); $searcher->content="<font size=\"+5\">Book-O-Rama Catalog Search</font><br /> <form action=\"search_result.php\" method=\"post\"> Choose Search Type<br /> <select name=\"search_type\"> <option value=\"Author\">Author</option><br /> <option value=\"Title\">Title</option><br /> <option value=\"ISBN\">ISBN</option><br /> </select><br /> Enter search Term:<br /> <input name=\"searchterm\" type=\"text\" size=\"40\" /><br /> <input name=\"submit\" value=\"Search\" type=\"submit\"> </form> "; $searcher->display(); ?>
<?
require("page.inc");
require("database.inc");
$search_result=new page();
$search_type=$_POST['search_type'];
$searchterm=trim($_POST['searchterm']);
$content="";
if(!get_magic_quotes_gpc()){
$searchterm=addslashes($searchterm);
$search_type=addslashes($search_type);
}
if($searchterm==''){
$search_result->content="Please Input The Search Iterm";
$search_result->display();
return;
}
@$con=new mysqli($lu_host,$lu_user,$lu_psw,$lu_db);
//或者上面兩行寫成 $con=new sqli('localhost','lustudy_web','123456',';lustudy')
if(mysqli_connect_error()){
$search_result->content= "ERROR:Can not connect database.Please Try Again Later";
$search_result->display();
exit;
}
@$con->select_db($lu_db);
$sqlstr="select * from books where ".$search_type." like '%".$searchterm."%'";//該語句不需要分號
//echo $sqlstr;
$web_reasult=$con->query($sqlstr);
$reasult_num=$web_reasult->num_rows;
$content.= "Number of books found : ".$reasult_num."<br /><br />";
//if($reasult_num==0){exit;}
for($i=0;$i<$reasult_num;$i++){//什麼時候用逗號什麼時候用分號來著
$row=$web_reasult->fetch_assoc();
$content.= ($i+1)." Title: ".$row['title']."<br />
Athor : ".$row['author']."<br />
ISBN : ".$row['isbn']."<br />
Price : ".$row['price']."<br /><br />";
}
$content.="<a href=\"search.php\"><font color=\"#330000\">返回</font></a><br /><br />";
$web_reasult->free();
$con->close();
$search_result->content=$content;
$search_result->display();
?>
6.全部資訊展示頁
<?
require("page.inc");
require("database.inc");
$managebook=new page2();
$content="";
$serchsrt="select isbn,title,author,price from books";
@$con=new mysqli($lu_host,$lu_user,$lu_psw,$lu_db);
if(mysqli_connect_error()){
$managebook->display();
echo "Database Connet Error.";
$managebook->displayfooter();
exit;
}
$result=$con->query($serchsrt);
$numrow=$result->num_rows;//注意這個地方不帶括號
//echo $numrow;
$managebook->display();
?>
<style type="text/css">
.bookall td{
background-color:#000000;
color:#FFFFFF;
}
</style>
<h1><center><font color="#000000">All Books Here</font></center></h1><br />
<table class="bookall" width="80%" align="center">
<tr>
<td>ID</td>
<td>Title</td>
<td>ISBN</td>
<td>Author</td>
<td>Price($)</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?
for($i=0;$i<$numrow;$i++){
$row=$result->fetch_assoc();
echo "<tr><td>".($i+1)."</td><td>".$row['title']."</td><td>".$row['isbn']."</td><td>".$row['author']."</td><td>".$row['price']."</td><td><a href=\"editbook.php?isbn=".$row['isbn']."\">Edit</a></td><td><a href=\"deletebook.php?isbn=".$row['isbn']."\">Delete</a></td>";
}
echo "</table>";
$result->free();
$con->close();
$managebook->displayfooter();
?>
7.從展示頁點選刪除按鈕,和刪除的過程
<?
require("page.inc");
require("database.inc");
$con=new mysqli($lu_host,$lu_user,$lu_psw,$lu_db);
$isbn=$_GET['isbn'];
//echo $isbn;
$deletestr="delete from books where isbn='".$isbn."'";
echo $deletestr;
$result=$con->query($deletestr);
if(!$result){
$deletebook=new page;
$deletebook->content="Delete False.";
$deletebook->display();
$deletebook->displayfooter();
exit;
}
//echo "<meta http-equiv=\"Refresh\" content=\"0\"; url=\"managebook.php\">";
header("Location:managebook.php");
?>
8.從展示頁進入修改頁面頁<?
require("page.inc");
require("database.inc");
$con=new mysqli($lu_host,$lu_user,$lu_psw,$lu_db);
$isbn=$_GET['isbn'];
//echo $isbn;
$selectstr="select title,isbn,author,price from books where isbn='".$isbn."'";
//echo $selectstr;
$result=$con->query($selectstr);
if(!$result){
$deletebook=new page;
$deletebook->content="Select False.";
$deletebook->display();
$deletebook->displayfooter();
exit;
}
$row=$result->fetch_assoc();
//echo "<meta http-equiv=\"Refresh\" content=\"0\"; url=\"managebook.php\">";
//header("Location:managebook.php");
$editbook=new page2();
$editbook->display();
?>
<style type="text/css">
.editbook tr td{
background-color:#FFF;
}
</style>
<h1><font color="#333333">Edit The Book</font></h1>
<form action="editto.php" method="post" >
<table border="0" cellpadding="1" align="center" cellspacing="30px" class="editbook">
<tr>
<td>ISBN:</td>
<td><input name="isbn" size="40" value="<? echo $row['isbn'];?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Author:</td>
<td><input name="author" size="40" value="<? echo $row['author'];?>"/></td>
</tr>
<tr>
<td>Title:</td>
<td><input name="title" size="50" value="<? echo $row['title'];?>"/></td>
</tr>
<tr>
<td>Price($):</td>
<td><input name="price" size="10" value="<? echo $row['price'];?>"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
<?
$result->free();
$con->close();
$editbook->displayfooter();
?>
9.修改頁面的處理頁和修改後頁面的展示頁<?
require("page.inc");
require("database.inc");
echo $_POST['isbn'];
$isbn=trim($_POST['isbn']);
$title=trim($_POST['title']);
$author=trim($_POST['author']);
$price=trim($_POST['price']);
$editto=new page2();
if(!get_magic_quotes_gpc()){
$isbn=addslashes($isbn);
$title=addslashes($title);
$price=addslashes($price);
$author=addslashes($author);
}
if(!preg_match("/^[0-9]-[0-9]{3}-[0-9]{5}-[0-9]$/",$isbn)){
$inserbook->display();
echo "$isbn ISBN Format Wrong!";
$inserbook->displayfooter();
exit;
}
if(!is_numeric($price)){
$editto->display();
echo "$price Price Format Wrong!";
$editto->displayfooter();
exit;
}
@$con=new mysqli($lu_host,$lu_user,$lu_psw,$lu_db);
//或者上面兩行寫成 $con=new sqli('localhost','lustudy_web','123456',';lustudy')
if(mysqli_connect_error()){
$inserbook->display();
echo "Databasa connect error!Please Try Again Later.";
$inserbook->displayfooter();
exit;
}
$con->select_db($lu_db);
$updatestr="update books set title='".$title."',author='".$author."',price=".$price." where isbn='".$isbn."'";
$result=$con->query($updatestr);
if(!$result){
$editto->display();
echo "更改失敗!";
$editto->displayfooter();
exit;
}
header("Location:editbook.php?isbn=".$isbn);
?>
10.插入新資料頁面
<?
require("page.inc");
$release=new page2();
$release->display();
?>
<style type="text/css">
.releasetable tr td{
background-color:#FFF;
}
</style>
<h1><font color="#333333">Book-O-Rama-New Book Entry</font></h1>
<form action="insert_book.php" method="post" class="releasetable">
<table border="0" cellpadding="1" align="center" cellspacing="30px">
<tr>
<td>ISBN:</td>
<td><input name="isbn" size="40"/></td>
</tr>
<tr>
<td>Author:</td>
<td><input name="author" size="40" /></td>
</tr>
<tr>
<td>Title:</td>
<td><input name="title" size="50" /></td>
</tr>
<tr>
<td>Price($):</td>
<td><input name="price" size="10" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Resister"></td>
</tr>
</table>
</form>
<?
$release->displayfooter();
?>
11.插入新資料之後的展示頁
<?
require("page.inc");
require("database.inc");
$inserbook=new page2;
$isbn=trim($_POST['isbn']);
$title=trim($_POST['title']);
$author=trim($_POST['author']);
$price=trim($_POST['price']);
if(!get_magic_quotes_gpc()){
$isbn=addslashes($isbn);
$title=addslashes($title);
$price=addslashes($price);
$author=addslashes($author);
}
if(!preg_match("/^[0-9]-[0-9]{3}-[0-9]{5}-[0-9]$/",$isbn)){
$inserbook->display();
echo "$isbn ISBN Format Wrong!";
$inserbook->displayfooter();
exit;
}
if(!is_numeric($price)){
$inserbook->display();
echo "$price Price Format Wrong!";
$inserbook->displayfooter();
exit;
}
@$con=new mysqli($lu_host,$lu_user,$lu_psw,$lu_db);
//或者上面兩行寫成 $con=new sqli('localhost','lustudy_web','123456',';lustudy')
if(mysqli_connect_error()){
$inserbook->display();
echo "Databasa connect error!Please Try Again Later.";
$inserbook->displayfooter();
exit;
}
$con->select_db($lu_db);
//此處應當加一個判斷
$insertsql="insert into books values ('".$isbn."','".$author."','".$title."','".$price."')";
//echo $insertsql;
$result1=$con->query($insertsql);
$inserbook->display();
if(!$result1){echo "插入失敗,有重複值。";$inserbook->displayfooter();exit;}
echo "資料插入成功!<br />您插入的資料為:<br />";
$selectstr="select title,author,price from books where isbn='$isbn'";
//echo $selectstr;
$result2=$con->query($selectstr);
//此處加一個判斷“抱歉無法獲得您剛才插入的資料”
$row=$result2->fetch_assoc();
echo "TiTle:《".$row['title']."》<br />";
echo "ISBN:".$isbn."<br />";
echo "Author:".$row['author']."<br />";
echo "Price:".$row['price']."<br />";
$inserbook->displayfooter();
?>