Import excel error

Many times i try to import excel sheet to table in db(mysql or MSSQL …) but no way to solve this problem ,
after that i read all subject and videos in YouTube and try examples attached in YouTube .
the result was the same.
first example i tried, it gets me this error:Scriptcase - Como Importar arquivos Excel XLS
the second example , it gets error message htat : file type is invalid
can you help me please?

1 Like
2 Likes

I have done it with a .csv file.

{file} is the File Uploader Component.
This code is onValidate.

$file1={file};
if($file1=="")
{
sc_error_message(“You need to upload a file!”);
}
// Clean field message used for feedback
{message} = ‘’;
//set session var
date_default_timezone_set(“America/Vancouver”);
$user=[UserID];
$now = date(“Y-m-d g:i a”);
///
if( $file1 <>"")
{
// Starts array of File
$arr_file = array();
// Address Directory’s uploader of Application
$path = $this->Ini->path_doc;
// Absolute path of the file
$file = $path . ‘/’ . {file};
$handle = fopen($file, ‘r’);
//{message} .= $file;
if(is_file($file))
{
// auto detect line endings
ini_set(“auto_detect_line_endings”, true);
///
$arr_file = file($file);
while(!feof($handle))
{
// handle each line of the CSV
$arr_line = fgetcsv($handle);
//var_dump($arr_line);
// skip this line if header row
$skip_line = false;
// check to see if this is a header row

	if($arr_line[1] == "Code") 
	{
		$skip_line = true;	
	}
	if($arr_line[1] == "Display Text") 
	{
		$skip_line = true;	
	}
	////
	if(empty($arr_line[1]))
	{
		$skip_line = true;
	}
	
	if($skip_line == false) 
	{
		$sql = "INSERT INTO temp_accomms(acc_abbrev,acc_name,
		acc_master_subject_array,acc_reason_array,acc_grade_array,acc_schoolid,
		acc_type,insby,inson) VALUES ('";
	    $sql .= trim(addslashes($arr_line[0])) . "', '";//code
		$sql .= trim(addslashes($arr_line[1])) . "', '";//display
		$sql .= trim(addslashes($arr_line[2])) . "', '";//subjects
		$sql .= trim(addslashes($arr_line[3])) . "', '";//reasons
		$sql .= trim(addslashes($arr_line[4])) . "', '";//grades
	    $sql .= $school . "', '"; 
		$sql .= trim(addslashes($arr_line[5])) . "', '";//type
		$sql .= $user . "', '"; 
		$sql .= $now . "')";//type
	    sc_exec_sql($sql);
	//	echo"$sql<br>";
	}
 }
}
else

{
// not a file
{message} .= " That is not CSV file!";

}
}//end if Tid >0

else

{
echo “TRY AGAIN”;
}
///

2 Likes

Thank you very much, this is a great solution that might help me solve my problem

I am very glad you answered my question and will try this solution.

I tried this great solution and it works very well.
But what about the update in the case of duplicate the primary key, so that if it does not find a duplicate, the program will add a new record, and if there is a duplicate, it will be replaced by the modification / update with the same base number

1 Like

Use this:

INSERT INTO table (column_list)
VALUES (value_list)
ON DUPLICATE KEY UPDATE
c1 = v1,
c2 = v2,
…;

1 Like

Many thanks for response
But I face this error:


and this is the new code for updating if there is a duplicate unique value

Your variable top is $Enviar_email
Bottom is $Enviar_emai

Just a typing error on your part.

1 Like

Ohhh … My God
You Are right

It working very good, But without updating, Just add mew rows.

Thanks in advance

Can someone create a video in English, Please