I am trying to figure out how to add functionality to import a CSV file (and XLSX file if possible) into a MySQL database using PHP PDO. I’ve looked up different methods on how to do it and none of them work for me even after I customize the code for my database and preferences.
Okay, so get the ball rolling the first thing I’d decide is to start with CSV only. Excel may be a nice addition later, but the three stages of coding are:
Make it work
Make it pretty
Make it fast
Excel would be part of step 2. Once we have a working solution we can check if we can generalise for more inputs.
Well, I already created a demo.csv file. It only has 1 record for now, but I can add more if needed.
Here is a simple screenshot.
Also, my precinct field defaults to a date and it shouldn’t be doing that at all. I’ll type in 02-03 and it changes to 3-Feb. I change the field type to text, but it keeps reverting back to 3-Feb each time the document is opened?
That’s a problem of the program you’re using there (excel?) trying to be smart. Usually when you open a CSV you get some options to indicate what type each column should be. Maybe that helps?
Anyway, have you figured out the next steps of the approach yet?
Certainly there are more steps in between. What are they? (rhetorical question when it comes to the owl)
If it helps, imagine you need to import users from a piece of paper by entering it into a form. What are the steps there? The steps for an automated import are similar.
Set the column format to “text” instead of letting Excel automatically choose one for you. It’s fairly good at choosing, but irritating for stuff like this. And for the printer serial numbers we used to import that it would usefully change into scientific notation.
@droopsnoot When I open the Excel CSV, it shows as 3-Feb. So, Excel thinks the value 02-03 is a date rather than text… (which makes sense since it can come out to be 02-03-2020.
However, it needs to be a text value (as you previously mentioned) to prevent the data from being altered.
Yes, but is Excel just displaying it like that, or when you look at the csv file in something else (something that doesn’t randomly change data formats, like notepad) is it actually saving the data as “03-Feb”?