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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.