atravotum Posted March 11, 2008 Share Posted March 11, 2008 Ok so here is the run down. I am trying to make a script for one of my bosses at work. The point is to upload a csv file, convert it to separate fields and store it to a database. Now the upload works fine, the conversion goes smoothly. But when i try to upload it to a database the fields just are not being created. This involves 2 scripts for truncating purposes I will post what is needed. Please any help is appreciated. Note: Im convinced the problem is converting it to a mysql friendly format, as if i pend varchar(blah) to the end of anything it post some of the fields but not all. Script pulling the csv file, converting it, then punches to a relay file. <?php REQUIRE 'relay.php'; global $strings, $cmd, $db_name, $db_user, $db_pword, $db_table, $file_temp; $db_name=$_POST["name"]; $db_user=$_POST["user"]; $db_pword=$_POST["pword"]; $file_temp="supdashboard.csv"; $db_table = "test"; $col=array("bob", "frank"); //f_mod_table($db_name, $db_user, $db_pword, "csv", "ADD", array("bob varchar(15)"));//$db_name, $db_user, $db_pword, $db_table, $cmd, $col); //f_store($db_name, $db_user, $db_pword, "csv", array("bob"), array("uhhh")); //Output a line of the file until the end is reached $file=fopen($file_temp,"r")or exit("Unable to open file!"); $i=0; while(!feof($file)) { $strings[$i]=fgets($file); str_handle($strings[$i], $i); $i++; } fclose($file); for($i=0;$i<count($col);$i++) { //$col[$i] .=" varbinary(".strlen($col[$i]).")"; /$bob=$col[$i]; /$bob.=" char(15)"; /f_mod_table($db_name, $db_user, $db_pword, $db_table, "ADD", array($bob)); } //f_delete_table($db_name, $db_user, $db_pword, $db_table); f_create_table($db_name, $db_user, $db_pword, $db_table); f_mod_table($db_name, $db_user, $db_pword, $db_table, "ADD", $col); //f_store($db_name, $db_user, $db_pword, $db_table, $col, $data); //Process the strings to convert into data to be uploaded to the database. function str_handle($tmp_str, $i) { global $col, $data; if(($i == 0) && (strlen($tmp_str)!= 0 )) { $i=0; $x1=0; $x2=0; while(strpos($tmp_str,",") != false) { $com=strpos($tmp_str, ','); $par=strpos($tmp_str, '"'); if(($par < $com) && substr($tmp_str, 0, 1) == '"') { $tmp_str = substr($tmp_str,($par+1)); $par=strpos($tmp_str, '"'); $x2=$par; $col[$i]=substr($tmp_str,$x1, $x2); $tmp_str = substr($tmp_str,($par+1)); } else { $x2=$com; $col[$i]=substr($tmp_str,$x1, $x2); $tmp_str = substr($tmp_str,($com+1)); } $i++; } } elseif(($i != 0) && (strlen($tmp_str)!= 0 )) { $i=0; $x1=0; $x2=0; while(strpos($tmp_str,",") != false) { $com=strpos($tmp_str, ','); $par=strpos($tmp_str, '"'); if(($par < $com) && substr($tmp_str, 0, 1) == '"') { $tmp_str = substr($tmp_str,($par+1)); $par=strpos($tmp_str, '"'); $x2=$par; $data[$i]=substr($tmp_str,$x1, $x2); $tmp_str = substr($tmp_str,($par+1)); } else { $x2=$com; $data[$i]=substr($tmp_str,$x1, $x2); $tmp_str = substr($tmp_str,($com+1)); } $i++; } } } ?> This is the relay script executing the push (i coded it as well but it has tested successfully on many trials when testing with static data) This is just the function in question also. //Add or delete fields from an existing table function f_mod_table($db_name, $db_user, $db_pword, $db_table, $cmd, $col) { // Connect To Database $con = mysql_connect($db_name, $db_user, $db_pword); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db_user, $con); // Counts the size of $col $num = count($col); // Checks to make sure $cmd is valid if($cmd != "ADD" AND $cmd != "DROP") { echo "Invalid paramater passed into $cmd. Needs to be either \"ADD\" or \"DROP\"."; } //If command is valid perfomrs either add or drop function per number of columns else if($cmd == "ADD") { for($i=0;$i<$num;$i++) { $sql="ALTER TABLE $db_table ADD COLUMN $col[$i]"; mysql_query($sql,$con); echo $col[$i]. " Stored! <br/> "; } } else if($cmd == "DROP") { for($i=0;$i<=$num;$i++) { $sql="ALTER TABLE $db_table DROP COLUMN $col[$i]"; mysql_query($sql,$con); } } // Disconnect from database. mysql_close($con); } Again any help would be appreciated its a freelance thing for my boss so hes not going to kill me anytime soon, but i would still like to put it behind me. Quote Link to comment https://forums.phpfreaks.com/topic/95531-solved-need-help-slightly-complicated-im-stuck-beyond-reason/ Share on other sites More sharing options...
atravotum Posted March 11, 2008 Author Share Posted March 11, 2008 sry i typoed *fix below* the 3 lines WERE commented out properly for($i=0;$i<count($col);$i++) { //$col[$i] .=" varbinary(".strlen($col[$i]).")"; //$bob=$col[$i]; //$bob.=" char(15)"; //f_mod_table($db_name, $db_user, $db_pword, $db_table, "ADD", array($bob)); } Quote Link to comment https://forums.phpfreaks.com/topic/95531-solved-need-help-slightly-complicated-im-stuck-beyond-reason/#findComment-489034 Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 without some debug data its hard to say what he problem is but on this line f_mod_table($db_name, $db_user, $db_pword, $db_table, "ADD", $col); shouldn't $col be a array ? ie f_mod_table($db_name, $db_user, $db_pword, $db_table, "ADD", array($col)); Quote Link to comment https://forums.phpfreaks.com/topic/95531-solved-need-help-slightly-complicated-im-stuck-beyond-reason/#findComment-489037 Share on other sites More sharing options...
haku Posted March 11, 2008 Share Posted March 11, 2008 Please use code tags when posting code. Its the # button above the box you type your text in. Quote Link to comment https://forums.phpfreaks.com/topic/95531-solved-need-help-slightly-complicated-im-stuck-beyond-reason/#findComment-489038 Share on other sites More sharing options...
atravotum Posted March 11, 2008 Author Share Posted March 11, 2008 Sry about the code thing I was not aware. I don't post on forums often. Answering the mad, I have tried declaring it as an array and if it was static data normally you would, but $col is already an array so it double declares it and produces $col to = array when it gets into the relay script. Quote Link to comment https://forums.phpfreaks.com/topic/95531-solved-need-help-slightly-complicated-im-stuck-beyond-reason/#findComment-489044 Share on other sites More sharing options...
atravotum Posted March 11, 2008 Author Share Posted March 11, 2008 Ok nvm I managed to get a work around. The basic problem I think is simply MYSQL does not like odd field names. I just created the first data row as the field names works like a charm. Thnx to everyone that tried to help. Quote Link to comment https://forums.phpfreaks.com/topic/95531-solved-need-help-slightly-complicated-im-stuck-beyond-reason/#findComment-490050 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.