How to set charset in import CSV file to mysql database? - php

I wrote a code that imports data from a CSV file into the database. However, I have a problem with setting charset on cp1250. When I use Polish characters in the database I see strange characters. I tried to use the SET NAMES function, but it does not work - any ideas?
<?php
$conn = mysqli_connect("localhost", "b", "c", "d");
if (isset($_POST["import"])) {
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {
$sqlInsert = "INSERT INTO evdb_Historia_Aktualizacja(ID,Imie_I_Nazwisko,Suma_Brutto_Przejazdy,Suma_Netto_Przejazdy,Gotowka_Przejazdy_Uber,Brutto_UberEats,Netto_UberEats,VAT_UberEats,Netto_Gotowka,Bonusy_Dodatki,Napiwki_Brutto,Napiwek_18,Napiwek,Dodatki_UberEats,Ilosc_Dni,Prowizja_Evelstar,Kwota_Po_Prowizji_Evelstar,VAT_Przejazdy,Faktury_Brutto,Faktury_Netto,VAT_Faktury,VAT_Nalezny,Suma_Bez_Skladek,Rodzaj_Umowy,Kwota_Skladek,Potracenia_Reczne,Potracenia_Stale,Przelew_Przewoz,Przelew_UberEats,Data_Od,Data_Do,Login)
values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "','" . $column[4] . "','" . $column[5] . "','" . $column[6] . "','" . $column[7] . "','" . $column[8] . "','" . $column[9] . "','" . $column[10] . "','" . $column[11] . "','" . $column[12] . "','" . $column[13] . "','" . $column[14] . "','" . $column[15] . "','" . $column[16] . "','" . $column[17] . "','" . $column[18] . "','" . $column[19] . "','" . $column[20] . "','" . $column[21] . "','" . $column[22] . "','" . $column[23] . "','" . $column[24] . "','" . $column[25] . "','" . $column[26] . "','" . $column[27] . "','" . $column[28] . "','" . $column[29] . "','" . $column[30] . "','" . $column[31] . "')";
$result = mysqli_query($conn, $sqlInsert);
if (! empty($result)) {
$type = "success";
$message = "CSV Data Imported into the Database";
} else {
$type = "error";
$message = "Problem in Importing CSV Data";
}
}
}
}
?>

Related

Upload CSV file and check if value is empy to set it = 0 php

I want to upload a csv file in my database. If a cell of the csv file is empty i want to set the value = 0 because it gives error Undefined offset. i tried to check the values in a for loop but it does not works.
$msg = 0;
if (isset($_POST['import'])) {
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
$i = 0;
while (($column = fgetcsv($file)) !== FALSE) {
if ($i > 0) {
if (!empty($column[0])){
//$insertdate = date("Y-m-d", strtotime(str_replace('/', '-', $column[3])));
$sql = "INSERT into tab1 (country,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dece)
values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "','" . $column[4] . "','" . $column[5] . "','" . $column[6] . "','" . $column[7] . "','" . $column[8] . "','" . $column[9] . "','" . $column[10] . "','" . $column[11] . "','" . $column[12] . "')";
$result = mysqli_query($conn, $sql);
if (isset($result)) {
$msg++;
}
}
}
$i++;
}
}
}
I'll update your code for add this section :
if( count($column) < 13 ){
$tmpI = count($column);
while( $tmpI < 14 ){
$column[$tmpI] = 0;
$tmpI++;
}
}
The code check if you have 13 elements in your array, if not create the missing key with the value 0.
$msg = 0;
if (isset($_POST['import'])) {
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
$i = 0;
while (($column = fgetcsv($file)) !== FALSE) {
if ($i > 0) {
if (!empty($column[0])){
if( count($column) < 13 ){
$tmpI = count($column);
while( $tmpI < 14 ){
$column[$tmpI] = 0;
$tmpI++;
}
}
//$insertdate = date("Y-m-d", strtotime(str_replace('/', '-', $column[3])));
$sql = "INSERT into tab1 (country,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dece)
values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "','" . $column[4] . "','" . $column[5] . "','" . $column[6] . "','" . $column[7] . "','" . $column[8] . "','" . $column[9] . "','" . $column[10] . "','" . $column[11] . "','" . $column[12] . "')";
$result = mysqli_query($conn, $sql);
if (isset($result)) {
$msg++;
}
}
}
$i++;
}
}
}

I am unable to increament id

I searched many stackoverflow questions it didn't help
I want to increment id by fetching last id from MySQL table.
I don't want to do auto increment in MySQL table because already one column is auto incremented.
<?php
include 'db.php';
$created = date('Y-m-d H:i:s');
//$json_data = array();
$message = array();
$error = array();
if ($_GET['vendor_id'] == "") {
$message[] = array("message" => "Values Empty");
} else {
$result = mysqli_query("SELECT loo_id FROM loo_list ORDER BY loo_id DESC LIMIT 1");
if ($result) {
$order_array = mysqli_fetch_assoc($result) or die(mysqli_error());
//echo $order_array['loo_id'];
}
$loo_id = $order_array['loo_id'] + 1;
$sql = "insert into loo_list(loo_id,name,address,geolocation,price,facility_category,facilities,count,accessbility,image,type,category,created_vendor,days,timings,terms_conditions,vendor_approval,created,warning,url,user_ids,overall,admin_approval,updated)values('" . $loo_id . "','" . $_GET['loo_name'] . "','" . $_GET['address'] . "','" . $_GET['loo_location'] . "','" . $_GET['price'] . "','" . $_GET['facility_category'] . "','" . $_GET['facilities'] . "','" . $_GET['count'] . "','" . $_GET['accessbility'] . "','" . $_GET['image'] . "','Offerers','" . $_GET['category'] . "','" . $_GET['vendor_id'] . "','" . $_GET['days'] . "','" . $_GET['timings'] . "','" . $_GET['terms_conditions'] . "','1','" . $created . "','0','','" . $_GET['user_ids'] . "','" . $_GET['overall'] . "','1','" . $created . "')";
$res1 = mysqli_query($db, $sql) or die(mysqli_error());
$message[] = array("message" => "success");
}
$json_data = array("result" => $message);
echo json_encode($json_data);
?>
Try this code.
if(trim($order_array['loo_id']) === ''){
$loo_id = 1;
}else{
$loo_id = intval($order_array['loo_id']) + 1;
}

Couldn't enter the data to mysql using php

It couldn't store the data to mysql. What to do? All variable and file name are correct.
<?php
require 'connection.php';
$conn = Connect();
$id =$conn->real_escape_string ($_POST['id']);
$name = $conn->real_escape_string ($_POST['name']);
$phone = $conn->real_escape_string ($_POST['phone']);
$address = $conn->real_escape_string ($_POST['address']);
$city = $conn->real_escape_string ($_POST['city']);
$zip = $conn->real_escape_string ($_POST['zip']);
$state = $conn->real_escape_string ($_POST['state']);
$item = $conn->real_escape_string ($_POST['item']);
$status = $conn->real_escape_string ($_POST['status']);
$enquiry_date = $conn->real_escape_string ($_POST['enquiry_date']);
$enquiry_user = $conn->real_escape_string ($_POST['enquiry_user']);
$query = "INSERT into enquiry
(id, name, phone, address, city, zip, state, item, status, enquiry_date, enquiry_user)
VALUES('" . $id . "','" . $name . "','" . $phone . "','" . $address . "','" . $city . "','" . $zip . "','" . $state . "','" . $item . "','" . $status . "','" . $enquiry_date . "')";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
echo "Thank You For Contacting Us <br>";
$conn->close();
?>
As #Jeff said:
$query = "INSERT into enquiry
(id, name, phone, address, city,
zip, state, item, status, enquiry_date, enquiry_user)
VALUES('" . $id . "','" . $name . "','" . $phone . "','" . $address . "','"
. $city . "','" . $zip . "','" . $state . "','" . $item . "','"
$status . "','" . $enquiry_date . "','" . $enquiry_user . "')";
You were missing . "','" . $enquiry_user

How to insert multiple tables array

I'm trying to insert data a form 4 table normal insert finish but data array
cannot query. Could you help me, please? 
Code:
for ($i = 1; $i <= (int)$_POST["hdnCount"]; $i++) {
$sql = "INSERT INTO status_b(emp_id,name_baby,date_baby,status_baby,class,age,id_card_baby,stay) VALUES ('" . $_POST["emp_id$i"] . "','" .
$_POST["name_baby$i"] . "','" .
$_POST["date_baby$i"] . "','" .
$_POST["status_baby$i"] . "','" .
$_POST["class$i"] . "','" .
$_POST["age$i"] . "','" .
$_POST["id_card_baby$i"] . "','" .
$_POST["stay$i"] . "');";
}
$sql = "INSERT INTO personal (st_date,emp_id,emp_date,division,department,sc,name_th,lname_th,blood,name_en, lname_en,per_date,id_card,exp_card,id_fund,date_fund,rate,acc_id,name_bank, m_bank,add_h,district_h,amphoe_h,province_h,add_n,district_n,amphoe_n,province_n, tel_home,tel,images,education,e_major,institution,name_e,tel_e)
VALUES('" . $_POST["st_date"] . "','" .
$_POST["emp_id"] . "','" .
$_POST["emp_date"] . "','" .
$_POST["division"] . "','" .
$_POST["department"] . "','" .
$_POST["sc"] . "','" .
$_POST["name_th"] . "','" .
$_POST["lname_th"] . "','" .
$_POST["blood"] . "','" .
$_POST["name_en"] . "' ,'" .
$_POST["lname_en"] . "','" .
$_POST["per_date"] . "','" .
$_POST["id_card"] . "','" .
$_POST["exp_card"] . "','" .
$_POST["id_fund"] . "','" .
$_POST["date_fund"] . "','" .
$_POST["rate"] . "','" .
$_POST["acc_id"] . "','" .
$_POST["name_bank"] . "' ,'" .
$_POST["m_bank"] . "','" .
$_POST["add_h"] . "','" .
$_POST["district_h"] . "','" .
$_POST["amphoe_h"] . "','" .
$_POST["province_h"] . "','" .
$_POST["add_n"] . "','" .
$_POST["district_n"] . "','" .
$_POST["amphoe_n"] . "','" .
$_POST["province_n"] . "' ,'" .
$_POST["tel_home"] . "','" .
$_POST["tel"] . "','" .
$_POST["images"] . "','" .
$_POST["education"] . "','" .
$_POST["e_major"] . "','" .
$_POST["institution"] . "','" .
$_POST["name_e"] . "','" .
$_POST["tel_e"] . "');";
$sql .= "INSERT INTO status (name_m,lname_m,date_m,id_card_m,date_r,location,tel_m,status,mary,num_baby)
VALUES
('" . $_POST["name_m"] . "','" .
$_POST["lname_m"] . "','" .
$_POST["date_m"] . "','" .
$_POST["id_card_m"] . "','" .
$_POST["date_r"] . "','" .
$_POST["location"] . "','" .
$_POST["tel_m"] . "','" .
$_POST["status"] . "','" .
$_POST["mary"] . "','" .
$_POST["num_baby"] . "');";
$sql .= "INSERT INTO tax (f_name,f_id,f_date,claim_f,mf_name,mf_id,mf_date,claim_mf,mm_name,
mm_id,mm_date,claim_mm,insurance,num_in,insurance_l,num_inl,unit,num_un,interest,
num_int,support,num_su,donate,num_do,buy,num_b,other,num_o)
VALUES
('" . $_POST["f_name"] . "','" .
$_POST["f_id"] . "','" .
$_POST["f_date"] . "','" .
$_POST["claim_f"] . "','" .
$_POST["mf_name"] . "','" .
$_POST["mf_id"] . "','" .
$_POST["mf_date"] . "','" .
$_POST["claim_mf"] . "','" .
$_POST["mm_name"] . "','" .
$_POST["mm_id"] . "','" .
$_POST["mm_date"] . "','" .
$_POST["claim_mm"] . "','" .
$_POST["insurance"] . "','" .
$_POST["num_in"] . "','" .
$_POST["insurance_l"] . "','" .
$_POST["num_inl"] . "','" .
$_POST["unit"] . "','" .
$_POST["num_un"] . "','" .
$_POST["interest"] . "','" .
$_POST["num_int"] . "','" .
$_POST["support"] . "','" .
$_POST["num_su"] . "','" .
$_POST["donate"] . "','" .
$_POST["num_do"] . "','" .
$_POST["buy"] . "','" .
$_POST["num_b"] . "','" .
$_POST["other"] . "','" .
$_POST["num_o"] . "');";
if (mysqli_multi_query($conn, $sql)) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);

parse csv and input to mysql

while(($data = fgetcsv($handle,0,",")) !== FALSE){
$num = count($data);
$sql = "INSERT INTO `calendar` (`service_id`, `sunday`, `monday`, `tuesday`, `wednesday`, `thursday`, `friday`, `saturday`, `start_date`, `end_date`) VALUES ('" . $data[0] . "','" . $data[1] . "','" . $data[2] . "','" . $data[3] . "','" . $data[4] . "','" . $data[5] . "','" . $data[6] . "','" . $data[7] . "','" . $data[8] . "','" . $data[9] . "');";
$collect .= $sql;
$count = count(explode(";",$collect));
if($count > 500){
$mysql->multi_query($collect);
$collect = '';
$count = 0;
}
$row++;
}
i'm parsing a csv and collecting queries and if it's more then 500 submitting to sql server
but the multi_query run only once why?

Categories