我使用checkfile上传文件 Month&年。
如果成功,它会将数据加载到数据库。
IF checkfile = false
我想显示提醒消息("import Fail")
foreach ($files1 as &$value) {
$checkfile = strpos($value,$input1 );
if($checkfile === false) {
//echo "<script>alert('Import Fail ');</script>";
}
else
{
echo "Import $value successfully! <br>" ;
$query = "load data local infile '//192.168.100.3/Groups/$location1/Timesheet/$value'
replace
$result = $connection->query($query) or exit("Error code ({$connection->errno}): {$connection->error}");
}
}
unset($value);
?>
我尝试使用echo 警告(&#39;导入失败&#39;);
但是它给了我文件夹中每个文件的多个警报。
答案 0 :(得分:2)
Way1:使用以下标志管理流程。如果任何文件成功导入,则更改标志,然后在标志上创建一个条件以显示警告。
$flag = 0;
foreach ($files1 as &$value) {
$checkfile = strpos($value,$input1 );
if($checkfile === false) {
//echo "<script>alert('Import Fail ');</script>";
}
else
{
$flag = 1;
echo "Import $value successfully! <br>" ;
// $query1 =
// "Delete FROM hklcanet_pha.psr this
// WHERE year(psr.ReportDate) = $input3
// AND month(psr.ReportDate) = $input2";
$query = "load data local infile '//192.168.100.3/Groups/$location1/Timesheet/$value'
replace
into table hklcanet_pha.psr fields terminated by ','
optionally enclosed by '\"'
lines terminated by '\n'
ignore 1 lines
(`ReportDate`, @dummy, @dummy, `Team_refno`,`Name/Description`,`Status`,`PIC`,`RequestDate`,`TargetEndDate`,`ActualEndDate`,`PlanStartDate`,`ActualStartDate`,`PlanUATDate`,`ActualUATDate`,`PlanImplement`,`ActualImplement` )";
//echo "Import $value successfully! <br>" ;
$result = $connection->query($query) or exit("Error code ({$connection->errno}): {$connection->error}");
}
}
if($flag == 1){
echo "<script>alert('Import Fail ');</script>";
}
unset($value);
?>
方式2:获取已成功导入的文件名的名称并为其创建条件。
$flag = [];
foreach ($files1 as &$value) {
$checkfile = strpos($value,$input1 );
if($checkfile === false) {
//echo "<script>alert('Import Fail ');</script>";
}
else
{
//Store successful file names in array
$flag[] = $value;
echo "Import $value successfully! <br>" ;
// $query1 =
// "Delete FROM hklcanet_pha.psr this
// WHERE year(psr.ReportDate) = $input3
// AND month(psr.ReportDate) = $input2";
$query = "load data local infile '//192.168.100.3/Groups/$location1/Timesheet/$value'
replace
into table hklcanet_pha.psr fields terminated by ','
optionally enclosed by '\"'
lines terminated by '\n'
ignore 1 lines
(`ReportDate`, @dummy, @dummy, `Team_refno`,`Name/Description`,`Status`,`PIC`,`RequestDate`,`TargetEndDate`,`ActualEndDate`,`PlanStartDate`,`ActualStartDate`,`PlanUATDate`,`ActualUATDate`,`PlanImplement`,`ActualImplement` )";
//echo "Import $value successfully! <br>" ;
$result = $connection->query($query) or exit("Error code ({$connection->errno}): {$connection->error}");
}
}
if(sizeof($flag) == 0){
//Display all the file names in alert
$failedFiles = implode(",",$flag);
echo "<script>alert('Import Failed for files : ".$failedFiles."');</script>";
}
unset($value);
?>