I'm having a problem with my importing csv script into mysql:
from now i can put the data from csv into mysql but only with strictly header arrange, but my export from csv always change the head of table, and i cannot control it.
my csv file is different from the table for example:
Name Status Last_name email_address employee_address phone_number
And all i need is to take the column name and put it into right column from table.
Thanks.
if (isset($_POST["import"])) {
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
$captabel = true;
$flag = true;
while (($column = fgetcsv($file, 1000, ",")) !== FALSE) {
if($captabel) { $captabel = false; continue; }
$celuleNecesare = array(0);
foreach ($celuleNecesare as $value) {
if(trim($column[$value]) == "" || $column[$value] == NULL)
$flag = false;
}
if($flag) {
$query = "INSERT into test (name ,employee_status, surname, email, address, phone)
values ('" . $column[2] . "','" . $column[7] . "','" . $column[8] . "','" . $column[5] . "','" . $column[0] . "','0" . $column[3] . "')";
$result = mysqli_query($conn, $query);
if (! empty($result)) {
$type = "success";
header( 'refresh: 0; success.php' );
} else {
$type = "error";
$message = "Problem in Importing CSV Data";
}
}
}
}
}
Just taking a quick look at your code, I have noticed something that is amiss:
if (isset($_POST["import"]))
{
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0)
{
$file = fopen($fileName, "r");
$captabel = true;
$flag = true;
while (($column = fgetcsv($file, 1000, ",")) !== FALSE)
{
if($captabel)
{
$captabel = false;
continue;
}
$celuleNecesare = array(0); <-- Is this being set externally to the code listed here.
foreach ($celuleNecesare as $value)
{
if(trim($column[$value]) == "" || $column[$value] == NULL)
$flag = false;
}
if($flag)
{
$query = "INSERT INTO test
(name ,employee_status, surname, email, address, phone)
VALUES
('" . $column[2] . "','" . $column[7] . "','" . $column[8] . "','" . $column[5] . "','" . $column[0] . "','0" . $column[3] . "')";
$result = mysqli_query($conn, $query);
if (! empty($result))
{
$type = "success";
header( 'refresh: 0; success.php' );
}
else
{
$type = "error";
$message = "Problem in Importing CSV Data";
}
}
}
}
}
You have the line $celuleNecesare = array(0); and then loop through it, but that does nothing in itself.
Your code should be something similar to this:
if (isset($_POST["import"]))
{
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0)
{
$file = fopen($fileName, "r");
$captabel = true;
$flag = true;
// Get the first row as the header row.
$headers = fgetcsv($file, 1000, ",");
// Variables to be used.
$counter = 0;
$nameCol = 0;
$statuscol = 0;
$lastnameCol = 0;
$emailAddressCol = 0;
$employeeAddressCol = 0;
$phoneNumberCol = 0;
foreach($headers as $header)
{
// Name Status Last_name email_address employee_address phone_number
// Lets work through the header row.
switch($header)
{
case 'Name' : $nameCol = $counter;
break;
case 'Status' : $statusCol = $counter;
break;
case 'Last_name': $lastNameCol = $counter;
break;
case 'email_address' : $emailAddressCol = $counter;
break;
case 'employee_address' : $employeeAddressCol = $counter;
break;
case 'phone_number' : $phoneNumberCol = $counter;
break;
default : die("Unknown column, ".$header);
break;
}
$counter++;
}
while (($column = fgetcsv($file, 1000, ",")) !== FALSE)
{
$query = "INSERT INTO test
(name ,employee_status, surname, email, address, phone)
VALUES
('" . $column[$nameCol]."','".$column[$employeeStatusCol]."','".$column[$lastnameCol]."','".$column[$emailAddressCol]."','".$column[$employeeAddressCol]."','0".$column[$phoneNumberCol]."')";
$result = mysqli_query($conn, $query);
// This section below would stop after the first record!!!!!
if (! empty($result))
{
$type = "success";
header( 'refresh: 0; success.php' );
}
else
{
$type = "error";
$message = "Problem in Importing CSV Data";
}
}
}
}
Your IF condition at the end would mean that you are only inserting a single record. Not sure if that is meant, but since you have a while loop for reading in the data, thought there would be many more records.
Related
I am just trying to skip the first row while importing a three-column .csv
I did see some complicated solutions which I believe don't fit in my code.
Can I get some help?
here is my the code for importing CSV:
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {
$value_age = $column[0];
$value_no = $column[1];
$value_benefit = $column[2];
$rider_id = $_POST['rider_id'];
$insertId = mysqli_query($con, "INSERT into shield_rider_value set company_id='" . $company_id . "', policy_id='" . $policy_id . "', rider_id='" . $rider_id . "', rValue_age='" . $value_age . "', rValue_no='" . $value_no . "', rValue_benefit='" . $value_benefit . "'");
if (!empty($insertId)) {
echo'<script> window.location.replace("?p=policy&pId='. $policy_id .'&alert=3"); </script>';
} else {
echo'<script> window.location.replace("?p=policy&pId='. $policy_id .'&alert=0"); </script>';
}
}
}
I also want to add a title row to the export file which is more complex for me.
export code I am using is:
$rider_id = $_GET['rider_data_download'];
$rider_list = $con->query("SELECT * FROM shield_riders where rider_id = $rider_id");
while($row = $rider_list->fetch_assoc()) {
$filename = "$row[rider_name].csv";
$fp = fopen('php://output', 'w');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
$query = "SELECT rValue_age, rValue_no, rValue_benefit FROM shield_rider_value WHERE rider_id=$rider_id ";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_row($result)) {
fputcsv($fp, $row);
}
exit;
}
Thanks!
I just found the simple solution for skipping the first row of CSV while import:
fgetcsv($file); // adding this before while loop.
Maybe this can help someone else too!
I am facing this problem in excel file the value is 1.93E+11 and It is not converted into 193000000000 when I import the csv to MYSQL table. It's value remain the same as 1.93E+11
How can I do it to convert it my code is
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
while (($column = fgetcsv($file, 58000, ",")) !== FALSE) {
$sqlInsert = "INSERT into excel ()
values ('" . $column[0] . "','" . $column[1] . "')";
$result = mysqli_query($conn, $sqlInsert);
if (! empty($result)) {
$type = "success";
$message = "CSV Data Imported into the Database";
You can typecast it to a float:
$value = (float) $column[1];
To be sure that you actually have a floating point number you can try checking like this
if (is_numeric($column[1]) && !is_int($column[1])) {
$value = (float) $column[1];
}
I need some help over here... what I try to do is to read a csv file (which is already converted from xls) then get all lines and put every field of a line into db
name,lastname,idprod
name,lastname,idprod
name,lastname,idprod
the issue might be that some fields are empty,
but after launching the script it starts working for like 1883 lines and put them in database, then the
$row["productid"] ." - ". $productid ."";
starts outputting empty value
here -
for the rest of the lines
Sorry for code in pastebin, this editor won't allow me to post this question with code saying some issues...
<?php
$rowe = 0;
$exploded;
$data;
$ass = 0;
$hardcore[0][0];
$swag = 0;
if (($handle = fopen("price_list_EN.csv", "r")) !== FALSE) {
$ass = 0;
$swag++;
while (($linebyline = fgetcsv($handle, 1000, ",")) !== FALSE) {
//$linebyline = explode(",", $linebyline);
$hardcore[$rowe][$ass] = $linebyline[$ass];
//echo $linebyline[$ass];
$exploded[$rowe][$ass] = $hardcore[$rowe][$ass];
//echo $exploded[$rowe][$ass];
//while (($explode = fgetcsv($linebyline, 1000, ",")) !== FALSE)
{
$num = count($data);
if ($ass == count($linebyline)){
echo "<br>";
$ass = 0;
$rowe++;
}else{
$ass++;
}
}
//}
fclose($handle);
}
//$linebyline = explode("\n", $_POST['message']);
//$linebyline = explode("\n", $data2);
//echo count($data2);
$imo=0;
$normovalue=0;
$normovalue2=0;
$normovalue3=0;
for ($i=0;$i<30000;$i++){
if ($exploded[$i][0] == ''){
$imo = $i;
}
}
for ($rowe=0;$rowe<$imo;$rowe++){
for ($ass=0;$ass<9;$ass++){
//$exploded = explode(";", $linebyline[$i]);
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('moday', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
}
if ($exploded[$rowe][0] !== "" || $exploded[$rowe][4] !== "" ||
$exploded[$rowe][3] !== "" || $exploded[$rowe][1] !== ""){
$productid = $exploded[$rowe][0];
$price = round($exploded[$rowe][4],2);
$disponibilita = $exploded[$rowe][3];
$indir = "null";
$descrizione = $exploded[$rowe][1];
$conn = new mysqli("localhost", "root", "", "moday");
$sql = "SELECT productid from data where productid='$productid'";
$result = $conn->query($sql);
if ($result->num_rows == 0) {
mysql_query("INSERT INTO
data(productid,price,disponibilita,indir,descrizione)
values('$productid','$price','$disponibilita','$indir','$descrizione')")
or die("Query non valida: " . mysql_error());
$normovalue++;
}else{
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row["productid"] == $productid){
echo "here ". $row["productid"] ." - ". $productid ."";
if ("$exploded[$rowe][0] == '' || $exploded[$rowe][1] ==
'' || $exploded[$rowe][3] == '' || $exploded[$rowe][4] == '' ||
$exploded[$rowe][2] == ''|| $exploded[$rowe][5] hh "){
echo $exploded[$rowe][0];
echo $rowe;
}
echo "<br /> \n";
$sql2 = "UPDATE data SET price='$price',
disponibilita='$disponibilita' where productid='$productid'";
$normovalue2++;
$result2 = $conn->query($sql2);
}else{
}
}
}
/*$risultato = mysql_query("INSERT INTO
data(productid,price,disponibilita,indir,descrizione)
values('$productid','$price','$disponibilita','$indir','$descrizione')")
or die("Query non valida: " . mysql_error());
*/
//echo "<br /> \n"
}
}
$normovalue3 = $normovalue + $normovalue2;
echo "N.1 is $normovalue 2 is $normovalue2 3 is $normovalue3";
?>
Put the $result into the database, as you desire:
<?php
$csv = "Chris,Garcia,1\nJohn,Doe,2\nJames,Smith,3";
foreach(explode("\n", $csv) as $key => $value) {
$result = explode(",", $value);
echo "Name: " . $result[0] . ", Lastname: " . $result[1] . ", ID: " . $result[2] . "\n";
}
exit();
?>
Hai i am using php to import a .csv file. The code is working fine. Now i am showing only error message if the data is not import means. I want to show the error message with wrong fileds. Here is my code :
if (isset($_POST["submitbutton"])) {
if (!empty($_FILES['file']['name'])) {
if (pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION) == 'csv') {
$handle = fopen($_FILES['file']['tmp_name'], "r");
//Grab the header in csv
$headers = fgetcsv($handle, 1000, ",");
$not_valid = [];
$i = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$aValid = false;
$field1 = mysqli_real_escape_string($dbConnection, $data[0]);
$field2 = mysqli_real_escape_string($dbConnection, $data[1]);
$field3 = mysqli_real_escape_string($dbConnection, $data[2]);
$field4 = mysqli_real_escape_string($dbConnection, $data[3]);
/* To Upload course Subscription data only course_id equal (25-11-2015) */
$sql = "select * from m_tl_course where id='$field2'";
$check = mysql_query($sql);
$sql1 ="select * from m_tl_user where id ='$field1'";
$check1 = mysql_query($sql1);
$sql4 = "select * from m_tl_role where id = '$field4'";
$check4 = mysql_query($sql4);
$sql2 = "select * from m_tl_subscription inner join m_tl_mastercourse on m_tl_subscription.Master_id = m_tl_mastercourse.Id where m_tl_mastercourse.LMS_Course_Id = '$field2'";
$check2 = mysql_query($sql2);
//$sql3="SELECT * FROM m_tl_role_assignments inner join m_tl_context where m_tl_role_assignments.userid='$field1' and m_tl_role_assignments.roleid='$field4' and m_tl_context.instanceid='$field2' and m_tl_context.contextlevel='50' ";
$time=strtotime("now");
$enrol="manual";
if((mysql_num_rows($check) > 0 && mysql_num_rows($check1) > 0 && mysql_num_rows($check4) > 0 ) && ( mysql_num_rows($check2) < 1 && mysql_num_rows($check3) < 1)) {
$aValid = true;
} else {
$not_valid[] = $i;
}
if ($aValid) {
$ret = "select m_tl_mastercourse.* from m_tl_mastercourse inner join m_tl_course on m_tl_mastercourse.LMS_Course_ID = m_tl_course.id where m_tl_course.id='$field2'";
$result=get_records_sql($ret,$limitfrom='', $limitnum='');
foreach($result as $res) {
$master_id = $res->Id;
}
$ret1 = "select * from m_tl_user where id='$field1'";
$result1=get_records_sql($ret1,$limitfrom='', $limitnum='');
foreach($result1 as $res1) {
$user_id = $res1->id;
}
$ret2="SELECT m_tl_context.id FROM m_tl_context inner join m_tl_course on
m_tl_context.instanceid=m_tl_course.id where m_tl_course.id = '$field2' and m_tl_context.contextlevel = '50' ";
$result3=get_records_sql($ret2,$limitfrom='', $limitnum='');
foreach($result3 as $res2) {
$context_id = $res2->id;
}
$import1= "Insert into m_tl_role_assignments values('','$field4','$context_id','$user_id','0','$time','0','0','$roleid','$enrol','0')";
mysql_query($import1);
$import = "INSERT into m_tl_subscription values('','$user_id','$master_id')";
mysql_query($import);
}
$i++;
}
fclose($handle);
if (!empty($not_valid)) {
$total_rows = $i - 1;
$valid_rows = $i - 1 - count($not_valid);
$invalid_rows = count($not_valid);
$v_ids = implode(', ', $not_valid);
echo "You are Uploading " . $total_rows. " Records. " . " <br><br> ". "In this " . $valid_rows . " Records are Inserted Successfully. and " . $invalid_rows . " records are failed to insert. " ."<br><br>". " So Please check the following csv Row Number's - " . $v_ids;
} else {
echo "Course Subscription Uploaded Successfully";
}
} else {
echo "<script>alert('csv files only allowed to upload')</script>";
}
} else {
echo "<script>alert('Please select a file')</script>";
}
}
if (is_uploaded_file($_FILES['file']['tmp_name']) && $_FILES['file']['error'] == 0) {
$target_Path = "../moodle/upload/";
$target_Path = $target_Path . basename($_FILES['file']['name']);
$file_location = move_uploaded_file($_FILES['file']['tmp_name'], $target_Path);
}
How to get the particular error field value.
How can I import 200k data faster?
And when I importing csv (delimited by comma) file using online, I got 403 error, and it inserted 200-400 data only. Also when I try to import it using localhost (xampp) i got
"Exception EAccessViolation in module xampp-control.exe at 001AA712.
Access violation at address 005AA712 in module 'xampp-control.exe'.
Read of address 00000042"
And the SQL Database connection is gone.
This is the code I used.
set_time_limit(0);
ignore_user_abort(true);
$file = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$temp = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if( ! $file)
{
$data['error'] = "Please select a file!";
}
else if($type != "application/vnd.ms-excel" && $type != "application/octet-stream")
{
$data['error'] = "Invalid file type!";
}
else
{
$newname = $file." - ".date("Ymd His");
move_uploaded_file($temp, "uploads/".$newname);
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "uploads/".$newname;
if( ! file_exists($csvfile))
{
echo "File not found. Make sure you specified the correct path.\n";
exit;
}
$file = fopen($csvfile,"r");
if( ! $file)
{
echo "Error opening data file.";
exit;
}
$size = filesize($csvfile);
if(!$size)
{
echo "File is empty.";
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$row = 1;
$data_imported = 0;
$file3 = fopen($csvfile,"r");
$total_file_count = (count(file(FCPATH."/".$csvfile)) - 2);
$i = 0;
$insert = "INSERT IGNORE INTO `invoice`
(`row1`,
.
.
to
.
.
`row33`
) VALUES ";
while($datas = fgetcsv($file3, 10000, ","))
{
$i++;
ob_implicit_flush(true);
if($row == 1)
{
// Ignore 1st line
}
else
{
$row1 = isset($datas[0]) ? $datas[0] : "";
.
.
to
.
.
$row33 = isset($datas[32]) ? $datas[32] : "";
if($i == 200 OR $total_file_count == $data_imported)
{
$insert .= "(
'".mysqli_real_escape_string($this->db->conn_id(),$row1)."',
.
.
to
.
.
'".mysqli_real_escape_string($this->db->conn_id(),$row33)."'
);";
}
else
{
$insert .= "(
'".mysqli_real_escape_string($this->db->conn_id(),$row1)."',
.
.
to
.
.
'".mysqli_real_escape_string($this->db->conn_id(),$row33)."'
),";
}
if($i == 200 OR $total_file_count == $data_imported)
{
$this->QModel->query($insert);
$i=0;
$insert = "INSERT IGNORE INTO `invoice`
(`row1`,
.
.
to
.
.
`row33`
) VALUES ";
}
$data_imported++;
}
$row++;
}
fclose($file3);
echo "Success imported ".number_format($data_imported)." data.";
Any ideas?
Thank you.