How to Import Excel file into mysql Database using PHP - php

this is my HTML code for the upload form
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
The PHP code for the connection to the database and the import is below.
<?
mysql_connect("localhost","root","") or die("Unable To Connect");
mysql_select_db("tutorial") or die("Database Not Found");
?>
<?php
if(isset($_POST["Import"]))
{
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
//$sql_data = "SELECT * FROM prod_list_1 ";
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData);
//exit();
$count++;
$sql = "INSERT into customers values ('','$emapData[1]','$emapData[1]','$emapData[2]')";
mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: test.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
This is what I tried but after I upload , it imports unreadable alphabets into my database table columns

use PHPExcel to import excel or CSV in database https://phpexcel.codeplex.com/

Related

exporting ang retrieving data from excel to database using php

I am trying to create a feature in my program where in you could upload data to the database by uploading data from excel (uploading excel file) i have provided the code below but the data in the database is encoded(i provided a screenshot).
is there something wrong with the code?
Excel format:
firstname lastname
_____________________
amir kumar
jhon doee
But this is what the data looks like when inserted into the database:
This is my index.php:
<form enctype="multipart/form-data" method="post" action="import.php" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
import.php
<?php
if(isset($_POST["Import"]))
{
//First we need to make a connection with the database
$host='localhost'; // Host Name.
$db_user= 'root'; //User Name
$db_password= '';
$db= 'testdatabase'; // Database Name.
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
$count = 0;
$sql_data = "SELECT * FROM person";
echo $sql_data;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData);
//exit();
$count++;
if($count > 1) {
$sql = "INSERT into person(firstname , lastname) values ('$emapData[0]','$emapData[1]')";
mysql_query($sql);
echo "success";
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
Don't use the Excel file as is. You see what gibberish you get.
Instead, do one of these:
"Export" the data from Excel into a .csv file, then LOAD DATA into MySQL.
Use a PHP API that lets you fetch data from Excel. Then INSERT the data into a MySQL table.

uploading excel/CSV file data into database

I have to write a code were i need to import the email ids of people with their names which will be on a excel sheet into the database, but the issue which am facing is, it is inserting blank data into the database,please help, am new to this concept,pardon me if i went wrong somewhere.
DB Structure
import.php
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="200" required="">
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
<?php
if(isset($_POST["Import"]))
{
$con = mysqli_connect("localhost","***","***","***");
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
$count = 0;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$count++;
if($count>1){
$query = "INSERT INTO import_email (via, vault_no, name, email, created_at) VALUES ('".$_SESSION['via']."', '".$_SESSION['vault_no']."', '$name[0]', '$email[1]', NOW())";
mysqli_query($con, $query);
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
//header('Location: profile_1.php');
}else{
echo 'Invalid File:Please Upload CSV File';
}
}
?>
EXCEL Structure(.csv format)
Thank you.

How can I import Excel file in mysql?

I have a code in which I can import .CSV file in my mysql. But I can not import Excel file. Some garbage values insert in my database while I import Excel file.
<?php
if(isset($_POST["Import"]))
{
//First we need to make a connection with the database
include("connection.php");
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
//$sql_data = "SELECT * FROM prod_list_1 ";
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData);
//exit();
$sql = "INSERT into city(city_id,city_name) values ('$emapData[0]','$emapData[1]')";
mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
Looks like you are trying to read an excel file using some code that is able to parse only csv files, you can't, the format is completely different.
Check for filetype and use a library like PHPExcel if the user sends you an excel spreadsheet.
You can use EasyXLS Excel library to import Excel file and than to insert data into MySQL database:
$workbook = new COM("EasyXLS.ExcelDocument");
$rows = $workbook->easy_ReadXLSActiveSheet_AsList($filename);
For more details see this link about importing Excel file to MySQL using EasyXLS.

how to save xls file as table in mysql using php

I want to save my excel file as table in database using PHP. Is it possible?
I tried like below. But we need to give table name.
<html>
<head>
<?php
if(isset($_POST["Import"]))
{
include 'database.php';
$filename=$_FILES["file"]["tmp_name"];
$heading=true;
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000)) !== FALSE)
{
$num = count($emapData);
echo $num;
echo $emapData[0];
//check if the heading row
if($heading) {
// unset the heading flag
$heading = false;
// skip the loop
continue;
}
$sql = "INSERT into sheet(name,first_name,last_name) values('$emapData[0]','$emapData[1]','$emapData[2]')";
$row=mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
</head>
<body>
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">
Only Excel/CSV File Import.
</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
</body>
</html>

cant upload data from excel into database using php

I cant upload data from excel into database using php and mysql.My code is given below.
Using these code added some unwanted data. Anyone can give me solution for these issue?
HTML:
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
PHP
<?php
if(isset($_POST["Import"]))
{
$host='localhost'; // Host Name.
$db_user= 'root'; //User Name
$db_password= '';
$db= 'excel'; // Database Name.
$conn=#mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
$count = 0;
//$sql_data = "SELECT * FROM prod_list_1 ";
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
/*print_r($emapData);
exit();*/
$count++;
if($count>1)
{
echo $in="insert into first (name, email) values('$emapData[0]','$emapData[1]')";
$insertTable= mysql_query($in);
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
}
else
{
echo 'Invalid File:Please Upload CSV File';
}
}
?>

Categories