I got warning when upload any csv file - php

I want to upload CSV file to mysql database but i got error can any one help me please i am new in php
Warning: fopen() [function.fopen]: Filename cannot be empty
Here Is code please help me
<?php
$connect = mysql_connect("localhost","root","");
mysql_select_db("csv",$connect);
?>
<?php
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h3>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." ."</h3>";
}
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$firstRow = true;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($firstRow) { $firstRow = false; }
else {
$import="INSERT into product_details(pname,price,total) values('$data[0]','$data[1]','$data[2]')";
mysql_query($import) or die(mysql_error());
}
}
fclose($handle);
print "Import done";
}else { ?>
<h3>Upload new csv by Browsing to file and Clicking on Upload</h3><br />
<form enctype="multipart/form-data" action='' method='post'>
<input type="file" name="filename" onchange="checkfile(this);" ><br /><br />
<input class="btn btn btn-primary" type="submit" name="submit" value="Upload">
</form>
In advance thanks

Related

How can i upload csv file to database in php and fix my errors?

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.

undefined offset 1 when trying to import csv file in database

i am trying import csv file in database but it showing undefined offset 1 and it storing values in db as
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿRoot Entryÿÿÿÿÿÿÿÿ
i don't know why.i was trying to fix that error from past 5 hours but i coudn't
here is my code
<body>
<form name="import" method="post" enctype="multipart/form-data">
<input type="file" name="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
include ("connection.php");
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
$filesop = array();
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = $filesop[0];
$email = $filesop[1];
$sql = mysql_query("INSERT INTO mdl_user (username, email) VALUES
('$name','$email')");
$c = $c + 1;
}
if($sql){
echo "You database has imported successfully. You have inserted
". $c ." recoreds";
}else{
echo "Sorry! There is some problem.";
}
}
?>
</body>
it showing undefined offset 1 error in this line
$email = $filesop[1];
can anyone help me
thanks in advance..

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 to Import Excel file into mysql Database using 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/

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