我有一个清单程序,该程序可以接受excel文件并将其转换为数据库,并且在唯一键中有一个名为hostname
的列名。
问题出在excel文件中,如果有空白值,它将插入hostname
中,但是如果有另一个空白值,数据库将认为它不是唯一的。我搜索了为什么它不能正常工作,显然只能一次又一次插入NULL
,因为空白和NULL之间存在差异,我希望将excel中的空白值转换为NULL,以便可以将其正确插入数据库中hostname
。
$allowedFileType = ['application/vnd.ms-
excel','text/xls','text/xlsx','application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType)){
$targetPath = 'uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$Reader = new SpreadsheetReader($targetPath);
$sheetCount = count($Reader->sheets());
for($i=0;$i<$sheetCount;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$cubicle = "";
if(isset($Row[0])) {
$cubicle = mysqli_real_escape_string($con,$Row[0]);
}
$hostname = "";
if(isset($Row[1])) {
$hostname = mysqli_real_escape_string($con,$Row[1]);
}
$groups = "";
if(isset($Row[2])) {
$groups = mysqli_real_escape_string($con,$Row[2]);
}
$batchfile = "";
if(isset($Row[3])) {
$batchfile = mysqli_real_escape_string($con,$Row[3]);
}
$macaddress = "";
if(isset($Row[4])) {
$macaddress = mysqli_real_escape_string($con,$Row[4]);
}
$ipaddress = "";
if(isset($Row[5])) {
$ipaddress = mysqli_real_escape_string($con,$Row[5]);
}
$operatingsystem = "";
if(isset($Row[6])) {
$operatingsystem = mysqli_real_escape_string($con,$Row[6]);
}
$build = "";
if(isset($Row[7])) {
$build = mysqli_real_escape_string($con,$Row[7]);
}
$manufacturer = "";
if(isset($Row[8])) {
$manufacturer = mysqli_real_escape_string($con,$Row[8]);
}
$physicalmemory = "";
if(isset($Row[9])) {
$physicalmemory = mysqli_real_escape_string($con,$Row[9]);
}
$model = "";
if(isset($Row[10])) {
$model = mysqli_real_escape_string($con,$Row[10]);
}
$serialnumber = "";
if(isset($Row[11])) {
$serialnumber = mysqli_real_escape_string($con,$Row[11]);
}
$assetnumber = "";
if(isset($Row[12])) {
$assetnumber = mysqli_real_escape_string($con,$Row[12]);
}
$ponumber = "";
if(isset($Row[13])) {
$ponumber = mysqli_real_escape_string($con,$Row[13]);
}
$deliverydate = "";
if(isset($Row[14])) {
$deliverydate = mysqli_real_escape_string($con,$Row[14]);
}
$division = "";
if(isset($Row[15])) {
$division = mysqli_real_escape_string($con,$Row[15]);
}
$warranty = "";
if(isset($Row[16])) {
$warranty = mysqli_real_escape_string($con,$Row[16]);
}
$lifecycle = "";
if(isset($Row[17])) {
$lifecycle = mysqli_real_escape_string($con,$Row[17]);
}
if (!empty($cubicle) || !empty($hostname) || !empty($groups) || !empty($batchfile) || !empty($macaddress) || !empty($ipaddress) || !empty($operatingsystem) || !empty($build) || !empty($manufacturer) || !empty($physicalmemory) || !empty($model) || !empty($serialnumber) || !empty($assetnumber) || !empty($ponumber) || !empty($deliverydate) || !empty($division) || !empty($warranty) || !empty($lifecycle)) {
$query = "insert into computer(cubicle, hostname, groups, batchfile, macaddress, ipaddress, operatingsystem, build, manufacturer, physicalmemory, model, serialnumber, assetnumber, ponumber, deliverydate, division, warranty, lifecycle) values('".$cubicle."','".$hostname."', '".$groups."', '".$batchfile."', '".$macaddress."', '".$ipaddress."', '".$operatingsystem."', '".$build."', '".$manufacturer."', '".$physicalmemory."', '".$model."', '".$serialnumber."', '".$assetnumber."', '".$ponumber."', '".$deliverydate."', '".$division."', '".$warranty."', '".$lifecycle."')";
$result = mysqli_query($con, $query);
答案 0 :(得分:0)
Escape +引用字符串(如果可用)。否则将其设置为字符串"NULL"
$item = isset($Row[123])
? '"' . mysqli_real_escape_string($con,$Row[123]) . '"'
: "NULL";
然后,在SQL查询中:不再引用。
答案 1 :(得分:0)
我设法解决了这个问题,我只是添加了一个代码$hostname = !empty($hostname) ? "'$hostname'" : "NULL";
,并删除了查询中的引号。