我正在尝试使用 phpexcel 让用户上传电子表格并插入 mysql。我让它工作,除非用户在电子表格中有空白行,它会在数据库中放置几个不必要的行。我已经阅读并且 mysql 不支持检查约束,其中大部分需要在应用程序级别完成,这是我正在尝试做的。PHP 如何让它检查特定列是否为空并跳过该行。任何帮助将不胜感激,或者如果有更好的解决方案,我愿意接受所有选择。我尝试了一段时间才知道该bldg
列是空的,而不是跳过该行但没有任何运气..
<?php require '../Classes/PHPExcel.php';
require_once '../Classes/PHPExcel/IOFactory.php';
$path = "../upload/uploads/$Filename";
$objPHPExcel = PHPExcel_IOFactory::load($path);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = 'L'; // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
$objReader = PHPExcel_IOFactory::createReaderForFile($path);
$objReader->setReadDataOnly(true);
echo '<br>Data: <table width="100%" cellpadding="3" cellspacing="0"><tr>';
for ($row = 16; $row <= $highestRow; ++ $row) {
echo '<tr>';
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
if($row === 1)
echo '<td style="background:#000; color:#fff;">' . $val . '</td>';
else
echo '<td>' . $val . '</td>';
}
echo '</tr>';
}
echo '</table>';
for ($row = 16; $row <= $highestRow; ++ $row) {
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
$sql="insert into excess1(date_excessed, bldg, floor, jack, equipment_owner, contact_name, contact_phone, qty, type_of_excess, asset_tag, service_tag, comments)
values('".$val[0] . "','" . $val[1] . "','" . $val[2]. "','" . $val[3]. "','" . $val[4]. "','" . $val[5]. "','".$val[6] . "','".$val[7] . "','".$val[8] . "','".$val[9] . "','".$val[10] . "','".$val[11] . "')";
$result = mysql_query($sql) or die(mysql_error());
print "$result";
}
}
unlink("../upload/uploads/$Filename");
?>
</article>