gpt4 book ai didi

php - PHP 中的 .xlsx 工作表中的日期未存储在数据库中

转载 作者:行者123 更新时间:2023-11-29 18:34:53 24 4
gpt4 key购买 nike

我有一个页面可以将 Excel 工作表记录保存到数据库中。除了“出生日期”列之外,记录的存储方式与 Excel 工作表中提供的方式相同。“出生日期”列仅保存从 Excel 工作表上传的第二条记录。我对此进行了很多搜索,但没有找到确切的原因。

这是我的完整代码,

<?php include 'blocks/headerInc.php' ; 
include 'Classes/PHPExcel/IOFactory.php';
?>
<?php
$scsmsg = "" ;
$errmsg = "" ;






if(isset($_POST['submit']))
{
extract($_POST);
//$moduleId = mysql_real_escape_string(htmlspecialchars(trim($module_id)));die;
if(empty($_FILES['bulk_file']['name']))
{
$errmsg .= 'Please Choose File.<br>';

}
if(!empty($_FILES['bulk_file']['name']))
{

$extensions = array('.xlsx');

$valid_extensions = '.xlsx';
$extension = strrchr($_FILES['bulk_file']['name'], '.');
if (!in_array($extension, $extensions))
{
$errmsg .="Wrong files format , alowed Extension only".$valid_extensions.""."<br>";
}
}


if($errmsg == "")
{
$created = date("Y-m-d h:i:s");
$register_date = date("Y-m-d");
$created_by = $_SESSION['session_admin_id'] ;


$bulk_file = $_FILES['bulk_file']['name'];
if(!empty($bulk_file))
{
$tmp_name=$_FILES['bulk_file']['tmp_name'];
$bulk_file=$_FILES['bulk_file']['name'];
$file_name = $bulk_file;
//$file=$file_name.".csv";
$inputFileName = "uploads/user/$file_name";
$file_sucess=move_uploaded_file($tmp_name,$inputFileName);







//$inputFileName = 'format.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


for($i=2;$i<=$arrayCount;$i++)
{
$salutation = trim($allDataInSheet[$i]["A"]);
$sbiempid = trim($allDataInSheet[$i]["B"]);
$branch = trim($allDataInSheet[$i]["C"]);
$middlename = trim($allDataInSheet[$i]["D"]);
$firstname = trim($allDataInSheet[$i]["E"]);
$lastname = trim($allDataInSheet[$i]["F"]);
$dateofbirth = trim($allDataInSheet[$i]["G"]);
$dateInput = explode('-',$dateofbirth);
$dob = $dateInput[2].'-'.$dateInput[1].'-'.$dateInput[0];
// $dob ="19".date("Y-m-d", strtotime($dateofbirth));
//var_dump($dob);
//print_r($dob);
$mobileno = trim($allDataInSheet[$i]["H"]);
$email = trim($allDataInSheet[$i]["I"]);
$city = trim($allDataInSheet[$i]["J"]);
$state = trim($allDataInSheet[$i]["K"]);
$designation = trim($allDataInSheet[$i]["L"]);


$corporate_user_sql=mysql_fetch_array(mysql_query("select * from tbl_corporate where id='$created_by'"));
$pre_define_day=$corporate_user_sql['pre_defined_day'];

$dataQuestion = array("type"=>"3","salutation"=>$salutation, "employee_id"=>$sbiempid, "branch"=>$branch, "middle_name"=>$middlename, "first_name"=>$firstname, "last_name"=>$lastname, "dob"=> $dob, "mobile"=>$mobileno, "email"=>$email, "city"=>$city,"state"=>$state,"designation"=>$designation,"created_on"=>$created, "created_by"=>$created_by,"status"=>"0","register_date"=>$register_date,"request_updated_day"=>$pre_define_day,"registration_type"=>"1");


$db->query_insert("tbl_user", $dataQuestion);
$question_id = mysql_insert_id();
$raw_password=uniqid().$question_id;
$md5_pass=md5(uniqid().$question_id);

$dataOption1 = array("registration_id"=>$sbiempid."_".$question_id,"password"=>$md5_pass,"raw_password"=>$raw_password);
$db->query_update("tbl_user", $dataOption1,"id=$question_id");
}

}
$scsmsg = "<b>Record inserted Successfully</b>";

}
}
?>
<div class="container pagecontainer">
<!-- Static navbar -->
<div class="row row-offcanvas row-offcanvas-right">
<!--/.col-xs-12.col-sm-9-->
<div class="col-sm-3 col-md-3 sidebar" id="sidebar">
<div id="left_panel" class="clearfix left">
<?php include 'blocks/leftnavInc.php' ; ?>
</div>
</div>
<div class="col-xs-12 col-sm-9 page-right">
<div class="panel panel-primary">
<div class="panel-heading">Upload User</div>
<div class="panel-body">
<div class="column col-sm-offset-0">
<?php
if($scsmsg!="")
{
echo "<div class='success'>".ucwords($scsmsg)."</div>";
}
if($errmsg!="")
{
echo "<div class='error'>".ucwords($errmsg)."</div>";
}
echo "<div id='error'></div>";
?>
<form class="form-horizontal" method="post" action="" enctype="multipart/form-data">



<div class="form-group">
<div class="col-md-12">
<div class="col-md-3">
<label for="username" class="control-label">Select FIle:</label>
</div>
<div class="col-md-9">

<input type="file" name="bulk_file" class="form-control" required="" >
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="col-md-3">
<label for="username" class="control-label">
<a href="CandidateProfileUploadInformation.xlsx" class="btn btn-info">Download Format</a></label>
</div>
<div class="col-md-9 text-right">
<button type="submit" name="submit" value="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
<button type="reset" onClick="javascript:window.location.href='quizList.php';" class="btn btn-danger"><i class="glyphicon glyphicon-ban-circle"></i> Cancel</button>
<button type="reset" onclick="javascript:history.go(-1)" class="btn btn-danger"><i class="glyphicon glyphicon-ban-circle"></i> Go Back</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!--/.sidebar-offcanvas-->
</div>
</div>
<?php include 'blocks/footerInc.php'; ?>

我可以知道我在哪里做错了吗,因为其余的数据字段都是按语义发布的。这仅存储 Excel 工作表中第二条记录的出生日期,其余行的出生日期将保存为 0000-00-00。任何建议和帮助将不胜感激。

最佳答案

我建议您在从电子表格内容构建数组时不要请求格式化值,而是使用

$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,false,true);

而不是

$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

然后手动将单元格中的原始时间戳日期值转换为数据库所需的格式

$dateofbirth = PHPExcel_Shared_Date::ExcelToPHPObject($allDataInSheet[$i]["G"])
->format('Y-m-d');

关于php - PHP 中的 .xlsx 工作表中的日期未存储在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45387921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com