jigen7 Posted October 9, 2007 Share Posted October 9, 2007 i have gotten this script that imports excel to mysql data i need to modify this so that i only need to insert certain columns in the table how do i do that for example i only need to insert values in the userid and username fields??thx <? $fcontents = file ('./spreadsheet.xls'); # expects the csv file to be in the same dir as this script for($i=0; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); $arr = explode("\t", $line); #if your data is comma separated # instead of tab separated, # change the '\t' above to ',' $sql = "insert into TABLENAME values ('". implode("','", $arr) ."')"; // i think this is the line need to be change mysql_query($sql); echo $sql ."<br>\n"; if(mysql_error()) { echo mysql_error() ."<br>\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72395-solved-help-modifying-excel-to-sql-import-script/ Share on other sites More sharing options...
trq Posted October 9, 2007 Share Posted October 9, 2007 Just prior to this line.... $sql = "insert into TABLENAME values ('". implode("','", $arr) ."')"; Place this. print_r($arr); This will let you see what array elements you need. From there all you need do is change the query to something like.... (I use 2 and 3 as an example.) $sql = "insert into TABLENAME values (uid,upname) VALUES ('{$arr[2]}','{$arr[3]}')"; Quote Link to comment https://forums.phpfreaks.com/topic/72395-solved-help-modifying-excel-to-sql-import-script/#findComment-365110 Share on other sites More sharing options...
jigen7 Posted October 9, 2007 Author Share Posted October 9, 2007 oks ill try that hope it works thx Quote Link to comment https://forums.phpfreaks.com/topic/72395-solved-help-modifying-excel-to-sql-import-script/#findComment-365111 Share on other sites More sharing options...
jigen7 Posted October 9, 2007 Author Share Posted October 9, 2007 so implode statement will be erased? Quote Link to comment https://forums.phpfreaks.com/topic/72395-solved-help-modifying-excel-to-sql-import-script/#findComment-365114 Share on other sites More sharing options...
trq Posted October 9, 2007 Share Posted October 9, 2007 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/72395-solved-help-modifying-excel-to-sql-import-script/#findComment-365116 Share on other sites More sharing options...
jigen7 Posted October 9, 2007 Author Share Posted October 9, 2007 wahh how come there are many array[0]?? when it print it outputted like this , is this alright??? suppose i want to save all that values in sql?? will the given code can do it?? Array ( [0] => URL [1] => Title [2] => Description [3] => Email [4] => SubmitURL [5] => LinkCategoryName [6] => UserName [7] => ContactedOn ) Array ( [0] => http://www.online-store-list.com/ [1] => http://www.online-store-list.com/ [2] => "Www.online-store-list.com is your source of links to electronics online store as well as cheap prices on computer, television, furniture, book, movie, music, office supply and more from big name online stores like amazon." [3] => webmaster@online-store-list.com [4] => http://www.online-store-list.com/ContactUs.html [5] => Shopping [6] => jigen7 [7] => 3/15/2005 12:59 ) Array ( [0] => http://www.lifesourcenutrition.co.uk/ [1] => http://www.lifesourcenutrition.co.uk/ [2] => Nutrition Resources. [3] => sales@chcinternet.com [4] => http://www.lifesourcenutrition.co.uk/index.php?ass=1430&qry=clothes [5] => Medical [6] => jigen7 [7] => 3/15/2005 12:50 ) Array ( [0] => http://www.seniority.co.uk/ [1] => Submit your Shopping url for free to Seniority Directory [2] => Free links directory. [3] => eldie.aban@harvestseo.com [4] => http://www.seniority.co.uk/directory/add/?catID=193 [5] => Link Directory [6] => jigen7 [7] => 5/26/2005 7:48 ) Array ( [0] => http://www.services-24.com/ [1] => Services 24 [2] => "Directory of UK information and services with specialist categories for business, cars, computers and the internet, finance, health, home, leisure, media, shopping, society, sport, travel and work." [3] => webmaster@services-24.com [4] => http://www.services-24.com/Shops/clothing.htm [5] => Services [6] => jigen7 [7] => 3/16/2005 13:48 ) Array ( [0] => http://accessories--wigs-.lessonbook.com/ [1] => The best search engine on the net! [2] => Travel debt consolidation investing business employment antiques books mortgage jobs shopping stocks accounting sports banking make money clothes partner programs credit cards. [3] => [4] => http://accessories--wigs-.lessonbook.com/search/link_to_us.asp [5] => Shopping [6] => jigen7 [7] => 3/15/2005 13:14 ) Quote Link to comment https://forums.phpfreaks.com/topic/72395-solved-help-modifying-excel-to-sql-import-script/#findComment-365121 Share on other sites More sharing options...
jigen7 Posted October 9, 2007 Author Share Posted October 9, 2007 what is wrong in theses statement? $sql = "insert into Links values('URL','Title','Description','Email','SubmitURL','ContactedOn') VALUES ('{$arr[0]}','{$arr[1]}','{$arr[2]}','{$arr[3]}','{$arr[4]}','{$arr[7]}')"; it gives me the error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUES ('http://www.shinku.com/','http://www.online-store-list.com/','"Www.onlin' at line 2 Quote Link to comment https://forums.phpfreaks.com/topic/72395-solved-help-modifying-excel-to-sql-import-script/#findComment-365129 Share on other sites More sharing options...
jigen7 Posted October 9, 2007 Author Share Posted October 9, 2007 ok it works now hehe lol thx Quote Link to comment https://forums.phpfreaks.com/topic/72395-solved-help-modifying-excel-to-sql-import-script/#findComment-365161 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.