1

I am trying to learn how to upload new records to MySQL table but seem to be failing so far. My attempt:

$uploaded = $_FILES["fileupload"]["name"];
`$try = mysqli_query($connect,"LOAD DATA INFILE IGNORE $uploaded INTO TABLE allrecords");`

I get an error. It's expected though as the code is a mess. I tried searching in Google but all seem to offer only inserting each records one by one to the database. I used a tag to select the Excel CSV file (exported from Excel) then on the receiving PHP file I use this query.

I use LOAD DATA INFILE IGNORE because I want to upload new records to the database but not include records who's primary key already exist in the database. Also I'm not sure how to do LOAD DATA INFILE and match each fields. Do I need to rename each column names/headers in Excel CSV to match the field names in database?

8
  • Post your error messages or stack trace. Commented Aug 28, 2015 at 5:51
  • 1
    I think what he wants to do @TimBiegeleisen is to get those surrounding single quotes around the '/full/path/to/filename', and to ironically not ignore 1 lines, and tweak line 1 to be perfect Commented Aug 28, 2015 at 5:55
  • @Drew Post this as an answer. And the OP should have included error output. Then I would not have to take wild guesses. Commented Aug 28, 2015 at 5:57
  • ah, resurrect yours, don't know if I have the strength :> Commented Aug 28, 2015 at 5:57
  • 1
    yeah guys I just confirmed it over there on that Answer. If I don't do the single quotes I get error 1064. Put em back it, I get my export. OUTFILE and LOAD DATA INFILE are connected by the hip with syntax finickiness and share almost everything in common Commented Aug 28, 2015 at 6:03

1 Answer 1

1

As @drew wisely mentioned in his comments above, you are missing single quotes around the file name in your LOAD DATA statement, which can mess things up. You may also be missing a few other things which would prevent it from working.

Try using this PHP code instead:

$uploaded = $_FILES["fileupload"]["name"];
`$try = mysqli_query($connect,"LOAD DATA INFILE IGNORE '$uploaded' INTO TABLE allrecords FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY ',,,\r\n'");`

Here is that LOAD DATA command as separate (readable) query:

LOAD DATA INFILE IGNORE '$uploaded'
INTO TABLE allrecords
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY ',,,\r\n'

Note carefully that the double quote in OPTIONALLY ENCLOSED BY has to be escaped so PHP doesn't choke.

And as another aside comment, LOAD DATA is fairly primitive with regard to what it can do handling data. It is intended as a workhorse to bring in raw data to MySQL. For complex filtering and manipulations of your raw data, you are probably better off doing the work in MySQL proper.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Tim and Drew, I will try this now and get back to you :)
what did you mean better off doing the work in MySQL proper? Did you mean INSERT each rows one by one in a loop? I was going to do that first but I thought it would make the process run very slow?
I mean that if you have complex rules for selectively retaining/excluding certain records, you are better off just reading in everything in one shot and then cleaning up the table inside MySQL where you have a wealth of tools to help you do that.
Using: $try = mysqli_query($connect,"LOAD DATA INFILE IGNORE '$uploaded' INTO TABLE allrecords FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n'"); I get error: Parse error: syntax error, unexpected '' LINES TERMINATED BY '' (T_CONSTANT_ENCAPSED_STRING) in /home/hidden/public_html/subfolder/loader.php on line 41
I forgot to escape the double quotes. Check my updated answer. Sorry for the confusion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.