php+MySQL图书管理系统(四)

来源:互联网 发布:weui 模板源码 编辑:程序博客网 时间:2024/05/07 22:24

1.图书的查询

a.要求可以对书的 类别, 书名, 出版社, 年份(年份区间), 作者, 价格(区间) 进行查询. 每条图书信息包括以下内容:

( 书号, 类别, 书名, 出版社, 年份, 作者, 价格, 总藏书量, 库存 )

b.可选要求: 可以按用户指定属性对图书信息进行排序. (默认是书名)

这里遇到的最大的问题就是汉字的排序问题 由于错在中文 首先编码就是一个问题 一般我们会使用国际通用的

编码方式 UTF-8 ,但是使用这个进行排序的时候结果并不理想 如果使用GBK编码格式的话,得到的结果还是

比较理想的

但是如果改变一个数据库的编码格式又会带来不可预料的结果 所以尝试找一些折中的办法试一下

网上找了各种办法 我照着做了但是似乎并没有什么卵用 点击打开链接

<style type="text/css">table.hovertable {font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #999999;border-collapse: collapse;margin:20px 250px;width:800px;padding: 0px 0px;}table.hovertable th {background-color:#c3dde0;border-width: 1px;padding: 8px;border-style: solid;border-color: #a9c6c9;}table.hovertable tr {background-color:#d4e3e5;}table.hovertable td {border-width: 1px;padding: 8px;border-style: solid;border-color: #a9c6c9;}</style><?phpecho "<h1 style='text-align:center; margin:20px 0px;'><font color='#50b3ed'>图书查询结果</font></h1>";echo "<a style='margin:0px 900px; width:50px;' href='searchBook.php'><font color='#1111e8'>Return</font></a>";require("MySqlUtils.php");header("Content-type:text/html;charset=utf-8");$bookID = $_POST['bookNumber'];$classfication = $_POST['bookClass'];$bookName = $_POST['bookName'];$press = $_POST['press'];$publicTime = $_POST['date'];$author = $_POST['author'];$price = $_POST['price'];$numberAll = $_POST['number'];$numberNow = $_POST['numberNow'];$orderBy = $_POST['orderBy'];$PublicTimeEnd = $_POST['dateEnd'];$priceHight = $_POST['priceHight'];$numberAllHigth = $_POST['numberHight'];$numberNowHight = $_POST['numberNowHight'];changeClassfication();checkForm();$sql = "select * from book where";$isFirstNotNull = true;if ($bookID){$isFirstNotNull = false;$sql = $sql." BookID like ".$bookID."%";}if ($classfication){if ($isFirstNotNull){$sql = $sql." Classification = '".$classfication."'";}else{$sql = $sql." and Classification = '".$classfication."'";}$isFirstNotNull = false;}if ($bookName){if ($isFirstNotNull){$sql = $sql." BookName like '%".$bookName."%'";}else{$sql = $sql." and BookName like '%".$bookName."%'";}$isFirstNotNull = false;}if ($press){if ($isFirstNotNull){$sql = $sql." Press like '%".$press."%'";}else{$sql = $sql." and Press like '%".$press."%'";}$isFirstNotNull = false;}if ($publicTime){if (!$PublicTimeEnd){if ($isFirstNotNull){$sql = $sql." PublicTime = '".$publicTime."'";}else{$sql = $sql." and PublicTime = '".$publicTime."'";}}else{if ($isFirstNotNull){$sql = $sql." PublicTime between '".$publicTime."' and '".$PublicTimeEnd."'";}else{$sql = $sql." and PublicTime between '".$publicTime."' and '".$PublicTimeEnd."'";}}$isFirstNotNull = false;}if ($author){if ($isFirstNotNull){$sql = $sql." Author like '%".$author."%'";}else{$sql = $sql." and Author like '%".$author."%'";}$isFirstNotNull = false;}if ($price){if (!$priceHight){if ($isFirstNotNull){$sql = $sql." Price = '".$price."'";}else{$sql = $sql." and Price = '".$price."'";}}else{if ($isFirstNotNull){$sql = $sql." Price between '".$price."' and '".$priceHight."'";}else{$sql = $sql." and Price between '".$price."' and '".$priceHight."'";}}$isFirstNotNull = false;}if ($numberAll){if (!$numberAllHigth){if ($isFirstNotNull){$sql = $sql." NumberAll = '".$numberAll."'";}else{$sql = $sql." and NumberAll = '".$numberAll."'";}}else{if ($isFirstNotNull){$sql = $sql." NumberAll bewteen '".$numberAll."' and '".$numberAllHigth."'";}else{$sql = $sql." and NumberAll bewteen '".$numberAll."' and '".$numberAllHigth."'";}}$isFirstNotNull = false;}if ($numberNow){if (!$numberNowHight){if ($isFirstNotNull){$sql = $sql." StoreNumber = '".$numberNow."'";}else{$sql = $sql." and StoreNumber = '".$numberNow."'";}}else{if ($isFirstNotNull){$sql = $sql." StoreNumber between '".$numberNow."' and '".$numberNowHight."'";}else{$sql = $sql." and StoreNumber between '".$numberNow."' and '".$numberNowHight."'";}}$isFirstNotNull = false;}if ($isFirstNotNull){$sql = "select * from book";}$sql = $sql." order by ".$orderBy;//." CONVERT( name USING gbk ) COLLATE gbk_chinese_ci ASC";//echo $sql;$link = getLink();if($link){$resoures = getResoures('libray', $sql);if(!$resoures){echo "<Script>alert('查询失败 请检查查询设置或者联系系统管理员!');</Script>";echo "<Script>window.location.href='searchBook.php'</Script>";}$info = mysql_fetch_array($resoures);if(!$info){echo "<Script>alert('查询结果为空 请修改查询条件!');</Script>";}$countBook = 1;echo "<table class='hovertable'>";echo "<tr><th>数目</th>  <th>书号</th>  <th>类别</th>  <th>书名</th>  <th>出版社</th>  <th>年份</th>  <th>作者</th>  <th>价格</th>  <th>总藏书数量</th>  <th>库存量</th>  </tr>";while($info){echo "<tr>    <th>$countBook</th>    <th>$info[BookID]</th>    <th>$info[Classification]</th>    <th>$info[BookName]</th>    <th>$info[Press]</th>    <th>$info[PublicTime]</th>    <th>$info[Author]</th>    <th>$info[Price]</th>    <th>$info[NumberAll]</th>    <th>$info[StoreNumber]</th>     </tr>";$countBook++;$info = mysql_fetch_array($resoures);}echo "</table>";closeConnect($link);}else{echo "数据库连接失败";}function changeClassfication(){global $classfication;if(1 == $classfication){$classfication = '人文';}else if(2 == $classfication){$classfication = '教辅';}else if(3 == $classfication){$classfication = '科技';}else if(4 == $classfication){$classfication = '游戏';}else if(5 == $classfication){$classfication = '生活';}else if(6 == $classfication){$classfication = '技术';}else if(7 == $classfication){$classfication = 'IT';}}function checkForm(){//下面是对图书编号的合法性进行检测global $bookID;$isRightBookID = preg_match('/[0-9]/', $bookID);if (!$isRightBookID && $bookID){echo "<Script>alert('图书编号含有非法字符')</Script>";echo "<Script>window.location.href='searchBook.php'</Script>";return false;}//价格global $price;$isRightPrice = preg_match('/[0-9]./', $price);if (!$isRightPrice && $price){echo "<Script>alert('图书价格含有非法字符')</Script>";echo "<Script>window.location.href='searchBook.php'</Script>";return false;}//库存global $numberAll;$isRightAllNumber = preg_match('/[0-9]/', $numberAll);if (!$isRightAllNumber && $numberAll){echo "<Script>alert('图书库存含有非法字符')</Script>";echo "<Script>window.location.href='searchBook.php'</Script>";return false;}global $numberNow;$isRightNowNumber = preg_match('/[0-9]/', $numberNow);if (!$isRightNowNumber && $numberNow){echo "<Script>alert('图书库存含有非法字符')</Script>";echo "<Script>window.location.href='searchBook.php'</Script>";return false;}return true;}?>

2.其他功能实现简介

对于管理员 还有借阅归还图书和借书证管理的功能 实现方法大同小异
对于用户来说,只有查询书籍和修改自己密码的功能 初始密码是123456 密码都是经过MD5加密后来储存的

3.总结

主要使用到的技术点:
1. PHPExcel的使用
这个开源的工具十分强大 不仅仅能够读取一个excel 而且可以创建一个excel表 最最关键的是可以在创建的时候设置好多的属性
详细的使用方法可以参考这里
2.MySql的使用
通过php来和后台的数据库MySql进行数据的交互 这里我主要是使用了MySql提供的一系列函数
类似于JDBC和ODBC 但是似乎更简单 封装更好
使用方法参考这里
3.php数据交互方式
一般都是通过表单进行一个数据的提交 可以指定是POST还是Get方法 通过php定义的全局数组来获取这个数据,此外cookie session也可以这样获取 可以说非常之简单
4.一个表单提交到不同的页面
这个很简单其实 就是利用javascript脚本 在sublim上加上一个点击事件 然后在触发这个点击事件的时候我们改变这个表单的action
属性就好了
<input type="submit" value="执行" style="width:50px;height:25px; margin:0px 250px;"onclick="javascript:this.form.action='deleteCard.php';">
5.不足之处 
写的紧 代码凌乱 一些设计不到位 这个其实是自己的水平不到
异常处理可能存在问题 对于很小的数据量应该没问题 大量的访问必然会出问题

暂时这个多 代码贴出 完善时会继续更新

下载地址 

0 0
原创粉丝点击