PHP实现MYSQL简单的增删改查功能

home.php查询:serch.php查询结果:search_result.php
(page.inc)增加数据:release.php插入过程和展示:insert_book
(database.inc)展示:managebook.php,删除:deletebook.php更改:editbook.php更改过程:editto.php
0.数据库






1.home.php

<?
require("page.inc");
$homepage=new page();
$homepage->content="Welcome to my page.";
$homepage->display();
?>
2.page.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">&copy; 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;
		}
	}
	
?>
3.database.inc

<?
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.搜索页

<?
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();
?>
5.搜索页的处理和显示搜索结果

<?
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();
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值