How to insert multiple tables array - php

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);

Related

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

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";
}
}
}
}
?>

mysql to postgresql database error

I create a script to convert my mysql database to a postgres database
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$psql = "
INSERT INTO subscriptions_sub (id,cust_id,tarrif_id,des,datetime_created,datetime_modified,credit,credit_limit,status,date_start,date_end,backoffice_userref,backoffice_deleted,backoffice_createdon,backoffice_createdby, backoffice_updatedon,backoffice_updatedby,backoffice_administration,backoffice_readonly,backoffice_order,backoffice_dummy)
VALUES('" .(int) $row["subscription_id"] . "','" . (int) $row["customer_id"] . "','" . (int) $row["tariffplan_id"] . "','" . $row["description"] . "',NULLIF('" . $row["datetime_created"] . "','0000-00-00 00:00:00')::timestamp,NULLIF('" . $row["datetime_modified"] . "','0000-00-00 00:00:00')::timestamp,'" . (int) $row["credit"] . "','" . (int) $row["credit_limit"] . "','" . $row["status"] . "',NULLIF('" . $row["date_start"] . "','')::timestamp,NULLIF('" . $row["date_end"] . "','')::timestamp,'" . (int) $row["backoffice_userref"] . "','" . (int) $row["backoffice_deleted"] . "',NULLIF('" . $row["backoffice_createdon"] . "','0000-00-00 00:00:00')::timestamp,'" . (int) $row["backoffice_createdby"] . "',NULLIF('" . $row["backoffice_updatedon"] . "','0000-00-00 00:00:00')::timestamp,'" . (int) $row["backoffice_updatedby"] . "','" . (int) $row["backoffice_administration"] . "','" . (int) $row["backoffice_readonly"] . "','" . (int) $row["backoffice_order"] . "','" . (int) $row["backoffice_dummy"] . "');
";
print_r($row);
$ret = pg_query($db, $psql);
if(!$ret){
echo pg_last_error($db);
} else {
echo "Records created succesfully\n";
}
When I run it I get this error:
invalid input syntax for type timestamp: "" in /home/ruud/Database2.php on line 38
ERROR: invalid input syntax for type timestamp: ""root#149-210-204-94:/home/ruud#
datetime_created and datetime_modified are from type TIMESTAMP
date_start and date_end are from type DATE
backoffice_createon and backoffice_update on are from type DATETIME
I cant seem to find out where I'm making a error.

I want to show an error if duplicates are there in database using php and mysql

I'm trying to show an error while entering duplicates using php and mysql, but i'm not getting how to complete, please give an solution........
this is my code:
mysql_query(
"INSERT INTO productcost (product, productCategory, model, purchasePrice, mrp, customerPrice, marginCustomer, dealerPrice, marginDealer)
VALUES ('" . $_POST["product"] . "','" . $_POST["productCategory"] . "','" . $_POST["model"] . "','" . $_POST["purchasePrice"] . "','" . $_POST["mrp"] . "','" . $_POST["customerPrice"] . "','" . $_POST["marginCustomer"] . "','" . $_POST["dealerPrice"] . "', '" . $_POST["marginDealer"] . "')");
$current_id = mysql_insert_id();
if(!empty($current_id)) {
$message = "New Product Added Successfully";
}
}
You have to create unique key in productcost table , using unique fields like (product, productCategory, model). Now execute insert query, if there is a recode in the table return error . now you can handle error and give message.
try{
mysql_query("INSERT INTO productcost (product_key_id,product, productCategory,model,purchasePrice, mrp, customerPrice, marginCustomer, dealerPrice, marginDealer)
VALUES
('" . $_POST["created_product_id"] . "','" . $_POST["product"] . "','".$_POST["productCategory"] . "','" . $_POST["model"] . "','".$_POST["purchasePrice"] . "','" . $_POST["mrp"] . "','".$_POST["customerPrice"] . "','" . $_POST["marginCustomer"] . "','".$_POST["dealerPrice"] . "', '" . $_POST["marginDealer"] . "')");
return TRUE;
}
catch(Exception $e){
return FALSE;
}
or you can check is there a recode in table before insert
select count(*) as cc from doc_upload where product_key_id = $_POST["created_product_id"];
To show an error message while entering duplicates:
// First check there are same data available or not using a query by counting the row
$sqlCheck = "SELECT COUNT(`id`) WHERE product = '" . $_POST["product"] . "' AND productCategory = '" . $_POST["productCategory"] . "' AND model = '" . $_POST["model"] . "'"; // You have to add mroe thing in where clause
$CheckQuery = mysql_query($sqlCheck);
// if there is no duplicate data
//
if ($CheckQuery > 0) {
# code...
mysql_query(
"INSERT INTO productcost (product, productCategory, model, purchasePrice, mrp, customerPrice, marginCustomer, dealerPrice, marginDealer)
VALUES ('" . $_POST["product"] . "','" . $_POST["productCategory"] . "','" . $_POST["model"] . "','" . $_POST["purchasePrice"] . "','" . $_POST["mrp"] . "','" . $_POST["customerPrice"] . "','" . $_POST["marginCustomer"] . "','" . $_POST["dealerPrice"] . "', '" . $_POST["marginDealer"] . "')");
$current_id = mysql_insert_id();
if(!empty($current_id)) {
$message = "New Product Added Successfully";
}
} else {
$message = "Data is Duplicated";
}
Note : I'm Giving you an Example . this is how you have to check
duplicate data

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?

Facing issue of single quotes in fckeditor

I want to enter single quotes in database through fckeditor..but My code is not work for me.
"insert into $user
(id,question,option1,option2,option3,option4,correctAnswer,category,section,chapter)
VALUES
("
. ",$newstd," .
"','". htmlspecialchars($_POST['FCKeditor0'],ENT_QUOTES) .
"','" . htmlspecialchars($_POST['FCKeditor1'],ENT_QUOTES) .
"','" . htmlspecialchars($_POST['FCKeditor2'],ENT_QUOTES) .
"','" . htmlspecialchars($_POST['FCKeditor3'],ENT_QUOTES) .
"','" . htmlspecialchars($_POST['FCKeditor4'],ENT_QUOTES) .
"','" . htmlspecialchars($_REQUEST['correctans'],ENT_QUOTES) .
"'," . htmlspecialchars($_REQUEST['MyRadio'],ENT_QUOTES) .
"'," . htmlspecialchars($_REQUEST['section'],ENT_QUOTES) .
"'," . htmlspecialchars($_REQUEST['chapter'],ENT_QUOTES) .
")";
Thank You ...
You can use it like this
insert into $user(id,question,option1,option2,option3,option4,correctAnswer,category,section,chapter)
values
(
"'.$newstd.'",
'".htmlspecialchars([\'$question1\'],ENT_QUOTES)."'
)
"insert into $user (id,question,option1,option2,option3,option4,correctAnswer,category,section,chapter) VALUES ("'$newstd'" , "',
'" . htmlspecialchars(['$question1'],ENT_QUOTES) . "',
'" . htmlspecialchars(['$question2'],ENT_QUOTES) . "',
'" . htmlspecialchars(['$question3'],ENT_QUOTES) . "',
'" . htmlspecialchars(['$question4'],ENT_QUOTES) . "',
'" . htmlspecialchars($_REQUEST['correctans'],ENT_QUOTES) . "',
" . htmlspecialchars($_REQUEST['MyRadio'],ENT_QUOTES) . "',
" . htmlspecialchars($_REQUEST['section'],ENT_QUOTES) . "',
" . htmlspecialchars($_REQUEST['chapter'],ENT_QUOTES) .
")";
Your query should be like
"insert into $user(id,question,option1,option2,option3,option4,correctAnswer,category,section,chapter)
VALUES
(
'".$newstd."' ,
'" . htmlspecialchars($question1,ENT_QUOTES) ."'
)";
You have problem with double quotes
Correct code is
"insert into $user (id,question,option1,option2,option3,option4,correctAnswer,category,section,chapter) VALUES ('$newstd','" . htmlspecialchars($question1, ENT_QUOTES) . "','" . htmlspecialchars($question2, ENT_QUOTES) . "','" . htmlspecialchars($question3, ENT_QUOTES) . "','" . htmlspecialchars($question4, ENT_QUOTES) . "','" . htmlspecialchars($_REQUEST['correctans'], ENT_QUOTES) . "'," . htmlspecialchars($_REQUEST['MyRadio'], ENT_QUOTES) . "'," . htmlspecialchars($_REQUEST['section'], ENT_QUOTES) . "','" . htmlspecialchars($_REQUEST['chapter'], ENT_QUOTES) . "')";
To maintain the readability to the code, you can use it this way
$question1= htmlspecialchars(['$question1'],ENT_QUOTES);
$question2= htmlspecialchars(['$question2'],ENT_QUOTES);
$question3= htmlspecialchars(['$question3'],ENT_QUOTES);
$question4= htmlspecialchars(['$question4'],ENT_QUOTES);
$correctans= htmlspecialchars($_REQUEST['correctans'],ENT_QUOTES);
$MyRadio= htmlspecialchars([$_REQUEST['MyRadio'],ENT_QUOTES);
$section= htmlspecialchars($_REQUEST['section'],ENT_QUOTES);
$chapter= htmlspecialchars($_REQUEST['chapter'],ENT_QUOTES);
"insert into $user (id,question,option1,option2,option3,option4,correctAnswer,category,section,chapter) VALUES (' ','$newstd','$quertion1','$quertion2','$quertion3','$quertion4','$correctans','$MyRadio','$section','$chapter')";

Categories