请帮我解决这个问题 我只需要上传一个excel文件上传文件夹,同时将数据导出到mysql数据库。
当前上传成功发生,如果我手动提供excel文件位置和文件名,数据将导出到数据库。 请告诉我在同一时间用它做什么方法。
用于将excel文件上传到'上传的代码集'文件夹
<?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>';
?>
答案 0 :(得分:0)
试试这个
从PHP文件执行数据库备份查询。下面是使用SELECT INTO OUTFILE查询创建表备份的示例:
$tableName = 'yourtable';
$backupFile = 'yourtable.sql';
$query = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysqli_query($con,$query);
要恢复备份,您只需运行LOAD DATA INFILE查询,如下所示:
select firstname=(case when ISNULL(firstname,'')<>'' AND len(firstname) = 5 then firstname end) from employeedetail