techker Posted March 24, 2012 Share Posted March 24, 2012 hey guys i have an existing code that inserts from scv to 1 DB. is there a way to add 2 more? cause there is 3 db for the products products(product title-ful and short description) products_images(image file name) products_attribute_values(quantity and price) $csv->file_new_name_body = $filename; $csv->Process(_UPLOAD_DIR_); if (!$csv->processed) { $message .= 'CSV upload error: '.$csv->error; } else { $row = 0; if (($handle = fopen(_UPLOAD_DIR_.$csv->file_dst_name, "r")) !== false) { while (($data = fgetcsv($handle, 1000, ",", '"')) !== false) { $sql = "INSERT INTO ".$TABLES['PRODUCTS']." SET categories_id = ".intval($_REQUEST['categories_id']).", order_id = 0, status = ".intval($_REQUEST['status']).", featured = 0, product_title = '".trim(saveToDB($data[0]))."', short_description = '".trim(saveToDB($data[1]))."', full_description = '".trim(saveToDB($data[2]))."'"; $sql_result = mysql_query($sql, $connection) or die ('Could not execute SQL query:<br />'.$sql.'<br /><strong>'.mysql_error().'</strong>'); Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/ Share on other sites More sharing options...
cpd Posted March 25, 2012 Share Posted March 25, 2012 To clarify, do you want to insert specific data into one table and other specific data into another table all from a single row in your .csv file? Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331062 Share on other sites More sharing options...
techker Posted March 25, 2012 Author Share Posted March 25, 2012 Ya.for the product i got a few filds.but the script has it in 3 diffrent tables..one for name and details .one fo images and the third for priceand qty... Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331074 Share on other sites More sharing options...
cpd Posted March 26, 2012 Share Posted March 26, 2012 Then inside your loop just create a second and third INSERT statement and execute it. Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331124 Share on other sites More sharing options...
techker Posted March 26, 2012 Author Share Posted March 26, 2012 how would i read it from the csv? is it this? short_description = '".trim(saveToDB($data[1]))."', full_description = '".trim(saveToDB($data[2]))."'"; Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331156 Share on other sites More sharing options...
cpd Posted March 26, 2012 Share Posted March 26, 2012 It depends where in the array the short description is. If its in the first column then you want to put it in as $data[0]; second column is $data[1] and so on.... Also, if your trimming everything you put through the saveToDB() function you should include the trim in that function; else your re-writing code for no reason. Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331159 Share on other sites More sharing options...
techker Posted March 26, 2012 Author Share Posted March 26, 2012 So QTY = '".trim(saveToDB($data[3]))."', Prices = '".trim(saveToDB($data[4]))."', Image = '".trim(saveToDB($data[5]))."', Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331161 Share on other sites More sharing options...
cpd Posted March 26, 2012 Share Posted March 26, 2012 Like I said it complete depends what column your QTY is and what column your Prices are. Unless you give us your spreadsheet we can't tell you but its fairly simple. QTY,Prices,Image maps to $data[0],$data[1],$data[2] Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331168 Share on other sites More sharing options...
techker Posted March 26, 2012 Author Share Posted March 26, 2012 i have to make it.. so on the csv: "product_title","short_description","full_description","QTY","Pirce","image.jpg" in phpmyadmin i seen i can import by csv?would it be faster and easier? Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331169 Share on other sites More sharing options...
cpd Posted March 26, 2012 Share Posted March 26, 2012 Short answer, yes. If you want to understand how this is working then no. RE your .csv file. "product_title"is $data[0] "short_description" is $data[1] "full_description" is $data[2] "QTY" is $data[3] "Pirce" is $data[4] "image.jpg" is $data[5] Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331185 Share on other sites More sharing options...
techker Posted March 26, 2012 Author Share Posted March 26, 2012 ok thx i will google the phpmyadmin import csv to see.. Quote Link to comment https://forums.phpfreaks.com/topic/259619-csv-insert/#findComment-1331187 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.