2017-07-05 30 views
0

您好,我有一个xls文件导入多行,并且我想将它们插入到数据库中,只有当价格列的值不同于其他值时才会显示错误消息。它工作在一半,它只插入那些有价格价值的行,但是返回html错误消息,它返回sql msg,这意味着它在else分支上。 这里是我的代码PHPExcel验证单元格是否具有empy值

$exceldata = array(); 

$uploadFilePath = 'uploads/'.basename($_FILES['doc']['name']); 
     move_uploaded_file($_FILES['doc']['tmp_name'], $uploadFilePath); 

     $inputfilename = 'uploads/'.$_FILES['doc']['name'].''; 
// Read your Excel workbook 
try 
{ 
    $inputfiletype = PHPExcel_IOFactory::identify($inputfilename); 
    $objReader = PHPExcel_IOFactory::createReader($inputfiletype); 
    $objPHPExcel = $objReader->load($inputfilename); 
} 
catch(Exception $e) 
{ 
    die('Error loading file "'.pathinfo($inputfilename,PATHINFO_BASENAME).'": '.$e->getMessage()); 
} 

// Get worksheet dimensions 
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn(); 

$header=$_POST['membership']; 

if($header==1) 
{ 
// Loop through each row of the worksheet in turn 
for ($row = 1; $row <= $highestRow; $row++) 
{ 

    // Read a row of data into an array 
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); 
    if ($rowData[0][9]=='') 
    { 
     echo 'No price for the product '.$rowData[0][1].''; 
    } 
    else 
    { 
    $a = array('8', '1', '2', '3', '4', '5','6'); 
    $b = array($rowData[0][0], $rowData[0][2], $rowData[0][3],$rowData[0][5],$rowData[0][6],$rowData[0][8],$rowData[0][8]); 
    $c = array_combine($a, $b); 

    $slug = str_replace(" ", "-",$rowData[0][1]); 
$slug = str_replace('"', "",$slug); 
$slug = str_replace('/', "-",$slug); 
    $stmt=$dbh->prepare("INSERT INTO tbl_products (name,slug,description,price) 
        VALUES (:name,:slug,:desc,:pret)"); 

$stmt->bindParam(":name",$rowData[0][1]); 
$stmt->bindParam(":slug",$slug); 
$stmt->bindParam(":desc",$rowData[0][10]); 
$stmt->bindParam(":pret",$rowData[0][9]); 

$stmt->execute(); 
$id_product=$dbh->lastInsertId(); 

$stmt=$dbh->prepare("INSERT INTO tbl_products_images_gallery (id_product,name,image,sort_order) 
        VALUES (:id,:name,:image,100)"); 

$stmt->bindParam(":id",$id_product); 
$stmt->bindParam(":name",$rowData[0][4]); 
$stmt->bindParam(":image",$slug); 

$stmt->execute(); 

$stmt=$dbh->prepare("SELECT id_category from tbl_catalog_categories where name=:name"); 
$stmt->bindParam(":name",$rowData[0][2]); 
$stmt->execute(); 
if($row=$stmt->fetch()) 
{ 
    $id_cat=$row['id_category']; 
} 

$stmt=$dbh->prepare("INSERT INTO tbl_products_to_categories (id_category,id_product) 
        VALUES (:id_cat,:id_prod)"); 

$stmt->bindParam(":id_cat",$id_cat); 
$stmt->bindParam(":id_prod",$id_product); 

$stmt->execute(); 

foreach($c as $key => $value) 
    { 

     $stmt=$dbh->prepare("INSERT INTO tbl_products_attributes_values (id_product,id_attribute,attribute_value) 
        VALUES (:id_product,:id_attribute,:value)"); 
$stmt->bindParam(":id_product",$id_product); 
$stmt->bindParam(":id_attribute",$key); 
$stmt->bindParam(":value",$value); 
$stmt->execute(); 
    } 


} 
} 
} 

回答

0

if (empty($rowData[0][9]))吧?

或测试空以及为空字符串 - 零点从PHPExcel完全有效的响应...

尤其是

$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); 

与它的空第二个参数是告诉PHPExcel返回一个空值如果小区根本没有在电子表格中

存在虽然你可以改变rangeToArray()调用

$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, '', TRUE, FALSE); 

返回一个空字符串,而不是null如果单元格不存在

+0

它仍然在else上并返回sql错误,它插入第一行,并在第二行上它应该返回if回显但它在else上发生致命错误:未捕获的PDOException:SQLSTATE [23000]:完整性约束违规:1048'id_category'列在/home/r35345publ/anticariat.umbrellamedia.ro/admin/xls.php:99中不能为空栈trace:#0 /home/r35345publ/anticariat.umbrellamedia.ro/admin/xls.php(99):PDOStatement-> execute()#1 {main}抛出/home/r35345publ/anticariat.umbrellamedia.ro/admin/ 99行上的xls.php – chris227

+0

那么你对单元格[0] [9]的检查只是检查价格,而不是类别....所以每行中的单元格包含类别? –

+0

它没关系,如果价格是空的,它不应该在其他所有插入的地方继续。 – chris227