Jump to content

Help needed to fix script for import of csv file to mysql


El Heso

Recommended Posts

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>";

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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; 
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.