i have this php code that is suppose to insert data from excel file to mysql database, from wordpress. This is the code:
<?php
global $wpdb;
$table_name = $wpdb->prefix . 'excelvalues';
echo $table_name;
if(isset($_POST["submit_file"]))
{
$file = "example.csv"//$_FILES["file"]["tmp_name"];
$file_open = fopen($file,"r");
while(($csv = fgetcsv($file_open, 1000, ",")) !== false)
{
$Anrede = $csv[0];
$Titel = $csv[1];
$Nachname = $csv[2];
$Vorname = $csv[3];
$Strasse = $csv[4];
$LKZ = $csv[5];
$PLZ = $csv[6];
$Ort = $csv[7];
$Mobil = $csv[8];
$Email = $csv[9];
$Geburtsdatum = $csv[10];
$Eintrittsdatum = $csv[11];
$Prüfungsjahr = $csv[12];
$Vermittlung = $csv[13];
$Bezirk = $csv[14];
$Kartennummer = $csv[15];
$LinkQR = $csv[16];
$wpdb->query("INSERT INTO $table_name(ID, Anrede, Titel,Nachname,Vorname,Strasse,LKZ,PLZ,Ort,Mobil,Email,Geburtsdatum,Eintrittsdatum,Prüfungsjahr,Vermittlung,Bezirk,Kartennummer,LinkQR) VALUES(NULL, '$Anrede', '$Titel', '$Nachname', '$Vorname', '$Strasse', '$LKZ', '$PLZ', '$Ort', '$Mobil', '$Email', '$Geburtsdatum', '$Eintrittsdatum', '$Prüfungsjahr', '$Vermittlung', '$Bezirk', '$Kartennummer', '$LinkQR')");
}
}
?>
The code works it inserts values on the database but the values are only the number of rows from excel but the values are missing, i dont understand what am i doing wrong.
Currently you retrieved the row id to each variable. This should work -
global $wpdb;
$table_name = $wpdb->prefix . 'excelvalues';
if(isset($_POST["submit_file"])){
$csvFile = file( 'example.csv' );
$all_data = [];
// fetch all data to array
foreach( $csvFile as $line ){
$all_data[] = str_getcsv( $line );
}
// fetch data from each row
foreach( $all_data as $row_key => $rows ){
// fetch data from each column
$Anrede = $rows[0];
$Titel = $rows[1];
$Nachname = $rows[2];
$Vorname = $rows[3];
$Strasse = $rows[4];
$LKZ = $rows[5];
$PLZ = $rows[6];
$Ort = $rows[7];
$Mobil = $rows[8];
$Email = $rows[9];
$Geburtsdatum = $rows[10];
$Eintrittsdatum = $rows[11];
$Prüfungsjahr = $rows[12];
$Vermittlung = $rows[13];
$Bezirk = $rows[14];
$Kartennummer = $rows[15];
$LinkQR = $rows[16];
// Insert into table
$wpdb->query("INSERT INTO $table_name(ID, Anrede, Titel,Nachname,Vorname,Strasse,LKZ,PLZ,Ort,Mobil,Email,Geburtsdatum,Eintrittsdatum,Prüfungsjahr,Vermittlung,Bezirk,Kartennummer,LinkQR) VALUES(NULL, '$Anrede', '$Titel', '$Nachname', '$Vorname', '$Strasse', '$LKZ', '$PLZ', '$Ort', '$Mobil', '$Email', '$Geburtsdatum', '$Eintrittsdatum', '$Prüfungsjahr', '$Vermittlung', '$Bezirk', '$Kartennummer', '$LinkQR')");
}
}
Related
I'm getting data from excel sheet and storing it in database. It is storing the data successfully. I had created two tables in database, one is to import values from excel sheet & store temporarily and other is to get values from temporarily stored table,also checking for duplicate records.
I can insert data in second table but please suggest me how to check duplicate records before storing in original table.
This is the code:
<?php
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
$row = 1;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$row++;
if($row != 1)
{
$custid = $filesop[0];
$zone = $filesop[1];
$city = $filesop[2];
$category = $filesop[3];
$focus = $filesop[4];
$customer_type = $filesop[5];
$lead_source = $filesop[6];
$exhibition = $filesop[7];
$organization_name = $filesop[8];
$assign_to = $filesop[9];
$description = $filesop[10];
$division = $filesop[11];
$product = $filesop[12];
$grade = $filesop[13];
$potential = $filesop[14];
$firstname = $filesop[15];
$lastname = $filesop[16];
$designation = $filesop[17];
$mobile = $filesop[18];
$primary_phone = $filesop[19];
$primary_email = $filesop[20];
$address_type = $filesop[21];
$address1 = $filesop[22];
$address2 = $filesop[23];
$state = $filesop[24];
$country = $filesop[25];
$hgf = "INSERT INTO temp_const set custid='$name', zone='$zone', city='$city', category='$category', focus='$focus', customer_type='$customer_type', lead_source='$lead_source', exhibition='$exhibition', organization_name='$organization_name', assign_to='$assign_to', description='$description', division='$division', product='$product', grade='$grade', potential='$potential', firstname='$firstname', lastname='$lastname', designation='$designation', mobile='$mobile', primary_phone='$primary_phone', primary_email='$primary_email', address_type='$address_type', address1='$address1', address2='$address2', state='$state', country='$country' ";
$sql = mysql_query($hgf);
$c = $c + 1;
}
}
if($sql){
echo "You database has imported successfully. You have inserted ". $c ."recoreds";
}else{
echo " Sorry! There is some problem.";
}
}
?>
You can create a unique index on the fields where you want to check on duplication, lets say it is fieldA, fieldB and fieldC
ALTER TABLE destTable add unique key idx_abc (fieldA,fieldB,fieldC);
after this you can copy your tmp table into the destTable with the option IGNORE
INSERT IGNORE INTO destTable SELECT * FROM temp_const;
or use only the field you want.
i am getting data from excel sheet and storing it in database it is storing the data successfully but i dont want store the first row of the table which is the header. help me out to get it done.
this is the code:
<?php
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
$row = 1;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$custid = $filesop[0];
$zone = $filesop[1];
$city = $filesop[2];
$category = $filesop[3];
$focus = $filesop[4];
$customer_type = $filesop[5];
$lead_source = $filesop[6];
$exhibition = $filesop[7];
$organization_name = $filesop[8];
$assign_to = $filesop[9];
$description = $filesop[10];
$division = $filesop[11];
$product = $filesop[12];
$grade = $filesop[13];
$potential = $filesop[14];
$firstname = $filesop[15];
$lastname = $filesop[16];
$designation = $filesop[17];
$mobile = $filesop[18];
$primary_phone = $filesop[19];
$primary_email = $filesop[20];
$address_type = $filesop[21];
$address1 = $filesop[22];
$address2 = $filesop[23];
$state = $filesop[24];
$country = $filesop[25];
$hgf = "INSERT INTO temp_const set custid='$name', zone='$zone', city='$city', category='$category', focus='$focus', customer_type='$customer_type', lead_source='$lead_source', exhibition='$exhibition', organization_name='$organization_name', assign_to='$assign_to', description='$description', division='$division', product='$product', grade='$grade', potential='$potential', firstname='$firstname', lastname='$lastname', designation='$designation', mobile='$mobile', primary_phone='$primary_phone', primary_email='$primary_email', address_type='$address_type', address1='$address1', address2='$address2', state='$state', country='$country' ";
$sql = mysql_query($hgf);
$c = $c + 1;
}
if($sql){
echo "You database has imported successfully. You have inserted ". $c ."recoreds";
}else{
echo " Sorry! There is some problem.";
}
}
?>
$flag = true;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
if($flag) { $flag = false; continue; }
//your code
i hope it will be helpful
I have many data on array, in this case I must insert on SQL one by one... can you help me to solve this? I want it to sort insert data... I use manual insert... can you give me solution to foreach or looping SQL?
$user_id = getUserId();
$current_dttm = getSQLDate();
$org_id = $_SESSION["ehr_org_id"];
$id = $this->getInosId();
list($patient_id,$inf_id,$num,$tindakan_id,$denum)=$args;
$inf_id = explode('%7C',$inf_id);
$inf_id_1 = $inf_id[0];
$inf_id_2 = $inf_id[1];
$inf_id_3 = $inf_id[2];
$inf_id_4 = $inf_id[3];
$inf_id_5 = $inf_id[4];
$inf_id_6 = $inf_id[5];
$inf_id_7 = $inf_id[5];
$num = explode('%7C',$num);
$num_1 = $num[0];
$num_2 = $num[1];
$num_3 = $num[2];
$num_4 = $num[3];
$num_5 = $num[4];
$num_6 = $num[5];
$num_7 = $num[6];
$tindakan_id = explode('%7C',$tindakan_id);
$tindakan_id_1 = $tindakan_id[0];
$tindakan_id_2 = $tindakan_id[1];
$tindakan_id_3 = $tindakan_id[2];
$tindakan_id_4 = $tindakan_id[3];
$tindakan_id_5 = $tindakan_id[4];
$tindakan_id_6 = $tindakan_id[5];
$tindakan_id_7 = $tindakan_id[6];
$denum = explode('%7C',$denum);
$denum_1 = $denum[0];
$denum_2 = $denum[1];
$denum_3 = $denum[2];
$denum_4 = $denum[3];
$denum_5 = $denum[4];
$denum_6 = $denum[5];
$denum_7 = $denum[6];
$sql_inf_1 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_1','$num_1','$tindakan_id_1','$denum_1','','$current_dttm','$current_dttm','$user_id','normal')";
$sql_1 = $db->query($sql_inf_1);
$sql_inf_2 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_2','$num_2','$tindakan_id_2','$denum_2','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_2);
$sql_inf_3 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_3','$num_3','$tindakan_id_3','$denum_3','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_3);
$sql_inf_4 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_4','$num_4','$tindakan_id_4','$denum_4','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_4);
$sql_inf_5 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_5','$num_5','$tindakan_id_5','$denum_5','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_5);
$sql_inf_6 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_6','$num_6','$tindakan_id_6','$denum_6','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_6);
$sql_inf_7 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_7','$num_7','$tindakan_id_7','$denum_7','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_7);
I want to insert all variable 1 - 7 on sort
You can put multiple sets of values in a single INSERT query. So concatenate all the values that you're inserting.
$values = array();
for ($i = 0; $i < count($inf_id); $i++) {
$values[] = "('$id', '$patient_id', '$org_id', '{$inf_id[$i]}','{$num[$i]}','{$tindakan_id[$i]}','{$denum[$i]}','','$current_dttm','$current_dttm','$user_id','normal')"
}
$values_string = implode(',', $values);
$sql = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd) VALUES $values_string";
$db->query($sql);
I am facing an error while inserting data records from CSV file to mysql database. I have a script which works fine. It inserts data from CSV file to mysql database. It has around 40,000 records. But after running the script, it inserts only around 1000 records and stops without error. What may be the problem? Please help.
Below is my script for inserting data.
include('dbconnection.php');
if (($handle = fopen("hotels.csv", "r")) !== FALSE) {
$flag = true;
$id=1;
while (($data = fgetcsv($handle, 100000000, ",")) !== FALSE) {
if($flag){
$flag = false;
continue;
}
$hotelid = mysql_real_escape_string($data[0]); //var_dump($hotelid); exit();
$hotelname = mysql_real_escape_string($data[1]);
$address1 = mysql_real_escape_string($data[2]);
$address2 = mysql_real_escape_string($data[3]);
$country = mysql_real_escape_string($data[4]);
$city = mysql_real_escape_string($data[5]);
$postcode = mysql_real_escape_string($data[6]);
$telephone = mysql_real_escape_string($data[7]);
$hotelfax = mysql_real_escape_string($data[8]);
$hotelemail = mysql_real_escape_string($data[9]);
$longitude = mysql_real_escape_string($data[10]);
$latitude = mysql_real_escape_string($data[11]);
$website = mysql_real_escape_string($data[12]);
$location = mysql_real_escape_string($data[13]);
$description = mysql_real_escape_string($data[14]);
$starrating = mysql_real_escape_string($data[15]);
$statecode = mysql_real_escape_string($data[16]);
$select_query = "SELECT * FROM `hotelDB`.`hotels` WHERE `hotelid` = $hotelid";
if(mysql_query($select_query) != '$hotelid'){
$sql = "INSERT IGNORE INTO `hotelDB`.`hotels` (`id`,`hotelid`, `hotelname`, `address1`,`address2`,`country`,`city`,`postcode`,`telephone`,`hotelfax`,`hotelemail`,`longitude`,`latitude`,`website`,`location`,`description`,`starrating`,`statecode`) VALUES ('$id','$hotelid', '$hotelname', '$address1','$address2', '$country', '$city', '$postcode', '$telephone', '$hotelfax', '$hotelemail', '$longitude', '$latitude', '$website', '$location', '$description', '$starrating', '$statecode')";
echo $sql;
$retval = mysql_query($sql,$conn);
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "<p style='color: green;'>Entered data having id = " .$id. " successfully</p><br>";
$id++;
}
}
echo "<br><p style='color: orange;'>Congratulation all data successfully inserted</p>";
fclose($handle);
}
mysql_close($conn);`
The insertion stops after inserting some values. This is example screenshot where the code stops executes.
You can try one query with multiple-row INSERT. In below example I am inserting 500 row with one insert query. You can change it to 1000.
<?php
include('dbconnection.php');
if (($handle = fopen("grnconnect-dump-hotels.csv", "r")) !== FALSE) {
//var_dump( $handle);
$flag = true;
$id=1;
$counter=0;
$sql = "INSERT IGNORE INTO `hotelDB`.`hotels` (`id`,`hotelid`, `hotelname`, `address1`,`address2`,`country`,`city`,`postcode`,`telephone`,`hotelfax`,`hotelemail`,`longitude`,`latitude`,`website`,`location`,`description`,`starrating`,`statecode`) VALUES";
$saveString="";
while (($data = fgetcsv($handle, 100000000, ",")) !== FALSE) {
//var_dump($data); exit();
if($flag){
$flag = false;
continue;
}
$hotelid = mysql_real_escape_string($data[0]); //var_dump($hotelid); exit();
$hotelname = mysql_real_escape_string($data[1]);
$address1 = mysql_real_escape_string($data[2]);
$address2 = mysql_real_escape_string($data[3]);
$country = mysql_real_escape_string($data[4]);
$city = mysql_real_escape_string($data[5]);
$postcode = mysql_real_escape_string($data[6]);
$telephone = mysql_real_escape_string($data[7]);
$hotelfax = mysql_real_escape_string($data[8]);
$hotelemail = mysql_real_escape_string($data[9]);
$longitude = mysql_real_escape_string($data[10]);
$latitude = mysql_real_escape_string($data[11]);
$website = mysql_real_escape_string($data[12]);
$location = mysql_real_escape_string($data[13]);
$description = mysql_real_escape_string($data[14]);
$starrating = mysql_real_escape_string($data[15]);
$statecode = mysql_real_escape_string($data[16]);
if(trim($counter)=="500") {
$sql = $sql.''.rtrim($saveString,",");
echo $sql;echo "<br /";
mysql_query($sql);
$counter=0;
$saveString="";
}
$saveString .="('".$id."','".$hotelid."', '".$hotelname."', '".$address1."','".$address2."', '".$country."', '".$city."', '".$postcode."', '".$telephone."', '".$hotelfax."', '".$hotelemail."', '".$longitude."', '".$latitude."', '".$website."', '".$location."', '".$description."', '".$starrating."', '".$statecode."'),";
$counter++;
}
if(trim($saveString)!=='') {
$sql = $sql.''.rtrim($saveString,",");
echo $sql;echo "<br /";
mysql_query($sql);
}
echo "<br><p style='color: orange;'>Congratulation all data successfully inserted</p>";
fclose($handle);
}
mysql_close($conn);
?>
Is there a particular reason to insert via script ?
Else I'd suggest you just insert manually or let the DB do it, sinces it's more efficient anyway. MySQL provides functions
for this: http://www.mysqltutorial.org/import-csv-file-mysql-table/
I need to insert multple records i have this right now.
Its adds also the empty field because i dont know how to exclude them from inserting:
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
/* as_factuurregel */
$product1 = $_POST['product1'];
$product2 = $_POST['product2'];
$product3 = $_POST['product3'];
$product4 = $_POST['product4'];
$product5 = $_POST['product5'];
$product6 = $_POST['product6'];
$aantal1 = $_POST['aantal1'];
$aantal2 = $_POST['aantal2'];
$aantal3 = $_POST['aantal3'];
$aantal4 = $_POST['aantal4'];
$aantal5 = $_POST['aantal5'];
$aantal6 = $_POST['aantal6'];
$sql = "INSERT INTO as_factuurregel (productid, factuurid, aantal)
VALUES ('$product1', 'test', '$aantal1'),('$product2', 'test', '$aantal2'),('$product4', 'test', '$aantal3'),('$product5', 'test', '$aantal5'),('$product5', 'test', '$aantal5')";
Thanks for helping.
Greetings
Try this:
$product1 = $_POST['product1'];
$product2 = $_POST['product2'];
$product3 = $_POST['product3'];
$product4 = $_POST['product4'];
$product5 = $_POST['product5'];
$product6 = $_POST['product6'];
$array_one = array($product1,$product2,$product3,$product4,$product5,$product6);
$aantal1 = $_POST['aantal1'];
$aantal2 = $_POST['aantal2'];
$aantal3 = $_POST['aantal3'];
$aantal4 = $_POST['aantal4'];
$aantal5 = $_POST['aantal5'];
$aantal6 = $_POST['aantal6'];
$array_two = array($aantal1,$aantal2,$aantal3,$aantal4,$aantal5,$aantal6);
$newArray = array();
foreach ($array_one as $key => $value) {
if($value != '' && $array_two[$key] != '')
{
$newArray[] = "('$value','test','$array_two[$key]')";
}
}
$values = implode(",",$newArray);
$sql = "INSERT INTO as_factuurregel (productid, factuurid, aantal)
VALUES ".$values;
You can optimize this solution if you optimize your input fields and move them in an array.