Uploading CSV file into mysql database using PHP - php

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();
?>

Try
<?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();
?>

Related

load data infile function with PHP

I have tried to import CSV file into MySQL database using PHP and MySQL load data Infile function.The query looks correct but still there is an error, while embedding it inside php code. Would you Please help me.
<?php
//connect to the database
$host='localhost';
$username='';
$password='';
$database_name='cricket';
$connection=mysql_connect($host,$username,$password,$database_name);
if(!$connection)
{
echo "no connection made";
}
//
else {
$query="LOAD DATA INFILE 'cricket.csv'
INTO TABLE cricket_batsman
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'";
mysql_query($query);
}
?>

Need to upload stats data from a csv file to sql database automatically every day?

Ok so im trying to use phpjobscheduler to update stats every day, It runs a command every day to open the php page and updates the db.
I have Googled and this is the code I managed so far:
<?php
$mysqli = new mysqli('localhost','xxxxx','xxxxx','xxxxx');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "LOAD DATA LOCAL INFILE 'xxxxx.CSV' REPLACE INTO TABLE `Stats_Day` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (#txdate, department, code, descript, QTY, Total) SET txdate = STR_TO_DATE(#txdate, '%Y/%m/%d')";
$res = $mysqli->query($query);
if ($mysqli->error) {
try {
throw new Exception("MySQL error $mysqli->error <br> Query:<br> $query", $msqli->errno);
} catch(Exception $e ) {
echo "Error No: ".$e->getCode(). " - ". $e->getMessage() . "<br >";
echo nl2br($e->getTraceAsString());
}
}
mysqli_close($con);
?>
This however is not working it gives me a blank page,
and nothing is updated on the SQL-database?
it connects to the db
(enter wrong details it shows incorrect user details)
but is not updating the db ? and shows no errors?
Please assist?
all I need to do is to update the db with the data from the .csv file every day at 1am.
You need to escape the double quote at ENCLOSED BY part. Without that, you terminated your string.
$query = "LOAD DATA LOCAL INFILE 'xxxxx.CSV' REPLACE INTO TABLE `Stats_Day` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES (#txdate, department, code, descript, QTY, Total) SET txdate = STR_TO_DATE(#txdate, '%Y/%m/%d')";
Note
Use error_reporting(E_ALL), and ini_set('display_errors', 1); to cactch your errors while developing.

mysql load data infile through php script - not working

I am trying to use the mysql LOAD DATA LOCAL INFILE to get some csv data into my mysql database through a php script using mysqli. This is what my sql string looks like:
LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv' INTO TABLE dashmaker.HHdata FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;
This is what my php script looks like:
$sql = "LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv'
INTO TABLE dashmaker.HHdata
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;";
$con=mysqli_connect("localhost","[user]","[password]","[database]");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$result = mysqli_query($sql, $con);
if (mysql_affected_rows() == 1) {
$message = "The data was successfully added!";
} else {
$message = "The user update failed: ";
$message .= mysql_error();
};
echo $message;
mysqli_close($con);
I found that I needed to set the mysql my.cnf to include local-infile under [mysql] and [mysqld] - so I have done that.
When I run the sql query through the shell it works. When I try to do it through the php script the error message ($message) I now get says:
The user update failed: Access denied for user ''#'localhost' (using password: NO)
One weird thing is that it doesn't show any user name before the #'localhost'. Can't understand why. Besides this, I use the same connection setting to run regular SELECT queries from the database using php scripts. The user also has FILE privileges.
I have searched extensively but haven't found anything that can explain what's going on. Any advice would be much appreciated.
You're mixing MySQL APIs with mysql_ and mysqli_ functions in a few instances.
mysql_affected_rows()
mysql_error()
They do not mix together; use mysqli_ exclusively.
Plus, you're not using brackets in [user] etc, are you? That is MSSQL syntax, remove them.
Plus, in mysqli_, DB connection comes first, invert these $result = mysqli_query($sql, $con); to read as $result = mysqli_query($con, $sql);
$sql = "LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv'
INTO TABLE dashmaker.HHdata
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;";
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$result = mysqli_query($con, $sql);
if (mysqli_affected_rows($con) == 1) {
$message = "The data was successfully added!";
} else {
$message = "The user update failed: ";
$message .= mysqli_error($con);
};
echo $message;
mysqli_close($con);

import glob text file from php to mysql

My glob code finds the file and attempts to insert it into the table but fails. I couldn't find anyone that is asking the same question. I will have many files that need to be inserted into the same table. It is a coma delimited text file.
Updated code*
The following code works as long as I rem the load data line. When I use the load data command manually, it works. I have not found a post example that has solved the problem "Why does the load data command not work?"
$con = new mysqli('localhost', 'username', 'password', 'schaadan_hash');
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Connected. ";
echo "<br />";
}
$upload = "LOAD DATA LOCAL INFILE 'ntlmhash2_1.txt' INTO TABLE ntlm2 FIELDS TERMINATED BY ',' ENCLOSED BY '' ESCAPED BY '\\' LINES TERMINATED BY '\n'(`clear` , `hash`);";
mysqli_query($con,$upload);
?>

php foreach() & glob() INSERT INTO w/ exec() is only INSERTING last .csv (of 30) into table

The last .csv file is processed and loaded into myTable, but there are 30 other .csv files prior to that - the .csv files are all being read because I echoed them out to the CLI and they are printed to the screen, but I cannot resolve why ONLY the last .csv is being INSERTED into myTable? Any ideas?
$mysqli = new mysqli("localhost", "root", "", "mydb");
$dir = "H:/path/20130116/";
$files = "*.csv";
foreach(glob($dir.$files) as $file) {
exec("mysql -u root -e \"USE mydb; myTable; LOAD DATA INFILE '" . $file . "' INTO TABLE myTable FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n'\"; ");
}
Upon more and more testing, removing the first call to myTable; in the MySQL line seems to have resolved the problem.

Categories