I am having a problem uploading a CSV file into my MySQL database. I'm not a very experienced coder so sorry if my attempt at solving the problem is way off, but I am going to include what I have tried so far.
Here is the HTML portion of the code:
<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>
And here is the PHP portion:
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);
The top two lines in the CSV file are not important, and there is one line that is the headers, so that's why I have it skipping the first three lines. The date format in the file is 6/5/1999, so I assumed I had to try to change that. I put "LINES TERMINATED BY '\n' IGNORE 3 LINES" but I'm not sure if I should even have that, the lines aren't terminated by anything.
I am using phpMyAdmin for my database, in case that is important.
When I try to execute this, it only goes to a page that says the page isn't working... so I know I'm probably way off or missing things. Any help would be appreciated, thanks!
EDIT: Here is my PHP file with the changes suggested by Vasiliy Zverev. Now it is not working because apparently phpMyAdmin has problems with the LOAD DATA LOCAL INFILE command?
<?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();
?>