Duplicate entry 'Manufacturer Name-0' for key 'PRIMARY' - php

I'm an intermediate at best and help will greatly be appreciated. I'm trying to upload csv file with database of drugs, manufacturer, generic names etc.
I use this for the .php
$this->db->insert('acc_coa',$manufacturer_coa);
}
$check_category = $this->db->select('*')->from('product_category')->where('category_name',$insert_csv['category_id'])->get()->row();
if(!empty($check_category)){
$category_id = $check_category->category_id;
}else{
$categorydata=array(
'category_name' => $insert_csv['category_id'],
'status' => 1
);
if ($count > 0) {
$this->db->insert('product_category',$categorydata);
}
And this for database:
CREATE TABLE acc_coa (
HeadCode varchar(50) COLLATE utf8_unicode_ci NOT NULL,
HeadName varchar(100) COLLATE utf8_unicode_ci NOT NULL AUTO_INCREMENT,
PHeadName varchar(50) COLLATE utf8_unicode_ci NOT NULL,
HeadLevel int(11) NOT NULL,
IsActive tinyint(1) NOT NULL,
IsTransaction tinyint(1) NOT NULL,
customer_id int(11) DEFAULT NULL,
manufacturer_id int(11) DEFAULT NULL,
IsGL tinyint(1) NOT NULL,
HeadType char(1) COLLATE utf8_unicode_ci NOT NULL,
IsBudget tinyint(1) NOT NULL,
IsDepreciation tinyint(1) NOT NULL,
DepreciationRate decimal(18,2) NOT NULL,
CreateBy varchar(50) COLLATE utf8_unicode_ci NOT NULL,
CreateDate datetime NOT NULL,
UpdateBy varchar(50) COLLATE utf8_unicode_ci NOT NULL,
UpdateDate datetime NOT NULL,
PRIMARY KEY (HeadName)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
But anytime i try i get this
Error Number: 1062
Duplicate entry 'Manufacturer Name-0' for key 'PRIMARY'
INSERT INTO acc_coa (HeadCode, HeadName, PHeadName, HeadLevel, IsActive, IsTransaction, IsGL, HeadType, IsBudget, manufacturer_id, IsDepreciation, DepreciationRate, CreateBy, CreateDate) VALUES ('502020000001', 'Manufacturer Name-0', 'Account Payable', '3', '1', '1', '0', 'L', '0', 0, '0', '0', '1', '2021-05-27 18:15:47')
Filename: C:/wamp64/www/efkwa/system/database/DB_driver.php
Line Number: 691

Are there any rows already inserted? It looks like that the Primary-Key-Value "Manufacturer Name-0" is already present.
Maybe your insert is executed twice?
Try to execute
DELETE FROM acc_coa;
and then try to insert again.

This error indicates that you already entered an example / row with that value (Manufacturer Name-0), the primary key acts as a unique identifier for each example, therefore it cannot be repeated in other examples / rows of the table

It's been resolved...
I deleted from line 691
// Display errors
return $this->display_error(array('Error Number: '.$error['code'], `enter code here`$error['message'], $sql));
But i would like to know why ?

You can do the following in MySQL client:
Create and insert the data into a temporary or intermediate table:
CREATE TABLE acc_coa_tmp AS SELECT * FROM acc_coa WHERE 1 = 0;
Note: No primary key on new table acc_coa_tmp.
In PHP:
try {
:
$db->insert(...);
} catch (SqlException $ex) {
// Failed insert CSV
// SQL insert into new table acc_coa_tmp
$db->insert(...);
}

Related

Mysql server gone away simple insert query

I am trying to upload video to S3 and then save it to DB, small videos are working fine they are getting saved and insterted to DB successfully but when it comes to large videos say over 10MB (which is not that large) are uploading to S3 but not saving to DB. Please suggest what is going on here. Because everything looks fine and why I'm getting MySQL server has gone away error for such simple query.
ERROR - 2021-05-16 06:46:21 --> Query error: MySQL server has gone away - Invalid query: INSERT INTO `pub_upload_list` (`publisher_id`, `standard_list_id`, `subject_id`, `chapter_id`, `file_name`, `file_short_description`, `file_size`, `file_type`, `file_submission_date`, `file_attributes`, `browse_link`, `file_owner`, `upload_date_time`, `upload_by`) VALUES (1, 1, 2, 14, '01Gan001_004.mp4', 'test 12212', 19, '8', NULL, 1, 'browse', 57, '2021-05-16 06:46:21', 0)
EDIT
Here is complete table structure -
CREATE TABLE `pub_upload_list` (
`upload_id` int(11) NOT NULL AUTO_INCREMENT,
`publisher_id` int(11) NOT NULL,
`standard_list_id` int(6) NOT NULL,
`subject_id` int(11) NOT NULL,
`chapter_id` int(11) NOT NULL,
`file_name` text COLLATE utf8mb4_unicode_ci NOT NULL,
`file_short_description` text COLLATE utf8mb4_unicode_ci NOT NULL,
`file_size` int(11) NOT NULL,
`file_type` text COLLATE utf8mb4_unicode_ci NOT NULL,
`file_submission_date` date DEFAULT NULL,
`file_attributes` int(1) NOT NULL COMMENT '0 = secured, 1 = unsecured',
`browse_link` enum('browse','link') COLLATE utf8mb4_unicode_ci NOT NULL,
`file_owner` int(11) NOT NULL,
`upload_date_time` datetime NOT NULL,
`upload_by` int(11) NOT NULL,
PRIMARY KEY (`upload_id`),
KEY `file_owner` (`file_owner`),
KEY `publisher_id` (`publisher_id`),
CONSTRAINT `pub_upload_list_ibfk_1` FOREIGN KEY (`publisher_id`) REFERENCES `publishers` (`publisher_id`)
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Issue with execution query of update table in stored trigger

I'm trying to execute this auto generate query from PHP but it's not working
INSERT INTO mvt (id_piece, article_id_article, code_mvt, origine_mvt, type_mvt, code_art, des_art, qte_old, qte_mvt, qte_new, link_piece)
SELECT
32,
57,
'MVT/15/12/0001',
'BSR/15/12/001',
'S',
'ART_039',
'AAAA',
a.qte_art,
1,
a.qte_art - 1,
'uuuu'
FROM article a
WHERE a.id_article = 57
Structure of table mvt
CREATE TABLE IF NOT EXISTS `mvt` (
`id_mvt` int(11) NOT NULL AUTO_INCREMENT,
`id_piece` int(10) NOT NULL,
`code_mvt` varchar(20) NOT NULL,
`origine_mvt` varchar(100) NOT NULL,
`date_mvt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`type_mvt` varchar(20) NOT NULL,
`article_id_article` int(10) unsigned NOT NULL,
`code_art` varchar(20) NOT NULL,
`des_art` varchar(255) NOT NULL,
`qte_old` double NOT NULL,
`qte_mvt` double NOT NULL,
`qte_new` double NOT NULL,
`link_piece` varchar(255) NOT NULL,
PRIMARY KEY (`id_mvt`),
KEY `article_id_article` (`article_id_article`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=229 ;
Trigger of mvt
DROP TRIGGER IF EXISTS `before_insert_mvt`;
DELIMITER //
CREATE TRIGGER `before_insert_mvt` BEFORE INSERT ON `mvt`
FOR EACH ROW BEGIN
UPDATE article SET qte_art = NEW.qte_new WHERE id_article = NEW.article_id_article;
END
Structure of table article
CREATE TABLE IF NOT EXISTS `article` (
`id_article` int(10) unsigned NOT NULL AUTO_INCREMENT,
`famille_id_famille` int(10) unsigned NOT NULL,
`marque_id_marque` int(10) unsigned NOT NULL,
`tva_id_tva` int(10) unsigned NOT NULL,
`code_art` varchar(20) DEFAULT NULL,
`des_art` varchar(255) DEFAULT NULL,
`paht_art` double DEFAULT NULL,
`pamp_art` double DEFAULT NULL,
`qte_art` double DEFAULT NULL,
`seuil_alert` double NOT NULL,
`seuil_reap` double NOT NULL,
`date_modif` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`etat` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id_article`),
KEY `article_FKIndex1` (`tva_id_tva`),
KEY `article_FKIndex2` (`marque_id_marque`),
KEY `article_FKIndex3` (`famille_id_famille`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=82 ;
And this is sql error
#1442 - Can't update table 'article' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
it doesn't work!
What am I missing ?
The trigger is trying to update the same table that you're getting the inserted data from, and that's not allowed. You can do it in two steps with a temporary table.
CREATE TEMPORARY TABLE temp_mvt AS
SELECT
32,
57,
'MVT/15/12/0001',
'BSR/15/12/001',
'S',
'ART_039',
'AAAA',
a.qte_art,
1,
a.qte_art - 1,
'uuuu'
FROM article a
WHERE a.id_article = 57;
INSERT INTO mvt SELECT * FROM temp_mvt;
DROP TABLE temp_mvt;

PDO update query runs changes no rows. No errors

So I'm running a PDO update working, and for some reason it won't update the table...
$business_id = 9874128;
$hidden = 1;
$query = "UPDATE business_property_overrides SET hidden=? WHERE business_id=?";
try {
$stmt = $pdo->prepare($query);
$stmt->execute(array($business_id, $hidden));
}
For some reason this won't update, even though I get no errors. The existing tables schema looks like this, and the data is:
There is an existing data set with business_id = 9874128 and hidden set to 0, but it won't update when I run the above code.
CREATE TABLE `business_property_overrides` (
`business_id` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(512) NOT NULL,
`apt_type` varchar(25) DEFAULT NULL,
`apt_num` varchar(9) DEFAULT NULL,
`street_address` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
`state` varchar(255) DEFAULT NULL,
`zip` varchar(25) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`url` varchar(512) DEFAULT NULL,
`hours` varchar(100) DEFAULT NULL,
`openhours` varchar(100) DEFAULT NULL,
`location` point DEFAULT NULL,
`yelp` varchar(512) DEFAULT '0',
`twitter` varchar(512) DEFAULT '0',
`hidden` tinyint(1) DEFAULT '0',
`merged` int(11) DEFAULT NULL,
`closed` tinyint(1) DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `business_id` (`business_id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9874134 DEFAULT CHARSET=utf8;
The hidden is TINYINT 1 characters long, you are assigning it business_id which is 7 characters long, that is the error.
Change
$stmt->execute(array($business_id, $hidden));
To:
$stmt->execute(array($hidden,$business_id))
As I've already commented over here, or you can simply use the placeholders of taking no care about the occurence like as
$query = "UPDATE business_property_overrides SET hidden = :hidden WHERE business_id = :business_id";
try {
$stmt = $pdo->prepare($query);
$stmt->execute(array(":business_id" => $business_id, ":hidden" => $hidden));
}

NULL entry at id field in Phpmyadmin

I exported the sql from one phpmyadmin db and imported same at another phpmyadmin db.
The create table statement is as follows
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) DEFAULT NULL,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The insert sql file also I have exported it as follows
INSERT INTO `h_stats` (`Id`, `Category`, `Success`, `Failure`, `ThCount`, `OCount`, `HDate`, `Count`) VALUES
(13, 'Hits', 31303, 8828, 8893, 30372, '2015-04-07', 40151),
Note :
When I try to insert new rows at the place of id NULL value is coming.
Can anyone suggest me what to change in create table statement.
You need to define the id as primary key and auto-increment .
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
So when you add null for id, the auto-increment will come in to play and set it the next value in insert.
Note that this will enforce you to have unique value for id so while doing insert leave id into the insert statement. Something as
insert into h_stats (`Category`,`Success` ...) values ( ....);

Error fetching row from database with PHP

I am trying to fetch a row from a table in my database, and everything is retrieved successfully except for one column (page_content), whose data comes in partially. Here is the table creation code:
CREATE TABLE 'usol_site_page' (
'page_id' int(11) NOT NULL auto_increment,
'page_type_id' int(11) NOT NULL,
'page_menu_id' int(11) NOT NULL,
'page_name' varchar(100) NOT NULL,
'page_link' varchar(100) NOT NULL,
'page_heading' varchar(255) default '',
'page_content' text,
'page_title' varchar(255) default NULL,
'meta_keywords' mediumtext,
'keyword_description' text,
'pic_small' varchar(255) default NULL,
'pic_main' varchar(255) default NULL,
'pic_size' varchar(50) default NULL,
'pic_type' varchar(50) default NULL,
'display_order' int(11) NOT NULL,
'parent_page_id' int(11) NOT NULL,
'status' varchar(20) NOT NULL,
'creation_date' date NOT NULL,
'last_update_date' date NOT NULL,
PRIMARY KEY ('page_id'),
KEY 'Refusol_page_menu60' ('page_menu_id'),
KEY 'Refusol_page_type44' ('page_type_id'),
CONSTRAINT 'Refusol_page_menu60' FOREIGN KEY ('page_menu_id') REFERENCES 'usol_page_menu' ('page_menu_id') ON DELETE CASCADE,
CONSTRAINT 'Refusol_page_type44' FOREIGN KEY ('page_type_id') REFERENCES 'usol_page_type' ('page_type_id') ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=67 DEFAULT CHARSET=latin1
Does anyone know why page_content is not being fully retrieved?
here is the code the query :
$extendQry = "";
if($mode != "admin"){
$extendQry = "AND p.status='enable' ";
}
$qry = "SELECT p.page_id, p.page_link, p.page_name, p.page_heading, p.page_content, p.status, p.page_title,
p.meta_keywords, p.keyword_description, p.pic_main, p.pic_small, p.parent_page_id,
t.page_type_id, t.page_type, m.page_menu_id, m.menu_type
FROM usol_site_page p, usol_page_type t , usol_page_menu m
WHERE p.page_id = ".$pageId." $extendQry
AND t.page_type_id = p.page_type_id
AND m.page_menu_id = p.page_menu_id";
$result = mysql_query($qry);
return mysql_fetch_row($result);
fellas thank you very much for your quick responce sorry for my very unclear question, i got it solved by changing datatype of the column (page_content) from 'text' (64KB) to mediumtext(16MB). and it is working perfectly now. thank you all for your kind support you guyz are the best.

Categories