pagegen Posted September 18, 2009 Share Posted September 18, 2009 Am creating a dynamic insert statement, to insert data from a csv to a database, the user maps the fields of excel columns to database. The code below works okk but the problem is the fields are not maping properly! if i have more columns in my database then excel then it will miss out the last few columns of excel to add!! any suggestions plz this is what it does: INSERT INTO billytest (id, DateAdded,Surname,Title,Email) VALUES ('0', '2009-09-18', '24/12/1985', 'h') even though i selected 5 fields <?php $mySQL = "INSERT INTO billytest (id, DateAdded"; $mySQL2 = "VALUES ('0', '$dateOfPurchase'"; $row = 1; $handle = fopen("billys.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); if($row <=1) { $row++; for ($c=0; $c < $num; $c++) { /////////// Read Varuables ////////////// $lan = 1; $GenerateInsert; for($i=0; $i <= $number; $i++) { if($GLOBALS['$post'.$lan] == "") {} else if ($GLOBALS['$post'.$lan] == $data[$c]) { //echo $data[$c] ." "; $mySQL = $mySQL . "," . $GLOBALS['$tableRow'.$lan] .""; $mySQL2 = $mySQL2 . ",'" . $data[$c] ."'"; //echo $GLOBALS['$tableRow'.$lan] . "<br/>"; } $lan++; } ///////////////////////////////// //echo $head . " " . $data[$c] . "<br />\n"; } } else { $mySQL2 = "VALUES ('0', '$dateOfPurchase'"; echo "<br />"; $lan2 = 2; $GenerateInsert; for ($c=0; $c < $num; $c++) { //////////// Check for empty fields /////////// if($GLOBALS['$post'.$lan2] == "") { $num = $num - 1;} else { $mySQL2 = $mySQL2 . ", '" . $data[$c] ."'"; $num = count($data); } $lan2++; } $row++; //$result = mysql_query("$mySQL)" . " $mySQL2)"); echo("$mySQL)" . " $mySQL2)"); if (!$result) { $done = '<br/><br/>Invalid query: ' . mysql_error() . "\n"; } else { $done = "<br/><br/>done";} } } echo $done; fclose($handle); //echo $mySQL . ") " . $mySQL2 . ")"; ////////////// Insert ///////// //mysql_query($mySQL . ")"); ?> Link to comment https://forums.phpfreaks.com/topic/174709-dynamic-insert-statment/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.