scuttzz Posted November 6, 2013 Share Posted November 6, 2013 Hi all, I have a little bit of a problem that I've been trying to solve for days now. Wonder if anyone could help.I have a table(named 'products') within this table the headers are as follows id, product_name,price, long_desc, short_desc, date_added, product_spec, directions, nut_info.I have a CSV file with colums ranging from 2 columns to 4, so a total of 3 tables.. Is there a way to import them to only the nut_info column in my Mysql table?Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/283643-import-a-csv-file-into-a-specific-column-within-a-table/ Share on other sites More sharing options...
cyber_alchemist Posted November 6, 2013 Share Posted November 6, 2013 Coincidences , i have to do the same thing and was figuring out how ... csv file format is i guess like this ... Year,Make,Model,Length 1997,Ford,E350,2.34 2000,Mercury,Cougar,2.38 where every value is separated by a comma , so i guess on could use explode(); to takeout values from it http://php.net/manual/en/function.explode.php it would help if one could get a format of the file and a example data inserted into it Quote Link to comment https://forums.phpfreaks.com/topic/283643-import-a-csv-file-into-a-specific-column-within-a-table/#findComment-1457159 Share on other sites More sharing options...
scuttzz Posted November 6, 2013 Author Share Posted November 6, 2013 The format is also separated by comma's, the first table has 3 headings (Typical nutritional values, per 100g, per 20g serving ), and the information within it is numbers and a few letters.. How ever for this specific row theres a total of 3 tables, but im sure that when i've figured out out to import the one the rest are simple. Thanks for the article ill give it a read... And to do with the format, its was originally a excel(.xls) file that I converted into a CSV through excel 2007. Does that answer all your questions? Quote Link to comment https://forums.phpfreaks.com/topic/283643-import-a-csv-file-into-a-specific-column-within-a-table/#findComment-1457160 Share on other sites More sharing options...
Barand Posted November 6, 2013 Share Posted November 6, 2013 use fgetcsv Quote Link to comment https://forums.phpfreaks.com/topic/283643-import-a-csv-file-into-a-specific-column-within-a-table/#findComment-1457163 Share on other sites More sharing options...
cyber_alchemist Posted November 6, 2013 Share Posted November 6, 2013 (edited) have you tried opening your saved .csv files on excel ??? if it open correctly then , their is a way around it using .csv format. --edited-- use fgetcsv thanks it helped better Edited November 6, 2013 by cyber_alchemist Quote Link to comment https://forums.phpfreaks.com/topic/283643-import-a-csv-file-into-a-specific-column-within-a-table/#findComment-1457164 Share on other sites More sharing options...
cyber_alchemist Posted November 6, 2013 Share Posted November 6, 2013 (edited) looked into the .csv files with comma , commas are escaped by " such as ," any conditional statement will solve it with explode isnt it ??? anyways here is the short script with the help of the example of php.net: excelupload.php <?php if (isset($_POST['submit'])) { $excel_name = $_FILES['excel']['name']; echo $excel_name . "<br />"; $excel_tmp_name = $_FILES['excel'][ 'tmp_name']; echo $excel_tmp_name . "<br />"; $excel_file_size = $_FILES['excel']['size']; echo $excel_file_size . "<br />"; $excel_file_type = $_FILES['excel']['type']; echo $excel_file_type . "<br />"; $excel_date = @date('y-m-d'); echo 'Date of file upload' . $excel_date; $row = 1; echo '<table border="1" >'; if (($handle = fopen($excel_tmp_name, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<tr> <td>$num</td><td>$row:</td>"; $row++; for ($c=0; $c < $num; $c++) { echo "<td>" . $data[$c] . "</td>\n"; } echo "</tr>"; } fclose($handle); } echo "</table>"; } ?> excel.html <html> <head><title>excel upload</title></head> <body> <form action="excelupload.php" method="post" enctype="multipart/form-data" > <input type="file" name="excel" id="excel" /> <input type="submit" name="submit" id="submit" value="submit" /> </form> </body> </html> Edited November 6, 2013 by cyber_alchemist Quote Link to comment https://forums.phpfreaks.com/topic/283643-import-a-csv-file-into-a-specific-column-within-a-table/#findComment-1457196 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.