我猜你不必查看整个代码,但无论如何我都会包含它。
<?php if (!$_POST) { ?>
<!DOCTYPE html>
<html>
<div class="header">
<img src="images/logo.png" alt="logo" />
</div>
<body background="images/background.png" >
<form action="" method="post" enctype="multipart/form-data">
Choose your file: <br />
<input name="csv" type="file" id="csv" /> <br /> <br />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
<?php
} else {
$connect = new mysqli("localhost", "username", "password", "csvdb");
if ($_FILES['csv']['size'] > 0) {
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file, "r");
$i = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($i > 0) {
$import = "INSERT into csvtb(project_id,unit_id,phase,building,level,orientation,apartment_type,size,garden,garden_and_terrace_size,bedrooms,parking,floorplan,sold) values('$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]''$data[11]','$data[12]','$data[13],'$data[14])";
$connect->query($import);
}
$i++;
}
fclose($handle);
print "Import done";
}
}
?>
错误在第30行,未定义的偏移量
$import = "INSERT into csvtb(project_id,unit_id,phase,building,level,orientation,apartment_type,size,garden,garden_and_terrace_size,bedrooms,parking,floorplan,sold) values('$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]''$data[11]','$data[12]','$data[13],'$data[14])";
$connect->query($import);
我使用许多php在线检查工具检查了我的代码,他们说代码很干净。我想我知道错误是什么,我猜是因为我没有插入索引0,因为我希望它是我的数据库的id。我不确定,如果我是,我不会问。我的PHP技能并不是那么好。
答案 0 :(得分:0)
CSV中的某些行可能比提供给查询的数据少。我建议以可以记录/调试的方式处理它。
使用您的示例代码,如果未发现数据有效,我刚刚完成了一些早期退出。您可以应用相同类型的逻辑并打印出不具有总共15个数组元素的行。看来你正在跳过元素0并选择1..14。那绝对没问题;只需检查以确保所有行都为您提供15个数组元素。
<?php
if ($_POST)
{
$connect = new mysqli("localhost", "username", "password", "csvdb");
if ($_FILES['csv']['size'] > 0)
{
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file, "r");
$i = 0;
$expected_array_count = 15;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
// skip first line
if ($i == 0) { i++; continue; }
// print offending line and continue to the next line
// if array does not have the expected count
if (count(data) !== $expected_array_count)
{
echo sprintf("Line %d has %d lines; %d expected: %s",
$i+1,
count(data),
$expected_array_count,
implode("~~", $array)
);
i++;
continue;
}
performImport($data);
$i++;
}
fclose($handle);
print "Import done";
}
<?php
}
else
{
<!DOCTYPE html>
<html>
<div class="header">
<img src="images/logo.png" alt="logo" />
</div>
<body background="images/background.png" >
<form action="" method="post" enctype="multipart/form-data">
Choose your file: <br />
<input name="csv" type="file" id="csv" /> <br /> <br />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
}
// function does the importing
function performImport($data)
{
global $connect;
// instead of entering data like this, there are
// better alternatives
$import = "
INSERT into csvtb
(
project_id,unit_id,phase,
building,level,orientation,
apartment_type,size,garden,
garden_and_terrace_size,bedrooms,parking,
floorplan,sold
)
values
(
'$data[1]','$data[2]','$data[3]',
'$data[4]','$data[5]','$data[6]',
'$data[7]','$data[8]','$data[9]',
'$data[10]','$data[11]','$data[12]',
'$data[13]','$data[14]'
)";
// one alterate
// $import = sprintf("insert into ... values (%d, %s, %d...)", $data[1], $data[2] ...)
// another alternate (http://php.net/manual/en/mysqli-stmt.bind-param.php)
// $import = "insert into ... values (?, ?, ...)"
// mysqli_stmt_bind_param($import, 'sss...', $data[1], $data[2]...);
// yet another alternate: use PDO
$connect->query($import);
}
?>