+5 votes
234 views
Import a file from a calculation template to Mysql with php

in PHP by (551k points)
reopened | 234 views

1 Answer

+3 votes
Best answer

Some web projects require managing large lists of data such as product listings, sales, user, news.

Many people find it easier to have this data offline in a spreadsheet .
The objective of this tutorial is to be able to perform the import independently of the Excel program or Libreoffice or another option available to the user.

After creating a MySQL database, you need to populate it with information. The database data is usually contained in a limited file with commas, or CSV, and needs to be imported into the MySQL database. This is a very simple task using the built-in functions that PHP has that make it easy to import from a CSV file and export it to a MySQL database.

In this case we have a list of prices, service and parts of computers.

image


Now, no matter what the spreadsheet is and in what operating system we are working, we will look for the option to export to text csv format separated by semicolons; this will generate a simple text file where the columns will be replaced by; and we can read it from any platform or language.

image


Let's now create in a mysql database suppose Services and create prices where we will add the information of the csv file

[/color] [color = # 0000cd] CREATE TABLE IF NOT EXISTS `prices` ( [/ color]
[/color] [color = # 0000cd] `id` int (10) NOT NULL AUTO_INCREMENT, [/ color]
[/color] [color = # 0000cd] ` concept` varchar (255) DEFAULT NULL, [/ color]
[/color] [color = # 0000cd] `price` decimal (10,2) DEFAULT NULL, [/ color]
[/color] [color = # 0000cd] PRIMARY KEY (`id`) [/ color]
[color = # 0000cd] ) ENGINE = MyISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1; [/colour]

Now the php code that does all the work


[color = # 0000cd] <? [/color] // connect to the database [/ color]
[color = # 0000cd] $ connect = mysql_connect ('localhost', 'user', 'key'); [/colour]
[/color] [color = # 0000cd] if (! $ connect) { [/ color]
[color = # 0000cd] die ('Can not connect to MySQL:'. mysql_error ()); [/colour]
[/color] [color = # 0000cd] } [/ color]
[color = # 0000cd] $ conexiondb = mysql_select_db ('Services', $ connect); [/colour]
[/color] [color = # 0000cd] // We upload the csv file that comes from the form [/ color]
[color = # 0000cd] move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], $ upload_dir. "/". $ FILES ["file"] ["tmp_name"]); [/colour]
[color = # 0000cd] $ cvs = $ upload_dir. "/". $ _FILES ["ficchero"] ["name"]; [/colour]
[/color] [color = # 0000cd] // row will count the number of rows in the file starts at 0 [/ color]
[color = # 0000cd] $ row = 0; [/colour]
[color = # 0000cd] $ fp = fopen ($ cvs. "", "r"); [/colour]
[/color] [color = # 0000cd] while (! feof ($ fp)) {// read the file one row at a time [/ color]
[color = # 0000cd] $ row ++; [/colour]
[/color] [color = # 0000cd] // I skip the first row since it has the titles and I'm not interested in inserting them in the database [/ color]
[/color] [color = # 0000cd] if ($ row> 1) { [/ color]
[color = # 0000cd] // The row that I read separated it with explode and the Indian that separates by; [/colour]
[color = # 0000cd] $ data = explode (";", fgets ($ fp)); [/colour]
[color = # 0000cd] $ id = $ data [0]; [/colour]
[color = # 0000cd] $ concept = $ data [1]; [/colour]
[color = # 0000cd] $ price = $ data [2]; [/colour]

[color = # 0000cd] $ query = "INSERT INTO prices (id, concept, price) VALUES ($ data [0], '$ data [1]', '$ data [2]')"; [/colour]

[color = # 0000cd] mysql_query ($ query, $ connect); [/colour]

[/color] [color = # 0000cd] // end of the cycle while [/ color]
[/color] [color = # 0000cd] } [/ color]
[/color] [color = # 0000cd] } [/ color]

[/color] [color = # 0000cd] // close the file [/ color]
[color = # 0000cd] fclose ($ csv); [/colour]

[color = # 0000cd] echo "Import finished !!"; [/colour]
[color = # 0000cd] mysql_close ($ connect); [/colour]
[/color] [color = # 0000cd] ?> [/ color]

You can also use this script and make it more flexible, allow the user to register the file in an html form with

[/color] [color = # 0000cd] <form action = "subircsv.php" method = "post" enctype = "multipart / form-data"> [/ color]
[/color] [color = # 0000cd] <input type = "file" name = "filecvs"> [/ color]
[/color] [color = # 0000cd] </ form> [/ color]

and thus be able to upload CSV files and import data from that CSV file to any data database mysql or even to several from a single file. There are many settings that can be made with this script and use it the way you want.

image


It can also be used when a lot of users must upload information to a web, client states or accounts, each one will be able to use the software that best suits them and then upload all of them in the same compatible format for all.

by (3.5m points)
edited

Related questions

+4 votes
1 answer
asked Jun 23, 2019 in PHP by backtothefuture (551k points) | 744 views
+4 votes
1 answer
asked Apr 27, 2020 in PHP by backtothefuture (551k points) | 1.2k views
+5 votes
1 answer
+5 votes
1 answer
asked Oct 6, 2019 in PHP by backtothefuture (551k points) | 1.6k views
+4 votes
1 answer
asked Oct 4, 2019 in PHP by backtothefuture (551k points) | 1.5k views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,632 questions
10,764 answers
510 comments
3 users