Help with LOAD DATA INTO FILE - php

I have a database setup like
`id` int(11) unsigned NOT NULL auto_increment,
`ad-id` int(11) default NULL,
`advertiser-id` int(11) default NULL,
`advertiser-name` varchar(250) default NULL,
`advertiser-category` varchar(250) default NULL,
`buy-url` varchar(255) default NULL,
`catalog-id` varchar(255) default NULL,
`currency` varchar(3) default NULL,
`description` varchar(255) default NULL,
`image-url` varchar(255) default NULL,
`in-stock` varchar(255) default NULL,
`isbn` varchar(13) default NULL,
`manufacturer-name` varchar(255) default NULL,
`manufacturer-sku` varchar(50) default NULL,
`name` varchar(255) default NULL,
`price` decimal(10,2) default NULL,
`retail-price` decimal(10,2) default NULL,
`sale-price` decimal(10,2) default NULL,
`sku` varchar(50) default NULL,
`upc` varchar(50) default NULL,
I am trying to load the data like this:
LOAD DATA LOW_PRIORITY LOCAL INFILE "/home/datafeed/tempfile.csv" REPLACE INTO TABLE products_import FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\n" (ad-id, advertiser-id, advertiser-name, advertiser-category, buy-url,catalog-id,currency, description,image-url,in-stock,isbn,manufacturer-name,manufacturer-sku,name,price,retail-price,sale-price,sku, upc);
but it doesn't seem to like database names with dashes in them. The field names are pretty much set in stone. If I try to insert without the field names it sets everything in the wrong field (starts with autoinc)
I'm not very good with MySQL any help would be appreciated.

Put back-ticks around the field names: `ad-id`, etc., like in your table structure listing.

Related

How do i make my database auto-generate unique Certificate number that will also be auto-incremented for example 00001

I am creating a database that will store 'graduate details'. I have been able to set the ID to auto increment, Unique and also my primary key that start from 1...
I also want to make the certificate number field unique and auto-increment value in an increasing order that will start from 00001.
Please any help on how to go about this will be very appreciated. Thank you.
This is the MYSQL code for the database below;
CREATE TABLE IF NOT EXISTS `graduates` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`f_name` varchar(25) NOT NULL,
`l_name` varchar(25) NOT NULL,
`gender` varchar(6) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
`city` varchar(15) DEFAULT NULL,
`region` varchar(30) DEFAULT NULL,
`phone` varchar(15) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`certificate_number` int(50) NOT NULL AUTO_INCREMENT,
`programme` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`marks` int(10) NOT NULL,
`college_name` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`date_of_birth` date DEFAULT NULL,
`created_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`created_at` date DEFAULT NULL,
`updated_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `certificate_number` (`certificate_number`)
) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8;

One Table is not being created other is being created

I am working on creating table in to my database with mysql and here is my code:
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL DEFAULT '',
`rating` tinyint(2) NOT NULL DEFAULT '0',
`proj_date` date NOT NULL DEFAULT '0000-00-00',
`proj_desc` text NOT NULL DEFAULT '',
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '0',
`emailid` varchar(100) NOT NULL DEFAULT '',
`customratings` varchar(100) NOT NULL DEFAULT '',
`photo_option` varchar(100) NOT NULL DEFAULT '',
`title` varchar(100) NOT NULL DEFAULT '',
`citation` varchar(100) NOT NULL DEFAULT '',
`date_option` varchar(100) NOT NULL DEFAULT '',
`rating_option` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);
But this is not reflecting into my database ? Why were I may be going wrong ?
but I tried creating other table with the following code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `setting` (
`id` int(11) NOT NULL auto_increment,
`script_url` text NOT NULL,
`date` varchar(4) NOT NULL,
`rateing` varchar(4) NOT NULL,
`photo` varchar(4) NOT NULL,
`dateformat` varchar(4) NOT NULL,
`page_limit` int(4) NOT NULL,
`proj_desc` varchar(4) NOT NULL,
`companyname` varchar(4) NOT NULL,
`text_color` varchar(255) NOT NULL,
`citation_color` varchar(255) NOT NULL,
`bg_color` varchar(255) NOT NULL,
`border_color` varchar(255) NOT NULL,
`ratingsformat` varchar(250) NOT NULL,
`rating` varchar(250) NOT NULL,
`customratings` varchar(250) NOT NULL,
`speed` varchar(250) NOT NULL,
`pagination` varchar(250) NOT NULL,
`version` varchar(250) NOT NULL,
`global_option` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;
") or mysqli_error($link);
it is being created correctly and both the tables are in the same file
No you cannot create table using an insert statement.
There are four types of SQL statements:
DML (DATA MANIPULATION LANGUAGE)
DDL (DATA DEFINITION LANGUAGE)
DCL (DATA CONTROL LANGUAGE)
TCL (TRANSACTION CONTROL LANGUAGE)
DML is your SELECT, INSERT, UPDATE and DELETE statements...
DDL is you your CREATE, ALTER and DROP statements.
See more info about types of SQL statements
In order to insert data in your table, first you need to create it.
How to create sql table from php
No. "INSERT INTO" used to insert the data to existing table only.You should create the table with respective column before try to insert the values.
Or you can check the table exist or not before going insert."If exists" u can insert the value else just create and insert the values.
This worked for me but I don't know how?
I just removed DEFAULT '' present while creating review table and table just got created
Here is edited code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL,
`rating` tinyint(2) NOT NULL,
`proj_date` date NOT NULL,
`proj_desc` text NOT NULL,
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL,
`emailid` varchar(100) NOT NULL,
`customratings` varchar(100) NOT NULL,
`photo_option` varchar(100) NOT NULL,
`title` varchar(100) NOT NULL,
`citation` varchar(100) NOT NULL,
`date_option` varchar(100) NOT NULL,
`rating_option` varchar(100) NOT NULL,
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);

Using PHPExcel to export Excel to MySql

I've found information on how to export data from MySQL to Excel, but not upside down, from Excel to MySQL.
Also, I'm asking using PHPExcel because a friend suggested to do it with it, but if there is an easiest way to do it, it's welcome.
I know I can export from excel to MySql (with static field) using following syntax:
LOAD DATA LOCAL INFILE '\\path' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'
But not all Excel fields will be imported into MySQL or in the same order.
This is the table structure that I have:
CREATE TABLE cliente (
clienteId int(11) NOT NULL AUTO_INCREMENT,
clienteIdentidadTipo varchar(45) DEFAULT NULL,
clienteIdEntidadNumero varchar(45) DEFAULT NULL,
clienteNombres varchar(100) DEFAULT NULL,
clienteApellidosPrimero varchar(45) DEFAULT NULL,
clienteApellidosSegundo varchar(45) DEFAULT NULL,
clienteDireccion varchar(45) DEFAULT NULL,
clienteCelular varchar(45) DEFAULT NULL,
clienteTelefono varchar(45) DEFAULT NULL,
clienteGenero varchar(45) DEFAULT NULL,
clienteFechaNacimiento date DEFAULT NULL,
clienteEstadoCivil varchar(45) DEFAULT NULL,
clienteCargo varchar(45) DEFAULT NULL,
clienteOcupacion varchar(45) DEFAULT NULL,
clienteRecibeMensajesEmail tinyint(1) DEFAULT NULL,
clienteEmail varchar(45) DEFAULT NULL,
clienteClave varchar(100) DEFAULT NULL,
clienteRecibeMensajesSms tinyint(1) DEFAULT NULL,
clienteObjetivoVermeMejor tinyint(1) DEFAULT NULL,
clienteObjetivoSalud tinyint(1) DEFAULT NULL,
clienteObjetivoDesempeno tinyint(1) DEFAULT NULL,
clientePracticaDeporte tinyint(1) DEFAULT NULL,
clienteServicioCardio tinyint(1) DEFAULT NULL,
clienteServicioPersonalizado tinyint(1) DEFAULT NULL,
clienteServicioGrupales tinyint(1) DEFAULT NULL,
clienteServicioPesas tinyint(1) DEFAULT NULL,
clienteServicioFisioterapia tinyint(1) DEFAULT NULL,
clienteServicioFuncional tinyint(1) DEFAULT NULL,
clienteServicioOtro varchar(45) DEFAULT NULL,
clienteMedioConocioGym varchar(45) DEFAULT NULL,
clienteEmergenciaNombre varchar(45) DEFAULT NULL,
clienteEmergenciaTelefono varchar(45) DEFAULT NULL,
clienteEps varchar(45) DEFAULT NULL,
clienteIps varchar(45) DEFAULT NULL,
clienteCiudadId int(11) NOT NULL,
clienteEmpresaId int(11) NOT NULL,
clienteGrupoId int(11) NOT NULL,
clienteCertificaConcentimientoInformado tinyint(1) DEFAULT NULL,
clienteCertificaCuestionamientoAntecedente tinyint(1) DEFAULT NULL,
clienteCongelacion int(11) DEFAULT NULL,
clienteDiasTolerancia int(11) DEFAULT NULL,
clienteFechaPago int(11) DEFAULT NULL,
clienteTiquetera tinyint(1) DEFAULT NULL,
clienteUsuario varchar(45) DEFAULT NULL,
historiaClinicaId int(11) NOT NULL,
planId int(11) NOT NULL,
clientePracticaDeporteCual varchar(45) DEFAULT NULL,
clienteCupoCredito int(7) NOT NULL,
clienteImagen varchar(500) NOT NULL,
clienteValidacion int(11) NOT NULL,
clienteRh varchar(45) DEFAULT NULL,
PRIMARY KEY (clienteId,clienteCiudadId,clienteEmpresaId,clienteGrupoId,historiaClinicaId,planId),
UNIQUE KEY clienteId_UNIQUE (clienteId),
KEY fk_cliente_ciudad_idx (clienteCiudadId),
KEY fk_cliente_empresa1_idx (clienteEmpresaId),
KEY fk_cliente_grupo1_idx (clienteGrupoId),
KEY fk_cliente_historiaClinica1_idx (historiaClinicaId),
KEY fk_cliente_plan1_idx (planId),
CONSTRAINT fk_cliente_ciudad FOREIGN KEY (clienteCiudadId) REFERENCES ciudad (ciudadId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_cliente_empresa1 FOREIGN KEY (clienteEmpresaId) REFERENCES empresa (empresaId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_cliente_grupo1 FOREIGN KEY (clienteGrupoId) REFERENCES grupo (grupoId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_cliente_historiaClinica1 FOREIGN KEY (historiaClinicaId) REFERENCES historiaclinica (historiaClinicaId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_cliente_plan1 FOREIGN KEY (planId) REFERENCES plan (planId) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And this is the Excel file that I want to import into the database:
This will probably help:
Import an excel file into a MySQL table with PHPExcel
Also, in the future, most of the questions you will have with programming have already been answered. So you can usually find the answer in 1-2 google searches.

Date type issue with Like clause Arabic string in mysql

I work on old system, and the search in popup was made dynamically. for example if popup fields: field1, field2, field3 and the user type كلمة in the textbox for search or any arabic word. the system will create a dynamic search condition like this:
field1 LIKE '%كلمة%' OR field2 LIKE '%كلمة%' OR field3 LIKE '%كلمة%'
the problem occur when one of these fields is date type, in this case give me mysql error:
illegal mix of collations for operation 'like'
when I change the the word I search to be English word, it give me warning but work correctly because I don't need the field of date type in this situation (I search for text).
also I can't change the core of this dynamic search php function because all system was built, so I can't use like this code:
OR DATE_FORMAT(field1, '%Y-%m-%d') LIKE '%كلمة%'
because I don't know which field is a Date type, it is a dynamic search.
also this issue occur on the hosting server only, and not on my localhost, my host is: Server version: 5.5.47-cll - MySQL Community Server (GPL) and my localhost mysql version is: 5.5.5.
this is show create table script:
CREATE TABLE `POS_QUOTATIONS` (
`DEPT_ID` varchar(20) NOT NULL,
`BILL_TYPE` int(11) NOT NULL,
`BILL_NUMBER` int(11) NOT NULL,
`BILL_NUMBER_TO_RTRN` int(11) NOT NULL,
`SALESPERSON_NAME` varchar(255) NOT NULL,
`BILL_DATE` date NOT NULL,
`POS_ID` int(11) NOT NULL,
`STOCK_ID` varchar(20) NOT NULL,
`CURRENCY_ID` varchar(3) NOT NULL,
`EXCHANGE_RATE` float NOT NULL,
`SALESPERSON_TYPE` int(1) NOT NULL,
`SALESPERSON_ID` int(10) NOT NULL,
`CLIENT_TYPE` int(1) NOT NULL,
`ACC_CODE` varchar(20) NOT NULL,
`CLIENT_NAME` varchar(255) NOT NULL,
`CLIENT_APPROVAL_NUMBER` varchar(20) NOT NULL,
`NOTES` varchar(255) NOT NULL,
`BILL_VALUE_BEFORE_DISCOUNT` double NOT NULL,
`OVRALL_COST` double DEFAULT '0',
`SALES_TAX` double NOT NULL,
`ADITIONAL_AMOUNT` double NOT NULL,
`DISCOUNT_PERCENTAGE` double NOT NULL,
`DISCOUNT_AMOUNT` double NOT NULL,
`BILL_VALUE_AFTER_DISCOUNT` double NOT NULL,
`CLIENT_PAYMENT` decimal(13,2) NOT NULL,
`REMAINING_AMOUNT` decimal(13,2) NOT NULL,
`PAYMENT_METHOD` int(11) NOT NULL,
`CREDIT_CARD_DETAILS` varchar(255) NOT NULL,
`BANK_OPERATION_NUMBER` varchar(50) NOT NULL,
`BANK_DETAILS` varchar(255) NOT NULL,
`BANK_ACC_CODE` varchar(20) NOT NULL,
`PAYMENT_METHOD_INFO` varchar(500) NOT NULL,
`CONTRACT_PRODUCT_DETAILS` text NOT NULL,
`CONTRACT_REC_VOUCHER_NO` varchar(255) NOT NULL,
`CONTRACT_REPAYMENT_DATE` varchar(255) NOT NULL,
`ROWID` varchar(255) NOT NULL,
`YEAR` int(4) NOT NULL,
`CREATION_USER` varchar(10) DEFAULT NULL,
`CREATION_TIMESTAMP` varchar(14) DEFAULT NULL,
`CREATION_COMPUTER_NAME` varchar(50) DEFAULT NULL,
`LASTUPDATE_USER` varchar(10) DEFAULT NULL,
`LASTUPDATE_TIMESTAMP` varchar(50) DEFAULT NULL,
`LASTUPDATE_COMPUTER_NAME` varchar(50) DEFAULT NULL,
`TRANS_FLAG` varchar(10) DEFAULT NULL,
`RECORD_STATUS` varchar(1) DEFAULT NULL,
`JOURNAL_STATUS` tinyint(1) DEFAULT NULL,
`PROCESS_NUMBER` varchar(50) DEFAULT NULL,
`OFFER_TERMS` text,
`TECHNICAL_SPECIFICATIONS` text,
`OFFER_STATUS` tinyint(1) DEFAULT NULL,
`PROJECT_DESCRIPTION` varchar(500) DEFAULT NULL,
`POS_CONTRACT_AGREEMENT_ID` varchar(50) DEFAULT NULL,
`COST_CENTER_CODE` varchar(50) DEFAULT NULL,
PRIMARY KEY (`ROWID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
and whatever the charset of database on the hosting server it produce this error when charset is utf8_general_ci or latin1_swedish_ci
thanks in advance.
When you use like on Date fields, they treated as latin strings. Assuming you are using utf8 charset, you need to convert date column before like it:
convert(field1 using utf8) LIKE '%كلمة%' OR convert(field2 using utf8) LIKE '%كلمة%' OR convert(field3 using utf8) LIKE '%كلمة%'
Ideally, if you add some logic here and conditionally apply conversion to date fields only. E.g. if you know that field2 is the only date field, you can build the query:
field1 LIKE '%كلمة%' OR convert(field2 using utf8) LIKE '%كلمة%' OR field3 LIKE '%كلمة%'

Pagination using category ID

I am facing an issue with pagination which display details using an category ID.
Below is the database table which we are using to fetch the data. We are getting some errors and data was not printing in the page. I have added the page code in Pastebin. Where is the error I have done?
PHP page code: http://pastebin.com/EXmu5X67
Database table: http://pastebin.com/ZiBhPDTd
--
-- Table structure for table `plot_details`
--
CREATE TABLE IF NOT EXISTS `plot_details` (
`plot_detailsid` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`content` text NOT NULL,
`imgname` varchar(100) NOT NULL,
`imgname1` varchar(100) NOT NULL,
`imgname2` varchar(100) NOT NULL,
`imgname3` varchar(100) NOT NULL,
`imgname4` varchar(100) NOT NULL,
`cid` varchar(100) NOT NULL,
`city` varchar(100) NOT NULL,
`locality` varchar(700) NOT NULL,
`sqft` varchar(100) NOT NULL,
`acre` varchar(100) NOT NULL,
`cent` varchar(100) NOT NULL,
`sqm` varchar(100) NOT NULL,
`bedroom` varchar(100) NOT NULL,
`bathroom` varchar(100) NOT NULL,
`price` varchar(100) NOT NULL,
`owner_type` varchar(100) NOT NULL,
`video_title` varchar(100) NOT NULL,
`url` varchar(100) NOT NULL,
`covered_area` varchar(400) NOT NULL,
`feet` varchar(400) NOT NULL,
`random_id` varchar(200) NOT NULL,
`email` varchar(200) NOT NULL,
`pnumber` varchar(100) NOT NULL,
`status` int(11) NOT NULL,
`field` varchar(100) NOT NULL,
`url_page` varchar(200) NOT NULL,
PRIMARY KEY (`plot_detailsid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=242 ;
I found the answer for my question below is the answer
block:
session_start();
we need to add a session start for this to get the value to be store. so the pagination will use the session id and work on all pages

Categories