标签:
<?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; } ?>
<?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; } ?>
标签:
原文地址:http://blog.csdn.net/u013224148/article/details/46490865