SalientAnimal Posted January 16, 2013 Share Posted January 16, 2013 Hi All, I want to create a webpage that allows a user to import a csv file into a database. Can anyone help me start with this? Or at least refer me to a really good source that can help me get this working. Link to comment https://forums.phpfreaks.com/topic/273229-csv-import-page/ Share on other sites More sharing options...
Christian F. Posted January 16, 2013 Share Posted January 16, 2013 This can be an easy question, a hard one, or something in the middle. All depending upon how the data in said CSV looks like. For most cases it's not as simple as just dumping the data directly into the database, as you'd end up with a lot of duplicated data in your database. So what I recommend you to do first, is to read up on database normalization. The video below is a great starting point, to show the principle behind normalization. Also, to get the CSV data from the file, you'll need the fgetscv () function. Link to comment https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406065 Share on other sites More sharing options...
White_Lily Posted January 16, 2013 Share Posted January 16, 2013 Try this: http://www.google.co...iw=1280&bih=933 Also to add to Christian's comment, data within a CSV could also be "unclean" such as characters that PHPMyAdmin (or whatever database your using) won't like. Link to comment https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406066 Share on other sites More sharing options...
Christian F. Posted January 16, 2013 Share Posted January 16, 2013 To nitpick a bit: phpMyAdmin isn't a database. It's a frontend to the MySQL database. Link to comment https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406069 Share on other sites More sharing options...
White_Lily Posted January 16, 2013 Share Posted January 16, 2013 Regardless, the database still won't like some characters. Link to comment https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406074 Share on other sites More sharing options...
Christian F. Posted January 16, 2013 Share Posted January 16, 2013 That's what escaping is for, yes. SalientAnimal: I just noticed that you have posted this in the "PHP coding help" section. It would have been better suited for the "Application design" section, as this section is for getting help with code you have written (yourself). Not how to plan the design of the code you are going to write. Link to comment https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406076 Share on other sites More sharing options...
SalientAnimal Posted January 16, 2013 Author Share Posted January 16, 2013 Ok, well I eventually managed to find some thing that did the trick :-). Yes I am guild of the cardinal sin of copy and pasting code without actually fully understanding it. The problem I have not, and what I want to add to this code is: 1. Ignore the first line of the csv file (i.e. Headings) 2. On the success page, display the total line written to the database. The Code: (FORM) <html> <body> <form action="submit/submit_sales_upload.php" method="post" enctype="multipart/form-data"> <label for="file">Source File:</label> <input name="csv" type="file" id="csv" /> <input type="submit" name="submit" value="Upload File"> </form> </body> </html> (SUBMIT) <?php $con = mysql_connect("localhost","root","mypassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mydatabase", $con); if ($_FILES[csv][size] > 0) { if ($row == 1){ $row++; continue; } //get the csv file $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"r"); //loop through the csv file and insert into database do { if ($data[0]) { mysql_query("INSERT INTO database.salesleads_ctr (champ ,msisdn ,alt_number ,id_number ,account_number ,sub_id ,customer_type ,campaign_code ,max_rrp_renew ,max_rrp_upgrade ,current_package ,cancellation_date) VALUES ( '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."', '".addslashes($data[3])."', '".addslashes($data[4])."', '".addslashes($data[5])."', '".addslashes($data[6])."', '".addslashes($data[7])."', '".addslashes($data[8])."', '".addslashes($data[9])."', '".addslashes($data[10])."', '".addslashes($data[11])."' ) "); } } while ($data = fgetcsv($handle,1000,",","'")); // //redirect header('Location: redirect_import.php?success=1'); die; } ?> Link to comment https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406115 Share on other sites More sharing options...
SalientAnimal Posted January 17, 2013 Author Share Posted January 17, 2013 Anyone have any advice here for me please? Link to comment https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406436 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.