techker Posted October 21, 2012 Share Posted October 21, 2012 Hey guys im stuck on this code that keeps on putting " " in my tables? i save the xls file to csv comma delimited. when i upload it ,i get "name" "age".... i think it the '".addslashes($data[0])."', if ($_FILES[csv][size] > 0) { //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 $Year (Ecole,N_Fiche,Nom,Prenom,Code_P,Foyer,Secondaire,Annee,Sortie) VALUES ( '263', '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."', '".addslashes($data[5])."', '".addslashes($data[6])."', '".addslashes($data[4])."', '$Year', '".addslashes($data[7])."' ) "); } } while ($data = fgetcsv($handle,1000,",","'")); nice new look by the way. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/ Share on other sites More sharing options...
Barand Posted October 21, 2012 Share Posted October 21, 2012 You don't read any data until you get to the end of the loop with a do..while(). All the processing is on empty $data the first time through the loop. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386813 Share on other sites More sharing options...
techker Posted October 21, 2012 Author Share Posted October 21, 2012 sorry why do you mean read?i have a form that is used to realy the post.. <?php if (!empty($_GET[success])) { echo "<b>CSV Importer!.</b><br><br>"; } //generic success notice ?> </p> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> Choose your file: <br /> <input name="csv" type="file" id="csv" /> <input name="Year" type="hidden" value="<?=$Fiscale?>" /> <input type="submit" name="Submit" value="Submit" /> </form> csv looks like this 7864804 Abraham Mohamed Masculin 5 ABRM27079607 501 Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386815 Share on other sites More sharing options...
Barand Posted October 21, 2012 Share Posted October 21, 2012 what you have is do { process data } while (get data) until you get data there is no data to process Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386816 Share on other sites More sharing options...
techker Posted October 21, 2012 Author Share Posted October 21, 2012 while ($data = fgetcsv($handle,1000,",","'")); odd cause it works it inserts the data correctly but add quotes before and after the data Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386817 Share on other sites More sharing options...
Barand Posted October 21, 2012 Share Posted October 21, 2012 It's still an incorrect way to loop in that case. Try fgetcsv($handle,1000) and use the defaults as your code defines single quote as string delimiter. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386821 Share on other sites More sharing options...
techker Posted October 21, 2012 Author Share Posted October 21, 2012 its odd my csv file 2441343 Abatouy, Ch 1336 Actif 263 A 1 1.8E+08 the query seperates the first and last name from second colum(data[1]) and puts " "abatouy" "ch" Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386822 Share on other sites More sharing options...
Barand Posted October 21, 2012 Share Posted October 21, 2012 csv looks like this 7864804 Abraham Mohamed Masculin 5 ABRM27079607 501 Where are the commas? Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386830 Share on other sites More sharing options...
techker Posted October 22, 2012 Author Share Posted October 22, 2012 there isnt any..odd ah?and i save it as comma delimited.. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386837 Share on other sites More sharing options...
Barand Posted October 22, 2012 Share Posted October 22, 2012 What did you you specify as the field delimiter when you created it? Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386839 Share on other sites More sharing options...
techker Posted October 22, 2012 Author Share Posted October 22, 2012 nothing?opened the xls saves as CSV(comma delimited) Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386840 Share on other sites More sharing options...
Barand Posted October 22, 2012 Share Posted October 22, 2012 Perhaps it's tab delimited by default. Try while ($data = fgetcsv($handle,1000,"\t")); Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386844 Share on other sites More sharing options...
techker Posted October 22, 2012 Author Share Posted October 22, 2012 same 1 260 2441343 "Abatouy Cha" 263 A Actif Actif 1 how can it take this 2441343 Abatouy, Cha 1336 Actif 263 A 1 1.8E+08 i guess the, between the first and last name make it data[1] data [2] Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386846 Share on other sites More sharing options...
techker Posted October 22, 2012 Author Share Posted October 22, 2012 but its ok the way it is i just need to remove the "" Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386847 Share on other sites More sharing options...
Barand Posted October 22, 2012 Share Posted October 22, 2012 if you specify the correct field delimiters and string terminators then the quotes should disappear. At the moment you are specifying commas when there aren't any and single quote instead of double quote (default) as string terminators. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386849 Share on other sites More sharing options...
techker Posted October 22, 2012 Author Share Posted October 22, 2012 so how would i do that?cause it is inserting the correct data? Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386850 Share on other sites More sharing options...
Barand Posted October 22, 2012 Share Posted October 22, 2012 Change the string terminator from "'" (double-single-double) to '"' (single-double-single), or omit it, since double quote is the default, as I did in my earlier post.. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386852 Share on other sites More sharing options...
techker Posted October 22, 2012 Author Share Posted October 22, 2012 ok cool i will see if i can figure it out..thx for the help Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1386853 Share on other sites More sharing options...
fenway Posted October 24, 2012 Share Posted October 24, 2012 It's troubling that you export your data, but you don't understand how, or why, it works. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1387355 Share on other sites More sharing options...
techker Posted October 24, 2012 Author Share Posted October 24, 2012 why would you say that?there is a few csv file formats.. thx for the comment. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1387404 Share on other sites More sharing options...
fenway Posted October 25, 2012 Share Posted October 25, 2012 why would you say that?there is a few csv file formats.. thx for the comment. Yes, but you are supposed to know which one you chose -- i.e. separated by tab, quote spaces, etc. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1387585 Share on other sites More sharing options...
techker Posted October 25, 2012 Author Share Posted October 25, 2012 its in my first post comma delimited.thats the only option i have with ms-dos and mac Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1387643 Share on other sites More sharing options...
techker Posted October 26, 2012 Author Share Posted October 26, 2012 this worked by the way Change the string terminator from "'" (double-single-double) to '"' (single-double-single), or omit it, since double quote is the default, as I did in my earlier post.. Quote Link to comment https://forums.phpfreaks.com/topic/269747-stuck-on-csv-to-mysql-code/#findComment-1388043 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.