php+MySQL图书管理系统

单本图书入库操作

简单的插入操作 要注意的是主键的唯一性 也就是说不能有编号一样的图书   客户端可以在插入之前检查一下是否存在此图书 然后反馈给用户 当然也可以直接进行操作 然后解析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;
	}
?>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

apple_51426592

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值