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>
Related
I'm trying to upload a csv file into the database,
but only first line gets inserted and it's still insert a blank space for the first field and shift all the other record.
PHP code:
<?php
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$e1 = $filesop[0];
$e2 = $filesop[1];
$e3 = $filesop[2];
$e4 = $filesop[3];
$sql = "INSERT INTO eyfstb(e1,e2,e3,e4) values ('$e1','$e2','$e3','$e4')";
$stmt = mysqli_prepare($db,$sql);
mysqli_stmt_execute($stmt);
$c = $c + 1;
}
if($sql){
echo "sucess";
}
else
{
echo "Sorry! Unable to import the data.";
}
}
?>
File upload Form:
<!DOCTYPE html>
<html>
<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="submit" value="submit">Upload</button>
</form>
</body>
</html>
But it's only first line importing, but i need all data to import.
I am uploading the csv file in php mysql with the following code:
en<?php
session_start();
if(!isset($_SESSION["UID"]))
{
header("Location: index.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= 'crm_6feb'; // Database Name.
$conn=mysqli_connect($host,$db_user,$db_password) or die("unable to connect to database");
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
//$sql_data = "SELECT * FROM prod_list_1 ";
$count = 0;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData);
//exit();
$count++;
if($count>1){
echo $sql = "insert into enquiry(en_name,en_mobile,en_landline,en_email,en_address,salesid,date_recieved,duplicate,date_add,ip) values ('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]',$emapData[5],$emapData[6],'$emapData[7]',$emapData[8],'$emapData[9]')";
$RES = mysqli_query($conn, $sql) or die("error in insert");
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: importleads.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}?>
<!DOCTYPE html>
<html>
<head>
<title>Add Order Status</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?php include("includes/header.php");?>
<div class="box">
<h2>Add Order Status</h2>
<div class="display">
<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>
</div></div></body></html>
Now, after I click upload button, I get the following message as an error :
C:\wamp\tmp\phpBEC.tmp error in insert
The excel contents is as follows:
Name| Mobile| Alternate Number|Email| Address| Sales id| Date Rec| Dupli|Date Add| Ip
Kindly suggest where i am doing things wrong.
Awaiting revert for your help.
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.
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/
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';
}
}
?>