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

1.单本图书入库操作

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

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

代码如下:
alert('图书入库成功')";
		echo "";
	}else{
		echo "";
		echo "";
	}


	function checkForm(){
		//下面是对图书编号的合法性进行检测
		global $bookID;
		if ($bookID == null){
			echo "";
			echo "";
			return false;
		}
		$isRightBookID = preg_match('/[0-9]/', $bookID);
		if (!$isRightBookID){
			echo "";
			echo "";
			return false;
		}
		//图书名称校验
		global $bookName;
		if (null == $bookName){
			echo "";
			echo "";
			return false;
		}
		//出版社
		global $press;
		if (null == $press){
			echo "";
			echo "";
			return false;
		}
		//作者
		global $author;
		if (null == $author){
			echo "";
			echo "";
			return false;
		}
		//价格
		global $price;
		if (null == $price){
			echo "";
			echo "";
			return false;
		}
		$isRightPrice = preg_match('/[0-9]./', $price);
		if (!$isRightPrice){
			echo "";
			echo "";
			return false;
		}
		//库存
		global $numberAll;
		if (null == $numberAll){
			echo "";
			echo "";
			return false;
		}
		$isRightAllNumber = preg_match('/[0-9]/', $numberAll);
		if (!$isRightAllNumber){
			echo "";
			echo "";
			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 "";
			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进行操作
具体操作可以参考这里 这里
 0) {
			$failReason = "Return Code: ".$_FILES["filePath"]["error"];
			//echo "Return Code: ".$_FILES["filePath"]["error"]."
"; } 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 ""; }else{ echo ""; echo ""; 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 ""; 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
"; $allInsertNumber++; if ($b) { //echo "yes"; $insertSuccessNumber++; }else{ //echo "no"; } //echo "$sql"; } $result = "共 $allInsertNumber 条数据,成功执行 $insertSuccessNumber 条数据!"; echo ""; echo ""; //工具函数 解决中文乱码 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)