【问题标题】:excel file upload using phpexcel and data insertion to the mysql database使用phpexcel上传excel文件并将数据插入mysql数据库
【发布时间】:2017-03-08 05:42:27
【问题描述】:

请帮我解决这个问题 我只需要上传一个excel文件来上传文件夹同时将数据导出到mysql数据库。

当前上传成功,如果我手动提供 excel 文件位置和文件名,其中的数据将导出到数据库。 请告诉我它应该同时使用什么方法。

用于将 excel 文件上传到“uploads”文件夹的代码集

<?php
require_once './config/MainConfig.php';
include './config/dbc.php';


$uploadedStatus = 0;

if (isset($_POST["submit"])) {
    if (isset($_FILES["file"])) {
//        $_SESSION['date_ss'] = $_POST['date_ss'];
//if there was an error uploading the file
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        } else {
            if (file_exists($_FILES["file"]["name"])) {
                unlink($_FILES["file"]["name"]);
                $uploadedStatus = 2;
            }
            $name = basename($_FILES['file']['name']);
            $name1 = explode('.', $name);
            if ($name1[count($name1) - 1] == 'csv' || $name1[count($name1) - 1] == 'xlsx') {

                $target_path = "uploads/";
                $target_location = $target_path . basename($_FILES['file']['name']);
                $_SESSION['target_location'] = $target_location;


//            $datess = $_POST['date_ss'];
                move_uploaded_file($_FILES["file"]["tmp_name"], $target_location);
                $uploadedStatus = 1;
            }
        }
    } else {
        echo "No file selected <br />";
    }
}
?>

<html>
    <head>
        <style>
            .file-upload {
                max-width: 580px;
                height: 200px;
                padding: 25px 35px 45px;
                margin: 0 auto;
                background-color: #fff;
                border: 1px solid rgba(0,0,0,0.1);  
            }

        </style>
    </head>

    <?php 
    if (array_key_exists("action", $_POST)) {
          if ($_POST['action'] == 'sendManualFileUpoadingData') {
              $Manual_note_No=$_POST['Manual_note_No'];
              $Phone_amount= $_POST[' Phone_amount'];
              echo $Manual_note_No;

          }
    }

    ?>
    <div class="container">
        <div class="wrapper">
            <div class="file-upload">  
                <div class="row">
                    <div class="col-md-4">Transfer Note Number :</div>
                    <div class="col-md-4"><?php echo''  ?></div>
                </div>
                <div class="row">
                    <div class="col-md-4">Phone quantity:</div>
                    <div class="col-md-4"><?php echo '' ?></div>
                </div>
                <div class="row">
                <div class="col-md-4"></div>
                <div class="col-md-4">
                    <form action="fileuploadexecution.php" method="post" enctype="multipart/form-data">
                        <input type="file" id="file" name="file" multiple="multiple" />
                        <p style="text-align: right; margin-top: 20px;">
                            <input type="submit"  value="Upload Files" name="submit" class= "btn btn-success" />
                        </p>
                    </form>
                </div>
                <div class="col-md-4"></div>
                </div>
                <div class="row">
                    <?php
                    if ($uploadedStatus == 1) {
                        echo 'file uploaded successfully';
                    } elseif ($uploadedStatus == 2) {
                        echo 'file already available';
                    }
                    ?>
                </div>

            </div>
        </div>    
    </div>



    <!-- you need to include the ShieldUI CSS and JS assets in order for the Upload widget to work -->
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    <script type="text/javascript" src="js/jquery.min.js"></script>
</html>

用于将excel文件数据导出到mysql表中的代码集

<?php

session_start();
//all save,update,delete

require_once './config/dbc.php';
//db connectin
require_once './class/database.php';
require_once './class/systemSetting.php';
$system = new setting();
//calling the class setting from systemsetting.php
$database = new database();

//                                MainConfig::connectDB();
//                                $datess = $_SESSION['date_ss'];
//                                $q = mysql_fetch_array(mysql_query("SELECT MAX(commission.num_of_session +1)AS commax FROM commission"));
//                                $sess = $q['commax'];


set_include_path(get_include_path() . PATH_SEPARATOR . 'ex_class/');
include './xl_upload/ex_class/PHPExcel/IOFactory.php';


// This is the file path to be uploaded.
//
//echo $_SESSION['target_location'];
//$inputFileName = $target_path . basename($_FILES['file']['name']);

 $inputFileName = 'testFile.xlsx';

try {
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch (Exception $e) {
    die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}


$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet
$count = 1;
for ($count; $count <= $arrayCount; $count++) {
    $Doc_No = trim($allDataInSheet[$count]["A"]);
    $ESN = trim($allDataInSheet[$count]["B"]);

    $insertTable = mysql_query("INSERT INTO `test_table` (`Doc_No`, `ESN`) VALUES ('".$Doc_No."','".$ESN."');") or die(mysql_error());
}
$msg = 'Record has been added. <div style="Padding:20px 0 0 0;"><a href="commision_data_upolad.php">Go Back</a></div>';

?>

excel file that going to upload

【问题讨论】:

  • 你要洞数据库或表
  • 只有单表表
  • 所以你可以使用下面的代码

标签: php mysql phpexcel


【解决方案1】:

试试这个

从 PHP 文件执行数据库备份查询。以下是使用 SELECT INTO OUTFILE 查询创建表备份的示例:

$tableName  = 'yourtable';
$backupFile = 'backup/yourtable.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysqli_query($con,$query);

要恢复备份,您只需像这样运行 LOAD DATA INFILE 查询:

$tableName  = 'yourtable';
$backupFile = 'yourtable.sql';
$query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysqli_query($con,$query);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多