如果用户已经在PHP中存在,则在将Excel文件数据上载到数据库时显示错误消息
我正在尝试使用如果用户已经在PHP中存在,则在将Excel文件数据上载到数据库时显示错误消息,php,excel,moodle,Php,Excel,Moodle,我正在尝试使用phpexcel库将一些Excel数据加载到数据库中,如果用户已经存在,那么一切都正常-没有显示任何错误 如果数据库中已经存在用户,如何显示错误消息 这是我的密码: <?php require('../config.php'); set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/'); include 'PHPExcel/IOFactory.php'; $input
phpexcel
库将一些Excel数据加载到数据库中,如果用户已经存在,那么一切都正常-没有显示任何错误
如果数据库中已经存在用户,如何显示错误消息
这是我的密码:
<?php
require('../config.php');
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';
$inputFileName = $_FILES['filename']['tmp_name'];
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);
$ref = 1;
$today = date("Y-m-d H:i:s");
for ($i = 1; $i <= $arrayCount; $i++) {
$username = trim($allDataInSheet[$i]["A"]);
$firstname = trim($allDataInSheet[$i]["B"]);
$lastname = trim($allDataInSheet[$i]["C"]);
$email = trim($allDataInSheet[$i]["D"]);
$password = trim($allDataInSheet[$i]["E"]);
if ($username != '' || $username != 0) {
$insert_record = new stdClass();
$insert_record->username = $username;
$insert_record->email = $email;
$insert_record->firstname = $firstname;
$insert_record->lastname = $lastname;
$insert_record->password = password_hash($password, PASSWORD_DEFAULT);
$insert_record->idnumber = 1;
$insert_record->timecreated = time();
$insert_record->mnethostid = 1;
$insert_record->confirmed = 1;
$resultcheck = $DB->insert_record('user', $insert_record);
}
}
header("Location:user_management.php");
?>
解决此问题有多种途径。这取决于您的实际代码 如果在数据库中将用户名字段设置为唯一,并且使用PDO查询,则无法尝试捕获错误代码:
try{
//Make the insert
}
catch(PDOException $e){
if($e->errorInfo[1] == 1062){
//Duplicated
}
else{
//Other errors...
}
}
我像这样使用try{$objPHPExcel=PHPExcel\u IOFactory::load($inputFileName);}catch(异常$e){if($e->errorInfo[1]==1062){echo“duplicated”;}else{die('Error loading file'.pathinfo($inputFileName,pathinfo_BASENAME)。“:'.$e->getMessage())}我试过这样做..它不起作用..实际上我将此项目连接到moodle..Try block应该是插入相关代码(查询),而不是excel代码。print_r($resultcheck)返回什么?有时它将头值存储在db中..从xl文件中,$resultcheck是一个类似
Array('column1'=>id',column2'=>name'的数组)
?然后您可以使用Count($resultcheck)
检查数组的长度。这可能会有所帮助。不,我正在尝试选择*fom表名,其中username=$allDataInSheet[$i];当发生此查询时,我必须发出错误消息。请提供您的数据库处理代码。我已使用一种可能的方法编辑了我的答案,以检查用户是否已经存在。
for ($i = 1; $i <= $arrayCount; $i++) {
...
$resultcheck = $DB->insert_record('user', $insert_record);
if ($resultcheck == false){
header("Location:error.php");
}
}
for ($i = 1; $i <= $arrayCount; $i++) {
...
$resultcheck = $DB->insert_record('user', $insert_record);
if ($resultcheck == false){
echo json_encode ('Your Error Message.');
exit ();
}
}
public function insert_record($table, $insert_record) {
$sql = "SELECT * FROM users WHERE username =: username";
$statement = $this->db->prepare ( $sql );
$statement->bindValue ( ':username', $insert_record->username, PDO::PARAM_STR );
$statement->execute ();
$result = $statement->fetch ( PDO::FETCH_ASSOC );
if (count($result) > 0){
return false;
}
// Do your other stuff here
}