csv import problem

hi all

i got problem with importing from csv to db. i’ve downloaded some codes and tried but it doesnt work.

this is the error:
C:/xampp/htdocsfileUploaded/temp/Please import text file
Error Occured!Please try again

[php]
include(‘importer.class.php’);

if ( isset($_POST[‘value’])== ‘yes’ ) {
//set the temp dir
echo $path = $_SERVER[‘DOCUMENT_ROOT’].‘fileUploaded/temp/’;

$importer = new importer( $path , 'test_01' , 'general_tbl' );

$importer->connectDatabase( 'localhost', 'root' ,'');

$importer->setDelimiter('comma'); 

if( $importer->importFile() ) {
   echo 'File imported successfully'; 
} else {
    echo "Error Occured!Please try again";
}    

}
?>

<tr>
	<td>Import Files</td>
	<td><input type="file" name="import_file" id="import_file" /><input type="submit" value="submit" /></td>		
</tr>	
[/php]

and the class below is included:

[php]

<?php class importer { public function __construct( $uploadDir , $database ,$tableName ) { $this->fileName = $uploadDir . $_FILES['import_file']['name']; $this->dbName = $database; $this->tableName = $tableName; } public function connectDatabase( $server , $user, $password ) { mysql_connect( $server ,$user,$password) or die('Couldnot connect mysql'); if ( mysql_select_db( $this->dbName ) ) { return true; } else { return false; } } public function setDelimiter( $seperator ) { if( $seperator == "tab" ) $this->separator = "\t"; else if ( $seperator == "comma" ) { $this->separator = ","; } } public function uploadFile() { if ( $_FILES['import_file']['type'] != "text/plain") { echo "Please import text file
"; return false; } if(move_uploaded_file( $_FILES['import_file']['tmp_name'] , $this->fileName )) { return true; } else { return false; } } public function deleteFile() { if( file_exists($this->fileName) ) { if( unlink( $this->fileName ) ) { return true; } } else { return false; } } public function importFile() { //upload file if( $this->uploadFile() ) { $tableFields = $this->getTableFields( $this->tableName ); $this->lines = file( $this->fileName ); if( $this->lines ) { foreach($this->lines as $line ) { $fields = array(); $fields = explode("$this->separator", $line); $this->insertDataIntoTable( $tableFields , $fields ); $fields = ""; } if( file_exists($this->fileName) ) { if( $this->deleteFile() ) { return true; } } } } return false; } public function getTableFields( $table ) { $tableFields = array(); $result = mysql_query( "SELECT *FROM " .$table ); $numOfField = mysql_num_fields($result); for( $i = 0 ; $i<$numOfField ;$i++ ) { $tableFields[] = mysql_field_name($result, $i); } return $tableFields; } public function insertDataIntoTable( $fields , $fieldsValues ) { $sql = 'INSERT INTO ' . $this->dbName . '.'.$this->tableName .' ( ' ; $values = ""; foreach( $fields as $key => $field ) { $sql .= $field . ' , '; } foreach($fieldsValues as $key => $value) { if( $key == 0 ) { $values = " VALUES ( NULL ,'" .$value ."' ,"; } else { $values .= " '".$value. "' ,"; } } $query = substr( $sql ,0 , -2); $values = substr( $values ,0 , -1); $query = $query .') '. $values ." )"; mysql_query( $query ); } } //END of Class [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service