El Heso Posted September 16, 2011 Share Posted September 16, 2011 Hi! I hope somebody can help me what im do wrong. i have checked that the data from the file is in $source_file but nothing imports to the database <?php include('config.php'); include('opendb.php'); if(isset($_POST['upload'])) { $source_file = @$_POST['userfile']; //$source_file = fopen('http://localhost/test/upload/test.csv', 'r'); $target_table = 'foretag'; function csv_file_to_mysql_table($source_file, $target_table, $max_line_length=10000) { if (($handle = fopen("$source_file", "r")) !== FALSE) { $columns = fgetcsv($handle, $max_line_length, ","); foreach ($columns as $column) { $column = str_replace(".","",$column); } $insert_query_prefix = "INSERT INTO $target_table (".join(",",$columns).")\nVALUES"; while (($data = fgetcsv($handle, $max_line_length, ";")) !== FALSE) { while (count($data)<count($columns)) array_push($data, NULL); $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; mysql_query($query); } fclose($handle); } } function quote_all_array($values) { foreach ($values as $key=>$value) if (is_array($value)) $values[$key] = quote_all_array($value); else $values[$key] = quote_all($value); return $values; } function quote_all($value) { if (is_null($value)) return "NULL"; $value = "'" . mysql_real_escape_string($value) . "'"; return $value; } } include('closedb.php'); echo "<br>done<br>"; ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 16, 2011 Share Posted September 16, 2011 Echo the query string along with any errors returned by MySQL if( !mysql_query($query) ) { echo "<br>Query: $query<br>Returned error: " . mysql_error() . "<br>"; } Quote Link to comment Share on other sites More sharing options...
requinix Posted September 16, 2011 Share Posted September 16, 2011 If the file is on the same server as MySQL, and your user has sufficient privileges, you can make MySQL load the file by itself. Quote Link to comment Share on other sites More sharing options...
El Heso Posted September 16, 2011 Author Share Posted September 16, 2011 answer Query: Returned error: Query was empty Quote Link to comment Share on other sites More sharing options...
El Heso Posted September 16, 2011 Author Share Posted September 16, 2011 The user dont have that permission,so i need a script that controls the import after posted a csv file If the file is on the same server as MySQL, and your user has sufficient privileges, you can make MySQL load the file by itself. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 16, 2011 Share Posted September 16, 2011 The query was nothing but an empty string, so there's the first problem . . . Quote Link to comment Share on other sites More sharing options...
El Heso Posted September 16, 2011 Author Share Posted September 16, 2011 but when i print out the $source_file the data exist, were can it be empty? The query was nothing but an empty string, so there's the first problem . . . Quote Link to comment Share on other sites More sharing options...
El Heso Posted September 16, 2011 Author Share Posted September 16, 2011 can this be the problem? $target_table = 'foretag'; i try to define what table in the database it should place the data The query was nothing but an empty string, so there's the first problem . . . Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 16, 2011 Share Posted September 16, 2011 Are you uploading the file to be inserted via a form? If so, an uploaded file isn't in the $_POST array, it's in the $_FILES array, and your <form> tag must have the enctype="multipart/fprm-data" attribute. Quote Link to comment Share on other sites More sharing options...
El Heso Posted September 16, 2011 Author Share Posted September 16, 2011 same problem quet is empty here is the hole code <form method="post" enctype="multipart/fprm-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> <?php include('config.php'); include('opendb.php'); $query=''; if(isset($_POST['upload'])) { $source_file = @$_FILES['userfile']; //$source_file = fopen('http://localhost/test/upload/test.csv', 'r'); $target_table = 'foretag'; function csv_file_to_mysql_table($source_file, $target_table, $max_line_length=10000) { if (($handle = fopen("$source_file", "r")) !== FALSE) { $columns = fgetcsv($handle, $max_line_length, ","); foreach ($columns as &$column) { $column = str_replace(".","",$column); } $insert_query_prefix = "INSERT INTO $target_table (".join(",",$columns).")\nVALUES"; while (($data = fgetcsv($handle, $max_line_length, ";")) !== FALSE) { while (count($data)<count($columns)) array_push($data, NULL); $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; mysql_query($query); } fclose($handle); } } function quote_all_array($values) { foreach ($values as $key=>$value) if (is_array($value)) $values[$key] = quote_all_array($value); else $values[$key] = quote_all($value); return $values; } function quote_all($value) { if (is_null($value)) return "NULL"; $value = "'" . mysql_real_escape_string($value) . "'"; return $value; } } if( !mysql_query($query) ) { echo "<br>Query: $query<br>Returned error: " . mysql_error() . "<br>";} include('closedb.php'); echo "<br>done<br>"; ?> Are you uploading the file to be inserted via a form? If so, an uploaded file isn't in the $_POST array, it's in the $_FILES array, and your <form> tag must have the enctype="multipart/fprm-data" attribute. Quote Link to comment Share on other sites More sharing options...
El Heso Posted September 17, 2011 Author Share Posted September 17, 2011 I fixed the problem by this code: $source_file = fopen('http://localhost/test/murt.csv', 'r'); $target_table = 'foretag'; if (($handle = $source_file) !== FALSE) { $columns = fgetcsv($handle, 10000, ";"); foreach ($columns as &$column) { $column = str_replace(".","",$column); } $insert_query_prefix = "INSERT INTO foretag (".join(",",$columns).")\nVALUES"; while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) { while (count($data)<count($columns)) array_push($data, NULL); $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; mysql_query($query); } fclose($handle); } function quote_all_array($values) { foreach ($values as $key=>$value) if (is_array($value)) $values[$key] = quote_all_array($value); else $values[$key] = quote_all($value); return $values; } function quote_all($value) { if (is_null($value)) return "NULL"; $value = "'" . mysql_real_escape_string($value) . "'"; return $value; } 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.