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();
}
}
}
}
它仍然在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] [9]的檢查只是檢查價格,而不是類別....所以每行中的單元格包含類別? –
它沒關係,如果價格是空的,它不應該在其他所有插入的地方繼續。 – chris227