码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-06-14 15:17:21      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

1.单本图书入库操作

简单的插入操作 要注意的是主键的唯一性 也就是说不能有编号一样的图书  客户端可以在插入之前检查一下是否存在此图书 然后反馈给用户 当然也可以直接进行操作 然后解析sql执行的返回结果 同样可以解析出错误的原因 并且反馈给用户

在数据提交到页面后最好对提交的数据进行校验 防止数据插入的时候出现错误或者造成数据库里数据的错误

代码如下:
<?php
	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'];


	$isRightForm = checkForm();

	$isRightInsert = insertBook();
	if ($isRightInsert){
		echo "<Script>alert('图书入库成功')</Script>";
		echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
	}else{
		echo "<Script>alert('图书入库失败,请重试!')</Script>";
		echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
	}


	function checkForm(){
		//下面是对图书编号的合法性进行检测
		global $bookID;
		if ($bookID == null){
			echo "<Script>alert('图书编号不能为空')</Script>";
			echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
			return false;
		}
		$isRightBookID = preg_match('/[0-9]/', $bookID);
		if (!$isRightBookID){
			echo "<Script>alert('图书编号含有非法字符')</Script>";
			echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
			return false;
		}
		//图书名称校验
		global $bookName;
		if (null == $bookName){
			echo "<Script>alert('图书名称不能为空')</Script>";
			echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
			return false;
		}
		//出版社
		global $press;
		if (null == $press){
			echo "<Script>alert('出版社不能为空')</Script>";
			echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
			return false;
		}
		//作者
		global $author;
		if (null == $author){
			echo "<Script>alert('作者不能为空')</Script>";
			echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
			return false;
		}
		//价格
		global $price;
		if (null == $price){
			echo "<Script>alert('价格不能为空')</Script>";
			echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
			return false;
		}
		$isRightPrice = preg_match('/[0-9]./', $price);
		if (!$isRightPrice){
			echo "<Script>alert('图书价格含有非法字符')</Script>";
			echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
			return false;
		}
		//库存
		global $numberAll;
		if (null == $numberAll){
			echo "<Script>alert('库存量不能为空')</Script>";
			echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
			return false;
		}
		$isRightAllNumber = preg_match('/[0-9]/', $numberAll);
		if (!$isRightAllNumber){
			echo "<Script>alert('图书库存含有非法字符')</Script>";
			echo "<Script>window.location.href='adminDoAddOneBook.php'</Script>";
			return false;
		}
		return true;
	}

	function insertBook(){
		global $bookID;
		global $classfication;
		global $bookName;
		global $press;
		global $publicTime;
		global $author;
		global $price;
		global $numberAll;
		$link = getLink();
		if ('0' == $link){
			echo "<Script>alert('数据库连接失败');</Script>";
			return false;
		}
		$class = null;
		if('1' == $classfication){
			$class = '人文';
		}else if ('2' == $classfication){
			$class = '教辅';
		}else if ('3' == $classfication){
			$class = '游戏';
		}else if ('4' == $classfication){
			$class = '科技';
		}else if ('5' == $classfication){
			$class = '生活';
		}else{
			$class = '技术';
		}
		$sql = "insert into book values(".$bookID.",'".$class."',"."'".$bookName."',"."'".$press."',"."'".$publicTime."',"."'".$author."',".$price.",".$numberAll.",".$numberAll.");";
		$isRightInsert = getResoures('libray', $sql);
		closeConnect($link);
		return $isRightInsert;
	}
?>

2.批量导入数据

数据存在于一个excel表格中 借助开源的phpexcel进行操作
具体操作可以参考这里 这里
<?php

	//引入PHPExcel
	require("./phpexcel/PHPExcel.php");
	require("./phpexcel/PHPExcel/Reader/Excel2007.php");
	require("./phpexcel/PHPExcel/Reader/Excel5.php");
	require("./phpexcel/PHPExcel/Cell.php");

	header('Content-Type:text/html;charset=utf-8;');
	$isUpLoadSuccess = false;
	$failReason = null;
	if ($_FILES["filePath"]["type"] == "application/octet-stream"){
		if ($_FILES["filePath"]["error"] > 0) {
			$failReason = "Return Code: ".$_FILES["filePath"]["error"];
			//echo "Return Code: ".$_FILES["filePath"]["error"]."<br />";
		} else {
			if (file_exists ( "upload/" . $_FILES ["filePath"] ["name"] )) {
				//echo $_FILES ["filePath"]["name"]."already exists.";
				$failReason = $_FILES ["filePath"]["name"]."already exists.";
				$failReason = "该文件已存在";
			} else {
				move_uploaded_file ($_FILES["filePath"]["tmp_name"], "upload/".$_FILES["filePath"]["name"]);
				$isUpLoadSuccess = true;
				//echo "Stored in: " . "upload/".$_FILES["filePath"]["name"];
			}
		}
	} else {
		//echo "Invalid file";
		$failReason = '未选中文件或文件不可见或文件类型错误';
	}
	if ($isUpLoadSuccess){
		//echo "<Script>alert('上传成功');</Script>";
	}else{
		echo "<Script>alert('".$failReason."');</Script>";
		echo "<Script>window.location.href='adminDoAddBook.php'</Script>";
		exit(0);
	}

	$filePath = "./upload/".$_FILES["filePath"]["name"];
	$objPHPExcel = new PHPExcel();
	$objPHPExcelReader = new PHPExcel_Reader_Excel2007();
	//判断文件是否可以读
	if($objPHPExcelReader->canRead($filePath)){
		//echo "yes";
	}else{
		//版本不对的时候切换版本
		$objPHPExcelReader = new PHPExcel_Reader_Excel5();
		if($objPHPExcelReader->canRead($filePath)){
		}else{
			echo "<Script>alert('文件不存在或者文件不可读,
				请检查后重试!');</Script>";
			return;
		}
	}
	$objPHPExcel = $objPHPExcelReader->load($filePath);
	//读取excel文件中的第一个工作表
	$currentSheet = $objPHPExcel->getSheet(0);
	//取到最大的列号
	$allColumn = $currentSheet->getHighestColumn();
	//取到最大的行号
	$allRow = $currentSheet->getHighestRow();
	//连接数据库
	$link = mysql_connect("127.0.0.1", "root", "")
				or die("不能连接到数据库".mysql_error());
	if($link){
		//echo "连接成功!";
	}
	$db_select = mysql_select_db("libray");
	if ($db_select) {
		//echo "yes";
	}
	$allInsertNumber = 0;
	$insertSuccessNumber = 0;
	for($currentRow = 2; $currentRow <= $allRow; $currentRow++){
		$sql = "insert INTO Book values (";
		for($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++){
			$str = $currentColumn.$currentRow;
			$val = $currentSheet->getCell($str)->getValue();
			if($currentColumn == 'B' || $currentColumn == 'C'
				|| $currentColumn == 'D' || $currentColumn == 'F'){
				//$val = convertToUTF8($val);
				$val = "'".$val."'";
			}
			if ($currentColumn == 'E') {
				$val = excelTime($val);
				$val = "'".$val."'";
			}
			if ('A' != $currentColumn) {
				$sql = $sql.",";
			}
			$sql = $sql.$val;
		}
		$sql = $sql.")";
		$b = mysql_query($sql);
		//echo "$sql<br>";
		$allInsertNumber++;
		if ($b) {
			//echo "yes";
			$insertSuccessNumber++;
		}else{
			//echo "no";
		}
		//echo "$sql";
	}
	$result = "共 $allInsertNumber 条数据,成功执行 $insertSuccessNumber 条数据!";
	echo "<Script>alert('".$result."');</Script>";
	echo "<Script>window.location.href='adminDoAddBook.php'</Script>";
	//工具函数 解决中文乱码
	function convertToUTF8($str){
		return iconv('utf-8','gb2312', $str);
	}
	function excelTime($date, $time = false) {
    	if(function_exists('GregorianToJD')){
        	if (is_numeric( $date )) {
        	$jd = GregorianToJD( 1, 1, 1970 );
        	$gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
        	$date = explode( '/', $gregorian );
        	$date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
       		."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
        	."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
        	. ($time ? " 00:00:00" : '');
        	return $date_str;
        	}
    	}else{
        	$date=$date>25568?$date+1:25569;
        	/*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
        	$ofs=(70 * 365 + 17+2) * 86400;
        	$date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
   		}
  		return $date;
	}
?>




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

标签:

原文地址:http://blog.csdn.net/u013224148/article/details/46490865

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!