我在将CSV文件上传到MySQL数据库时遇到问题。我不是一个非常有经验的编码员,所以如果我试图解决这个问题的方法已经过时了,那我很抱歉,但是我将包括我到目前为止所尝试的内容。
以下是代码的HTML部分:
<form enctype="multipart/form-data" action="camperUpload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" class="btn btn-border" />
</form>
这是PHP部分:
function processFile($uploadedFile) {
// file contents
$file_contents = $uploadedFile["tmp_name"];
$SQL_statement = "LOAD DATA LOCAL INFILE '$file_contents' INTO TABLE C_CAMPER FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' IGNORE 3 LINES
(camperFName,campLName,camperAddress,camperCity,camperZip,camperCountry,camperPhone,camperEmail,@camperAltEmail,@camperDOB,camperAge,camperSchool,camperGradYear,camperGPA,camperACT/SAT,camperPar,camperParPhone,camperPrimPos,camperSecPos);";
SET camperDOB = STR_TO_DATE(@camperDOB, '%b-%d-%Y'),
camperAltEmail = NULLIF(@camperAltEmail, 'null');"
// Run SQL query
$DB->execute($SQL_statement);
CSV文件中的前两行并不重要,并且有一行是标题,这就是为什么我跳过前三行的原因。文件中的日期格式是6/5/1999,所以我假设我必须尝试更改它。我把&#34; LINES TERMINATED BY&#39; \ n&#39; IGNORE 3 LINES&#34;但我不确定我是否应该这样做,线条不会被任何东西终止。
我正在使用phpMyAdmin作为我的数据库,以防这很重要。
当我尝试执行此操作时,它只会转到一个页面,表明该页面不起作用...所以我知道我可能会离开或丢失一些东西。任何帮助将不胜感激,谢谢!
编辑: 这是我的PHP文件,其中包含Vasiliy Zverev建议的更改。现在它不工作,因为显然phpMyAdmin有LOAD DATA LOCAL INFILE命令的问题?
<?php
// version 1.02
// display errors for debugging
ini_set("display_errors", true);
error_reporting(E_ALL);
// Open a connection to the SQL server so we can run queries later.
$conn = new mysqli(removed for privacy); // DON'T FORGET TO EDIT THIS PART!
// Output error info if there was a connection problem
if ($conn->connect_errno) {
die("<h3> Uh oh! It looks like we're having trouble connecting to the
website at the moment. Try again soon! {$conn->connect_error}</h3>");
}
// file name
$file_contents = $_FILES["uploadedfile"]["tmp_name"];
$SQL_statement = "LOAD DATA LOCAL INFILE '$file_contents' INTO TABLE C_CAMPER FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' IGNORE 3 LINES
(camperFName,campLName,camperAddress,camperCity,camperZip,camperCountry,camperPhone,camperEmail,@camperAltEmail,@camperDOB,camperAge,camperSchool,camperGradYear,camperGPA,`camperACT/SAT`,camperPar,camperParPhone,camperPrimPos,camperSecPos)
SET camperDOB = STR_TO_DATE(@camperDOB, '%m/%d/%Y'),
camperAltEmail = NULLIF(@camperAltEmail, 'null');";
// Run SQL query
if( !$conn->query($SQL_statement)) {
echo $conn->error;
}
// Close the SQL connection
$conn->close();
?>
答案 0 :(得分:0)
尝试
<?php
// version 1.02
// display errors for debugging
ini_set("display_errors", true);
error_reporting(E_ALL);
// Open a connection to the SQL server so we can run queries later.
$conn = new mysqli(removed for privacy); // DON'T FORGET TO EDIT THIS PART!
// Output error info if there was a connection problem
if ($conn->connect_errno) {
die("<h3> Uh oh! It looks like we're having trouble connecting to the
website at the moment. Try again soon! {$conn->connect_error}</h3>");
}
// file name
$file_contents = $_FILES["uploadedfile"]["tmp_name"];
$SQL_statement = "LOAD DATA LOCAL INFILE '$file_contents' INTO TABLE C_CAMPER FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' IGNORE 3 LINES
(camperFName,campLName,camperAddress,camperCity,camperZip,camperCountry,camperPhone,camperEmail,@camperAltEmail,@camperDOB,camperAge,camperSchool,camperGradYear,camperGPA,`camperACT/SAT`,camperPar,camperParPhone,camperPrimPos,camperSecPos)
SET camperDOB = STR_TO_DATE(@camperDOB, '%m/%d/%Y'),
camperAltEmail = NULLIF(@camperAltEmail, 'null');";
// Run SQL query
if( !$conn->query($SQL_statement)) {
echo $conn->error;
}
// Close the SQL connection
$conn->close();
?>