Reading csv to insert in mysql. fill empty columns with previous line - php

I have a csv file. I'm trying to read this file and import data to mysql database.
For example
OrderID,ProductId,ProductDescription
100001962,15,Product1
"",32,Product2
"",31,Product3
100001546,24,Product4
How can i have my database like
100001962,15,Product1
100001962,32,Product2
100001962,31,Product3
100001546,24,Product4
new.php
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="csv" value="" />
<input type="submit" name="submit" value="Save" />
</form>
</body>
</html>
upload.php
header('Content-Type: text/html; charset=UTF-8');
require_once 'db.php';
if($_FILES['csv']['error'] == 0){
$name = $_FILES['csv']['name'];
$ext = strtolower(end(explode('.', $_FILES['csv']['name'])));
$type = $_FILES['csv']['type'];
$tmpName = $_FILES['csv']['tmp_name'];
// check the file is a csv
if($ext === 'csv'){
if(($handle = fopen($tmpName, 'r')) !== FALSE) {
// necessary if a large csv file
set_time_limit(0);
mysqli_set_charset($connection,"utf8");
$success = 0;
$nosuccess = 0;
while(( $data = fgetcsv($handle, 1000, ',')) !== FALSE) {
// number of fields in the csv
$csv[$row]['col1'] = str_replace("'", " ", $data[0]);//orderid
$csv[$row]['col2'] = str_replace("'", " ", $data[1]);//productid
$csv[$row]['col3'] = str_replace("'", " ", $data[2]);//productdesc
if ($csv[$row]['col1']==0){
if ($csv[$row-1]['col1']!=0){
$sql = "INSERT INTO csv (orderid, orderdate, orderstatus) VALUES ('". $csv[$row-1]['col1']."','". $csv[$row-1]['col2']."','". $csv[$row-1]['col3']."')";
}
if ($csv[$row-2]['col1']!=0){
$sql = "INSERT INTO csv (orderid, orderdate, orderstatus) VALUES ('". $csv[$row-2]['col1']."','". $csv[$row-2]['col2']."','". $csv[$row-2]['col3']."')";
}
//etc
}
else
{
$sql = "INSERT INTO csv (orderid, orderdate, orderstatus) VALUES ('". $csv[$row]['col1']."','". $csv[$row]['col2']."','". $csv[$row]['col3']."')";
}
mysqli_query($connection, $sql);
}
}
}
but doesn't work

Related

UTF8 in php mysql

I am trying to import data from xls to mysql via php. I am facing issue in save UTF-8 text via it. I am getting it saved as ???????. My database table structure is utf8_general_ci as well my php code is like below
<?php
$con = mysqli_connect('localhost', 'myuser', 'mypass', 'mydb');
if(isset($_POST["submit"]))
{
mysqli_query($con,'SET character_set_results=utf8');
mysqli_query($con,'SET names=utf8');
mysqli_query($con,'SET character_set_client=utf8');
mysqli_query($con,'SET character_set_connection=utf8');
mysqli_query($con,'SET character_set_results=utf8');
mysqli_query($con,'SET collation_connection=utf8_general_ci');
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$i = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$option1 = $filesop[0];
$option2 = $filesop[1];
$option3 = $filesop[2];
$option4 = $filesop[3];
$correctans = $filesop[4];
$question_text = $filesop[5];
$cat_id = $filesop[6];
$sub_cat_id = $filesop[7];
$level_id = $filesop[8];
$quesimage = $filesop[9];
$sql = mysqli_query($con,"INSERT IGNORE INTO questions (option1, option2,option3,option4,correctans,question_text,cat_id,sub_cat_id,level_id,quesimage) VALUES ('".$option1."','".$option2."','".$option3."','".$option4."','".$correctans."','".$question_text."','".$cat_id."','".$sub_cat_id."','".$level_id."','".$quesimage."')");
$i = $i + 1;
}
//echo $sql;
if($sql)
{
echo "You database has imported successfully. You have inserted ". $i ." records";
}
else
{
echo "Sorry!";
}
}
?>
<html>
<head>
<title>Import Questions</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
Upload Excel File : <input type="file" name="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
its working fine with English Text but getting issue in Hindi or Gujarati Text. How can I solve it ?
Thanks
Note that when using fgetcsv() function for reading data the locale setting is taken into account. If LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function.
You can try another thing - to convert your .csv document on-the-fly (change the UCS-2 with the your file encoding):
function parse_csv($filename) {
if (($handle != fopen($filename, "r"))) return false;
while (($cols = fgetcsv($handle, 1000, "\t")) !== FALSE) {
foreach( $cols as $key => $value ) {
$cols[$key] = trim( $cols[$key] );
$cols[$key] = iconv('UCS-2', 'UTF-8', $cols[$key]."\0") ;
$cols[$key] = str_replace('""', '"', $cols[$key]);
$cols[$key] = preg_replace("/^\"(.*)\"$/sim", "$1", $cols[$key]);
}
echo var_dump($cols); //This will display an array of your data
}
}
Using the same idea of the previous post, with a little modification:
function global_client_charset($charset){
if(!isset($charset)){
$user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
if(strrpos($user_agent,"linux")){
$GLOBALS["CHARSET"] = "UTF-8";
}else if(strrpos($user_agent,"windows")){
$GLOBALS["CHARSET"] = "ISO-8859-1";
}
}else{
$GLOBALS["CHARSET"] = $charset;
}
}
function toUTF8($data){
if($GLOBALS["CHARSET"] === "ISO-8859-1"){
return iconv("ISO-8859-1", "UTF-8", trim($data));
}else if($GLOBALS["CHARSET"] === "UTF-8"){
return trim($data);
}else{
return trim($data);
}
}
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$i = 0;
//do you know the charset you are receiving ??
//global_client_charset("ISO-8859-1");
global_client_charset();
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$option1 = $filesop[0];
$option2 = $filesop[1];
$option3 = $filesop[2];
$option4 = $filesop[3];
$correctans = $filesop[4];
$question_text = $filesop[5];
$cat_id = $filesop[6];
$sub_cat_id = $filesop[7];
$level_id = $filesop[8];
$quesimage = $filesop[9];
$query = "INSERT IGNORE INTO questions (option1, option2,option3,option4,correctans,question_text,cat_id,sub_cat_id,level_id,quesimage) VALUES ('".
$option1."','".$option2."','".$option3."','".$option4."','".$correctans."','".$question_text."','".
$cat_id."','".$sub_cat_id."','".$level_id."','".$quesimage."')";
//echo toUTF8($query); die();
$sql = mysqli_query($con,toUTF8($query));
$i = $i + 1;
}
if($sql)
{
echo "You database has imported successfully. You have inserted ". $i ." records";
}
else
{
echo "Sorry!";
}
}
?>
<html>
<head>
<title>Import Questions</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
Upload Excel File : <input type="file" name="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

store gujarati data from csv to MySQL database using php

Following code which is used to browse csv file.
Fatch data from csv and store in a MySQL database.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<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']['name'];
$handle = fopen($file, "r");
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = $filesop[0];
$marks = $filesop[1];
mysql_query("set name utf8");
$query = "INSERT INTO temp (name, marks) VALUES ('".$name."','".$marks."')";
echo $query;
$sql = mysql_query($query);
}
}
?>
</div>
</body>
</html>
Following code is used connect database.
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "test";
$conn = mysql_connect("$hostname","$username","$password") or die(mysql_error());
mysql_select_db("$database", $conn) or die(mysql_error());
?>
When I run code its stored data like "????????".
My CSV file like below
Following phpmyadmin version information
Following is my table structure
if you're getting stored ???????? in database, then,
You need to check the database collation first, change it to utf8_general_ci or utf8mb4_general_ci if simple utf_general_ci doesn't work.
Second, if collation is fine then it could also be possible that string is already in utf8 format, and you're converting it forcefully again.
Use following function to convert string to utf8.
function convToUtf8($str){
if( mb_detect_encoding($str,"UTF-8, ISO-8859-1, GBK")!="UTF-8" )
return iconv("gbk","utf-8",$str);
else
return $str;
}
Then in your code
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = convToUtf8($filesop[0]);
$marks = convToUtf8($filesop[1]);
//mysql_query("set name utf8"); No need to do this now
$query = "INSERT INTO temp (name, marks) VALUES ('$name','$marks')";
echo $query;
$sql = mysql_query($query);
}
and look here and set db collation

Unable to submit csv file data into corresponding database fields using PHP

Currently I am trying to upload a CSV file and enter each record into the database 1 by 1. The columns on the CSV have the same name as the field names in the database but sometimes the data will be in a different order within the CSV. When I say in a different order, I mean that instead of a list of names always being in the 1st column, they might be in the 3rd column.
Really what I'm asking is how will I do the above as I'm really stuck.
At the moment I doesn't insert into the database but it does get the array from the CSV file.
Code Below:
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>CSV Import</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="csv"/>
<input type="submit" name="submit" value="Save" />
</form>
</body>
</html>
config.php
<?php
/* Database Connection */
$con = mysql_connect('xxxxxxxx', 'xxxxxxxx', 'xxxxxxxx');
if(! $con )
{
die('Could not connect: ' . mysql_error());
}
$select_db = mysql_select_db('xxxxxxxx');
?>
upload.php
<?php
include('config.php');
$file = "test.csv";
$separator = ",";
$length = filesize($file);
$handle = fopen($file, "r");
$csvData = fgetcsv($handle, $length, $separator);
fclose($handle);
$i = 0;
while($i >= 1){
$title = $csvData[0];
$firstName = $csvData[1];
$secondName = $csvData[2];
$emailAddress = $csvData[3];
$houseNumber = $csvData[4];
$mobileNumber = $csvData[5];
$address1 = $csvData[6];
$address2 = $csvData[7];
$address3 = $csvData[8];
$address4 = $csvData[9];
$postcode = $csvData[10];
mysql_query("INSERT csv SET title='$title', firstName='$firstName' ,secondName='$secondName', emailAddress='$emailAddress', houseNumber='$houseNumber' ,mobileNumber='$mobileNumber', address1='$address1', address2='$address2', address3='$address3' ,address4='$address4', postcode='$postcode'")
$i++;
}
?>
The code you're using to insert the records will fail here:
$i = 0;
while($i >= 1){ // $i = 0. This test will fail.
// do stuff
}
The correct upload.php, assuming that there is a header line which contains the column names:
<?php
include('config.php');
$file = "test.csv";
$separator = ",";
$length = 0; // size of the longest line(!), 0 = no limit
$fields = array('title', 'firstName', 'secondName', 'emailAddress', 'houseNumber', 'mobileNumber', 'address1', 'address2', 'address3', 'address4', 'postcode'); // use it as a white list
$handle = fopen($file, "r");
// get 1st line (header) and flip keys and values
// format like [title] --> 0, [firstName] --> 1, ...
$header = array_flip(fgetcsv($handle, $length, $separator));
$values = array();
// while we can read lines as csvData:
while(($csvData = fgetcsv($handle, $length, $separator)) !== false){
foreach ($fields as $field) // put all values in an array in correct order
$values[] = $csvData[$header[$field]];
mysql_query("INSERT INTO csv (" . implode(',', array_keys($header)) . ") VALUES (" . implode(',', $values) . ")"); // only for demonstration - be careful with spaces and quotes in values - better switch to PDO!
}
fclose($handle);
Hope that helps! Untested, but might be working :).
Note: this will fail when someone omits values and you have declared them in your database as NOT NULL.
Be aware that this sample does not do any counter-measures to SQL injections. Please forget about mysql_query and learn to use PDO.

How do I insert data from a CSV file into my database?

I'm trying to upload my txt file into my database but I don't think anything happens. I checked my database in phpmyadmin but nothing was inserted. How do I load and insert my data into mysql database?
Here's my code:
<?php
$conn = mysql_connect("localhost", "login", "password") or die(mysql_error());
mysql_select_db("database", $conn);
if(!isset($_POST['submit']))
{
$uploadtxt = "nyccrash.txt";
$handle= fopen($uploadtxt, "r");
// error checking.
if($handle === false) {
die("Error opening $uploadtxt");
}
while($fileop = fgetcsv($handle, 1000, ",") !== false) {
$crash_year = $fileop[0];
$accident_type = $fileop[1];
$collision_type = $fileop[2];
$weather_condition = $fileop[3];
$light_condition = $fileop[4];
$x_coordinate = $fileop[5];
$y_coordinate = $fileop[6];
$sql = mysql_query("INSERT INTO nyccrash (crash_year, accident_type, collision_type, weather_condition, light_condition, x_coordinate, y_coordinate) VALUES ($crash_year, $accident_type, $collision_type, $weather_condition, $light_condition, $x_coordinate, $y_coordinate)");
} }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> NYC Crash Data </title>
<link ref="stylesheet" type "text/css" href="../style/style.css" />
</head>
<body>
<div id="mainWrapper">
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="file"/>
<br/>
<input type="submit" name="submit" value="submit"/>
</form>
</div>
If this is text data then you forgot ' around data
$sql = mysql_query("INSERT INTO nyccrash (crash_year, accident_type,
collision_type, weather_condition, light_condition, x_coordinate, y_coordinate)
VALUES ('$crash_year', '$accident_type', '$collision_type',
'$weather_condition', '$light_condition', '$x_coordinate', '$y_coordinate')");
Here's how to parameterise SQL statements with untrusted input.
$stmt = $db->prepare("INSERT INTO nyccrash (crash_year, accident_type,
collision_type, weather_condition, light_condition, x_coordinate, y_coordinate)
VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param('sssssss', $crash_year, ...);
$stmt->execute();
See http://codular.com/php-mysqli for more info on this.
If you don't understand why you should be doing it this way, look up SQL injection, and don't write another line of code until you do understand it.
You can do it with the library of this answer
$csv = New CSVReader();
$result = $csv->parse_file('Test.csv');//path to file should be csv
echo '<pre>'; //
print_R($result); // Only for testing
if($result){
foreach($result as $row){
$crash_year = $row['crash_year'];
$accident_type = $row['accident_type'];
$collision_type = $row['collision_type'];
$weather_condition = $row['weather_condition'];
$light_condition = $row['light_condition'];
$x_coordinate = $row['x_coordinate'];
$y_coordinate = $row['y_coordinate'];
$query = "INSERT INTO nyccrash";
$query .= "(crash_year, accident_type, collision_type, weather_condition, light_condition, x_coordinate, y_coordinate)";
$query .= " VALUES ";
$query .= "('$crash_year','$accident_type','$collision_type', '$weather_condition', '$light_condition', '$x_coordinate', '$y_coordinate')";
mysqli_query($query);
unset($query);
}
}
One thing i noticed that in the insert query you must have some varchar fields in your database table and for that you are missing commas. Wrap the varchar fields with commas. This might be the problem and use die and mysql_error to see what the error really is.

Creating a CSV Upload to Mysql

I have a table in MYSQL where I wish to have the option that my customer can upload a CSV file. By default I want this to add information which isn't there and automatically update it if there is information there.
These are the columns I have:
id customer_name customer_name_letterhead customer_notes systype status signaltype verification address postcode telephone mobile mobiletwo email mainarea installation Contract expiration SPA nservice maintenance monitoring MS certdate
I already know that I need to have an excel document with all of these in the headers of the rows. E.g A1-V1 has those headers.
edit:
I have made this:
<?PHP if(isset($_POST['SUBMIT']))
{
$fname = $_FILES['sel_file']['name'];
$chk_ext = explode(".",$fname);
if(strtolower($chk_ext[1]) == "csv")
{
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql = "INSERT into Customers(id,customer_name,customer_name_letterhead,customer_notes,systype,status,signaltype,verification,address,postcode,telephone,mobile,mobiletwo,email,mainarea installation,Contract,expiration,SPA,nservice,maintenance,monitoring,MS,certdate) values('$data[0]','$data[1]','$data[2]''$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]','$data[14]','$data[15]','$data[16]','$data[17]','$data[18]','$data[19]','$data[20]','$data[21]','$data[22]','$data[23]','$data[24]')";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
echo "Successfully Imported";
}
else
{
echo "Invalid File";
}
}?>
<form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post'>
Import File : <input type="file" name="sel_file" id="sel_file">
<input type='submit' name='submit' value='submit'>
</form>
Upon clicking submit, nothing happens.
Here is a picture of my csv file:
I ended up searching for a video on YouTube which really did help. It ended up giving me this:
<?PHP
if(isset($_POST['submit']))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file,"r");
while(($fileop = fgetcsv($handle,1000, ",")) !== false)
{
$customer_name = $fileop[0];
$customer_name_letterhead = $fileop[1];
$customer_notes = $fileop[2];
$systype = $fileop[3];
$status = $fileop[4];
$signaltype = $fileop[5];
$verification = $fileop[6];
$address = $fileop[7];
$postcode = $fileop[8];
$telephone = $fileop[9];
$mobile = $fileop[10];
$mobiletwo = $fileop[11];
$email = $fileop[12];
$mainarea = $fileop[13];
$installation = $fileop[14];
$Contract = $fileop[15];
$expiration = $fileop[16];
$SPA = $fileop[17];
$nservice = $fileop[18];
$maintenance = $fileop[19];
$monitoring = $fileop[20];
$MS = $fileop[21];
$certdate = $fileop[22];
$sql = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO Customers (
customer_name,
customer_name_letterhead,
customer_notes,
systype,
status,
signaltype,
verification,
address,
postcode,
telephone,
mobile,
mobiletwo,
email,
mainarea,
installation,
Contract,
expiration,
SPA,
nservice,
maintenance,
monitoring,
MS,
certdate
) VALUES (
'$customer_name',
'$customer_name_letterhead',
'$customer_notes',
'$systype',
'$status',
'$signaltype',
'$verification',
'$address',
'$postcode',
'$telephone',
'$mobile',
'$mobiletwo',
'$email',
'$mainarea',
'$installation',
'$Contract',
'$expiration',
'$SPA',
'$nservice',
'$maintenance',
'$monitoring',
'$MS',
'$certdate'
)");
if($sql)
{
echo 'Uploaded '. $sql . ' entries';
}
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<br>
<input type="submit" name="submit" value="Upload CSV File">
</form>
not used it myself but have you tried this,
http://php.net/manual/en/function.str-getcsv.php
you could also try breaking the csv up into lines using explode on \n and then splitting it up further based on the ,.
once you have all the chunks push it into the database.

Categories