数据查询:
查询页面:search.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>商品订单查询</title>
</head>
<body>
<h2>商品订单查询</h2>
<form action="results.php" method="post">
<label>选择查询类型:</label>
<select name="searchtype">
<option value="author">Author</option>
<option value="title">Title</option>
<option value="isbn">ISBN</option>
</select>
<br/>
<label>输入查询关键字:</label>
<input name="searchterm" type="text" size="40"/>
<br/>
<input type="submit" name="submit" value="查询"/>
</form>
</body>
</html>
查询结果页面:results.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>订单查询结果</title>
</head>
<body>
<h2>商品订单查询结果</h2>
<?php
//获取并过滤用户输入的数据
$searchType=$_POST['searchtype'];
$searchTerm=trim($_POST['searchterm']);
//如果用户没有输入任何内容时提示
if(!$searchType||!$searchTerm){
echo '您没有输入任何搜索内容,请返回后再次输入';
exit;
}
//去掉所有临时字符,对数据进行格式过滤
if(!get_magic_quotes_gpc()){
$searchType=addslashes($searchType);
$searchTerm=addslashes($searchTerm);
}
//连接数据库,@是错误抑制操作符
@ $db=new mysqli('localhost','root','','books');
//如果连接数据库出现异常时提示
if(mysqli_connect_errno()){
echo '无法连接数据库,请稍后再试';
exit;
}
//定义SQL语句并执行
$query="SELECT * FROM books WHERE ".$searchType." LIKE '%".$searchTerm."%'";
$result=$db->query($query);
//获取查询结果的数量
$num_results=$result->num_rows;
echo "<p>找到的书籍数量为:".$num_results."</p>";
for($i=0;$i<$num_results;$i++){
//获取结果中的每一行
$row=$result->fetch_assoc();
echo "<p><strong>"
.($i+1)
." Title:"
.htmlspecialchars(stripslashes($row['title']))
."</strong><br/>Author:."
.stripslashes($row['author'])
."<br/>ISBN:"
.stripslashes($row['isbn'])
."<br/>Price:"
.stripslashes($row['price'])
."</p>";
}
//释放结果集
$result->free();
//断开数据库连接
$db->close();
?>
</body>
</html>
添加数据页面:newbook.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>添加新书</title>
</head>
<body>
<h2>添加新书</h2>
<form action="insert_book.php" method="post">
<label>ISBN</label><input type="text" name="isbn" maxlength="13" size="13"/>
<label>Author</label><input type="text" name="author" maxlength="30" size="30"/>
<label>Title</label><input type="text" name="title" maxlength="60" size="30"/>
<label>Price $</label><input type="text" name="price" maxlength="7" size="7"/>
<input type="submit" value="提交">
</form>
</body>
</html>
添加之后的结果页面:insert_book.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加书籍后的结果</title>
</head>
<body>
<h2>添加书籍后的结果</h2>
<?php
//获取用户输入的数据
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];
//如果用户输入的数据为空时提示
if(!$isbn || !$author || !$title || !$price){
echo "您有其中一项没有输入任何数据,请返回并重试。";
exit;
}
//去掉所有临时字符,对数据进行格式过滤
if(!get_magic_quotes_gpc()){
$isbn=addslashes($isbn);
$author=addslashes($author);
$title=addslashes($title);
$price=doubleval($price);
}
//连接数据库
@ $db=new mysqli('localhost','root','','books');
//数据库连接异常提示
if(mysqli_connect_errno()){
echo '无法连接数据库,请稍后再试';
exit;
}
/**
*
//定义SQL语句并执行
$query="INSERT INTO books VALUES ('".$isbn."','".$author."','".$title."','".$price."')";
$result=$db->query($query);
//添加是否成功给予提示
if($result){
echo $db->affected_rows."书籍已经添加到数据库";
}else{
echo "添加异常,书籍没有添加成功";
}
//释放结果集
$result->free();
//断开数据库连接
$db->close();
*/
/**
* 用Prepared语句
*/
$query="INSERT INTO books VALUES (?,?,?,?)";
$stmt=$db->prepare($query);
$stmt->bind_param("sssd",$isbn,$author,$title,$price);
$stmt->execute();
echo $stmt->affected_rows.'本书添加到数据库';
$stmt->close();
?>
</body>
</html>