mjsnosk8 Posted March 16, 2011 Share Posted March 16, 2011 Hi all, Just curious why this works: while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){ $import="INSERT into $prodtblname ($csvheaders1) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')"; } And this does not: $headdata_1 = "'$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]'"; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){ $import="INSERT into $prodtblname ($csvheaders1) values($headdata_1)"; } it puts $data[#'s] in the database fields instead of the actual data that '$data[0]','$data[1]'... relates to. I wrote a script to create the values in $headdata_1 based on the number of headers in $csvheaders1 but can't seem to get it working in the sql statement. Thanks Quote Link to comment Share on other sites More sharing options...
mattal999 Posted March 16, 2011 Share Posted March 16, 2011 When you define $headata outside the while clause, $data has not yet been defined. This means that your $headata variable will be: '', '', '', '', '', '', '', '' Not quite what you were expecting. Quote Link to comment Share on other sites More sharing options...
mjsnosk8 Posted March 17, 2011 Author Share Posted March 17, 2011 Hey thanks for the fast reply. Got me looking in the right direction. My work around is: User saves csv formated headers in the "profiles" table from a form. then I: $fo_w = fopen($H1datafile, 'w'); fwrite($fo_w, ''); fclose($fo_w); $fo_a = fopen($H1datafile, 'a'); $count = substr_count($header_row_1, ',') ; $i=0; while($i<=$count) { $data = "\'".'$data[' . $i . ']'."\',"; $i++; fwrite($fo_a, $data); } fclose($fo_a); $read = file($H1datafile); foreach($read as $line){ $header_data_1 = substr($line, 0, -1); } And store the value clause string $header_data_1 into the db. Later in the page I: $H1dataphp = 'temps/H1data.php'; $fo_w = fopen($H1dataphp, 'w'); $line1= '<?php while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){'."\n"; fwrite($fo_w, $line1); $line2 = '$insert = "INSERT into r_source_feed_1 ('.$header_row_1.') values('.$header_data_1.')";'."\n"; fwrite($fo_w, $line2); $line3 = 'mysql_query($insert) or die(mysql_error()); } ?>'; fwrite($fo_w, $line3); fclose($fo_w); Then include H1data.php on the page I need it. Now you can enter any number of headers in the text field and insert just those columns from a linked text file. MJ :D Quote Link to comment 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.