How to insert excel file in hindi font to mysql using php

i show my code you.
(form.html)

<html>
<head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />

<body>
<form action="import_query.php" method="post" name="upload_excel" enctype="multipart/form-data">
<div>
 <label>Import CSV/Excel File:</label>
 <input type="file" multiple name="filename" id="filename">
 <button type="submit" id="submit" name="submit" data-loading-text="Loading...">Upload</button>
</div>
</form>
<body>
</head>
</html>

(import_query.php)

<?php
if (isset($_POST['submit'])) 
{
include('database.php');
 
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
 

 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
 
 echo $data[0]." > ";
  echo $data[1]." > ";
   echo $data[2]." > ";
   echo $data[2]." > ";
 mysql_query("INSERT into hindii (user_name, first_name, last_name, date_added)
 values('$data[0]', '$data[1]', '$data[2]', NOW())");
 
 }
 
fclose($handle);
header( 'Content-Type: text/html; charset=utf-8' );
//print "Import done";
echo "<script type='text/javascript'>alert('Successfully Imported a CSV File for User!');</script>";
echo "<script>document.location='index.php'</script>";
//view upload form
}
?>

(database.php)

<?php
mysql_select_db('hindi_data',mysql_connect('localhost','root','' ))or die(mysql_error());
?>๏ปฟ

Hi @nehadangivds8 and welcome to the forum.

I have just completed a similar project:

I used the following library to convert the XLS to a CVS file:

https://github.com/PHPOffice/PHPExcel

Then this PHP function to convert the file to an array:

http://php.net/manual/en/function.fgetcsv.php

Next I extracted each CVS line from the array and inserted the data into a dbase table.

More details can be found here:

https://www.sitepoint.com/community/t/how-to-improve-the-performance-of-this-query/282001

1 Like

this is not working dear โ€ฆ my out put is ??? ???

I think the output is due to the language not being recognized because the Meta Charset is incorrect. It should be UTF-8

A character in UTF8 can be from 1 to 4 bytes long. UTF-8 can represent any character in the Unicode standard. UTF-8 is backwards compatible with ASCII. UTF-8 is the preferred encoding for e-mail and web pages

It is also advisable to use MySQLi because MySQL has been dropped from the latest PHP versionsโ€ฆ

Try this:

<?php
# SET ERROR REPORTING
  declare(strict_types=1); // REMOVE IF NOT PHP7
  error_reporting(-1);
  ini_set('display_errors', '1');
  ini_set('display_html_errors', '1');
  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 
# Import uploaded file to Database
   $handle = fopen($_FILES['filename']['tmp_name'], "r");

# VALIDATE $_POST
	if( isset($_POST['submit']) ) 
	{ 
		// include('database.php');

		$counter = 0;
		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
		{
			fred($data);
			if( ++$counter > 3):
				exit;
			endif;	
		 	echo $data[0]." > ";
		  echo $data[1]." > ";
		  echo $data[2]." > ";
		  echo $data[2]." > ";

		  $sql = "
		  	INSERT INTO `hindii` 
		  	(
		  			`user_name`, 
		  			`first_name`, 
		  			`last_name`, 
		  			`date_added`
		  	)
		 		VALUES
		 		(
		 			'$data[0]', 
		 			'$data[1]', 
		 			'$data[2]', 
		 			 NOW()
		 		)
		  ";
		  fred( $sql );
		 	# $ok = mysql_query( $sql );

		}// while
		fclose($handle);

# OUTPUT RESULTS
	header( 'Content-Type: text/html; charset=utf-8' );
	print "Import done";
	echo "<script type='text/javascript'>alert('Successfully Imported a CSV File for User!');</script>";
	echo "<script>document.location='index.php'</script>";
	// view upload form
} // endif if (isset($_POST['submit'])) 


# DEBUGGING
//==============================
function fred($val)
{
	$style = 'width:88%; margin:1em auto; padding:0.42em; border:solid 1px red;';
	echo '<pre style="' .$style .'">';
		print_r($val);
	echo '</pre>';

}
1 Like

You are using dangerous obsolete mysql code that has been completetley removed from Php. You need to use PDO with Prepared Statements.https://phpdelusions.net/pdo

Never ever put variables in a query.
Do not output internal errors to the user. That info is only good to hackers.
Do not depend on a button to be submitted for your script to work. It will completely fail in certain circumstances. You need to check the REQUEST METHOD.

if ($_SERVER['REQUEST_METHOD'] == 'POST')

1 Like

OHK thankyou so much sir, for helping me,

1 Like

sorry sir but output is like this

Can you provide me full code, so that May understand the way the file will be imported

Try changing function fred($val) to the following which should show moe information.

# DEBUGGING
//==============================
function fred($val)
{
	$style = 'width:88%; margin:1em auto; padding:0.42em; border:solid 1px red;';
	echo '<pre style="' .$style .'">';
		var_dump ($val);
	echo '</pre>';

}

Did you change the incorrect Meta tag to UTF-8?

Can you supply a sample CVS file because it looks as though the data maybe corrupt.

Also the most likely cause is an incorrect meta character set. There are various methods to change the character set.

1 Like

nehavds.csv (210 Bytes)
this is demo excel file

This is the content of the CSV file:

??? ,???? ???,??? ,??? 
?????,?????,??????,??????????
?????,?????,??????,??????????
?????,?????,??????,??????????
?????,?????,??????,??????????
?????,?????,??????,??????????
?????,?????,??????,??????????
?????,?????,??????,??????????

So it looks as though you script is working correctly.

1 Like

nooo

YES!

Would someone else please download the file and check the contents?

1 Like

can you send me one csv demo hindi formate

I used Google Translate and copied this into the first line of the supplied CSV file:

Array
(
    [0] => เคฎเฅ‹เคนเคฎเฅเคฎเคฆ
    [1] =>  เคœเฅ‰เคจ
    [2] =>  เคฒเฅ‹เคนเคพเคฐ
    [3] =>  2017/11/26
)

  เคฎเฅ‹เคนเคฎเฅเคฎเคฆ > เคœเฅ‰เคจ > เคฒเฅ‹เคนเคพเคฐ > เคฒเฅ‹เคนเคพเคฐ >

INSERT INTO `hindii` 
(
  `user_name`, 
  `first_name`, 
  `last_name`, 
  `date_added`
)
VALUES
(
  'เคฎเฅ‹เคนเคฎเฅเคฎเคฆ', 
   เคœเฅ‰เคจ', 
  ' เคฒเฅ‹เคนเคพเคฐ', 
   NOW()
)
1 Like

Iโ€™m also just seeing question marks in that file.

2 Likes

plzz can u supply a your csv file, i think my extention is wrong

Ditto.

2 Likes

Here is the modified CSV file:

rahul.csv (303 Bytes)

2 Likes