Php+mysql Library Management System (III)

Source: Internet
Author: User

1. Single Book Storage operationSimple Insert operation Note that the uniqueness of the primary key means that you cannot have a numbered bookThe client can check the existence of the book before inserting it and give it back to the user of course it can also be done directly and then parse the return result of SQL execution can also resolve the cause of the error and feedback to the user
When data is submitted to the page, it is best to validate the submitted data to prevent errors in data insertion or errors in the data in the database.
The code is as follows:
<?phprequire ("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 (' book Storage Success ') </Script>"; echo "< script>window.location.href= ' admindoaddonebook.php ' </Script> ';} Else{echo "<script>alert (' book storage failed, please try again! ') </Script> "echo" <script>window.location.href= ' admindoaddonebook.php ' </Script> ";} function Checkform () {///below is the detection of the legality of the book number global $bookID; if ($bookID = = null) {echo "<script>alert (' book number cannot be empty ') < /script> "; echo" <script>window.location.href= ' admindoaddonebook.php ' </Script> "; return false;} $isRightBookID = Preg_match ('/[0-9]/', $bookID), if (! $isRightBookID) {echo "<script>alert (' book Number contains illegal words</Script> "; echo" <script>window.location.href= ' admindoaddonebook.php ' </Script> "; return false;} Book name check global $bookName; if (null = = $bookName) {echo "<script>alert (' book name cannot be empty ') </Script>"; echo "< script>window.location.href= ' admindoaddonebook.php ' </Script> "; return false;} Publisher Global $press; if (null = = $press) {echo "<script>alert (' publisher cannot be empty ') </Script>"; echo "<Script> window.location.href= ' admindoaddonebook.php ' </Script> "; return false;} Author Global $author; if (null = = $author) {echo "<script>alert (' author cannot be empty ') </Script>"; echo "<Script> window.location.href= ' admindoaddonebook.php ' </Script> "; return false;} Price global $price; if (null = = $price) {echo "<script>alert (' Price cannot be empty ') </Script>"; echo "<Script> window.location.href= ' admindoaddonebook.php ' </Script> "; return false;} $isRightPrice = Preg_match ('/[0-9]./', $price), if (! $isRightPrice) {echo "<script>alert (' book price contains illegal characters ') </ Script> "; echo"<script>window.location.href= ' admindoaddonebook.php ' </Script> "; return false;} Inventory global $numberAll; if (null = = $numberAll) {echo "<script>alert (' inventory cannot be empty ') </Script>"; echo "<script >window.location.href= ' admindoaddonebook.php ' </Script> "; return false;} $isRightAllNumber = Preg_match ('/[0-9]/', $numberAll), if (! $isRightAllNumber) {echo "<script>alert (' Book inventory contains illegal characters ') </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 (' Database connection failed ') </ Script> "; return false;} $class = null;if (' 1 ' = = $classfication) {$class = ' humanities ';} else if (' 2 ' = = $classfication) {$class = ' des ';} else if (' 3 ' = = $classfication) {$class = ' game ';} else if (' 4 ' = = $classfication) {$class = ' technology ';} else if (' 5 ' = = $CLassfication) {$class = ' life ';} else{$class = ' technology ';} $sql = "INSERT INTO book values (". $bookID. ", '". $class. "',". "'". $bookName. "',". "'". $press. "',". "'". $publicTime. "',". "'". $author. "',". $price. ",". $numberAll. ",". $numberAll. "); $isRightInsert = Getresoures (' Libray ', $sql); Closeconnect ($link); return $isRightInsert;}? >

2. Importing data in batchesdata exists in an Excel table with open source Phpexcelspecific operation can refer here here
<?php//introduction of Phpexcelrequire ("./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 = "The file already exists";} 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 = ' unselected files or files are not visible or file type error ';} if ($isUpLoadSucceSS) {//echo "<script>alert (' upload succeeded ');</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 ();//Determine if the file can be read if ($objPHPExcelReader->canread ($filePath)) {//echo "yes";} else{//version is incorrect when switching version $objphpexcelreader = new Phpexcel_reader_excel5 (); if ($objPHPExcelReader->canread ($filePath)) {}else{echo ' <script>alert (' file does not exist or the file is unreadable, please check and try again! ');</script> "; return;}} $objPHPExcel = $objPHPExcelReader->load ($filePath);//Read the first worksheet in an Excel file $currentsheet = $objPHPExcel->getsheet (0);//Fetch the largest column number $allcolumn = $currentSheet->gethighestcolumn ();//Fetch the largest line number $allrow = $currentSheet->gethighestrow ( );//Connect Database $link = mysql_connect ("127.0.0.1", "Root", "") or Die ("Cannot connect to database". Mysql_error ()); if ($link) {//echo "Connection succeeded! ";} $DB _select = mysql_select_db ("Libray"); if ($Db_select) {//echo "yes";} $allInsertNumber = 0; $insertSuccessNumber = 0;for ($currentRow = 2; $currentRow <= $allRow; $currentRow + +) {$sql = "Inser T 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 = "Total $allInsertNumber data, successfully executed $insertSuccessNumber data!"; echo "<script>alert ('". $result. "); </Script> "; echo" <script>window.location.href= ' admindoaddbook.php ' </Script> ";//tool function to solve Chinese garbled 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 Library Management System (III)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.