Mysql server gone away simple insert query - php

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

Related

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

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(...);
}

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;

mysql unique for multiple columns

When payment happen, sometimes its captured double entry in table.
I want to ignore double entry capture so i want to insert records when these created, user_id, amount fields should be unique.
How do i make it ? Below is my table.
CREATE TABLE `transactions` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`user_id` int(20) NOT NULL,
`project_id` int(20) DEFAULT NULL,
`foreign_id` int(20) NOT NULL,
`class` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`transaction_type_id` int(20) DEFAULT NULL,
`amount` float(10,2) NOT NULL,
`description` text COLLATE utf8_unicode_ci,
`payment_gateway_id` int(20) DEFAULT NULL,
`gateway_fees` float(10,2) NOT NULL,
`is_old` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=266 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
To strictly answer your question, you create a unique composite key on the combination of those 3 columns. That way no two rows can exist with a combination of the 3 of them in the composite index.
CREATE TABLE `transactions2` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`user_id` int(20) NOT NULL,
`project_id` int(20) DEFAULT NULL,
`foreign_id` int(20) NOT NULL,
`class` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`transaction_type_id` int(20) DEFAULT NULL,
`amount` float(10,2) NOT NULL,
`description` text COLLATE utf8_unicode_ci,
`payment_gateway_id` int(20) DEFAULT NULL,
`gateway_fees` float(10,2) NOT NULL,
`is_old` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
unique key(created,user_id,amount) -- <------------------- right here
);
insert some data to test it:
insert transactions2 (created,modified,user_id,project_id,foreign_id,class,
transaction_type_id,amount,description,payment_gateway_id,gateway_fees,is_old) values
('2009-01-01 12:00:00','2009-01-01 12:00:00',666,1,1,'a',1,100,'desc',1,12,1);
-- inserts fine (above)
Try it again with exactly the same data:
insert transactions2 (created,modified,user_id,project_id,foreign_id,class,
transaction_type_id,amount,description,payment_gateway_id,gateway_fees,is_old) values
('2009-01-01 12:00:00','2009-01-01 12:00:00',666,1,1,'a',1,100,'desc',1,12,1);
-- error 1062: Duplicate entry
-- change it barely:
insert transactions2 (created,modified,user_id,project_id,foreign_id,class,
transaction_type_id,amount,description,payment_gateway_id,gateway_fees,is_old) values
('2009-01-01 13:00:00','2009-01-01 12:00:00',666,1,1,'a',1,100,'desc',1,12,1);
-- inserts fine
Also, use ENGINE=INNODB. Read about that Here.
Please read Mysql multi column indexes a.k.a. composite indexes.
Lastly, the concept of what you are talking about is not far off from Insert on Duplicate Key Update. Just throwing that reference out there for you.
You can achieve the same using ,handling at the time of inserting,
You can try WHERE NOT EXISTS with INSERT.
Something like this.(You need to specify column name who has NOT NULL constraint,i missed all those columns)
INSERT INTO table_name(`created`,`user_id`,`amount`) VALUES =
'$created','$user_id','$amount'
WHERE NOT EXISTS
(SELECT *
FROM table_name
WHERE created ='$created' AND user_id='$user_id' AND amount='$amount')
Hope this helps.

MySQL Stored Procedure [Copy table 1 -> table 2]

My knowledge of MySQL/ SQL apart from the simple CRUD operations is basic.
If I had to use a stored procedure to move certain attributes (not in a specific order) to another table how could it be done?
These are the following tables. I want to move from the 1st to the 2nd table.
As you can see the datatype sizes are different for certain columns.
CREATE TABLE IF NOT EXISTS `source_cdr` (
`callstart` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`src` varchar(80) NOT NULL DEFAULT '',
`dst` varchar(80) NOT NULL DEFAULT '',
`accountcode` varchar(50) NOT NULL,
`uniqueid` varchar(100) NOT NULL,
`ID` int(11) NOT NULL AUTO_INCREMENT,
`callanswer` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`callend` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`disposition` varchar(50) NOT NULL,
`cdr_id` int(11) unsigned NOT NULL DEFAULT '0',
`pin_code` varchar(4) NOT NULL,
`provider` int(11) NOT NULL,
PRIMARY KEY (`ID`),
KEY `calldate_idx` (`callstart`) USING BTREE,
KEY `idx_acc_code_calldate` (`accountcode`,`callstart`) USING BTREE,
KEY `uniqueid` (`uniqueid`),
KEY `cdr_id` (`cdr_id`),
KEY `idx_uniqueid_cdr_id` (`uniqueid`,`cdr_id`)
) ENGINE=MyISAM;
--
CREATE TABLE IF NOT EXISTS `destination_cdr` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`calldate` datetime NOT NULL,
`source` varchar(80) NOT NULL,
`destination` varchar(80) NOT NULL,
`account_code` varchar(30) DEFAULT NULL,
`pincode` varchar(45) NOT NULL,
`duration_call` bigint(20) NOT NULL DEFAULT '0',
`duration_talk` bigint(20) NOT NULL,
`disposition` varchar(255) NOT NULL,
`clid` varchar(80) DEFAULT NULL,
`cdr_id` bigint(20) DEFAULT NULL,
`vxcdr_id` bigint(20) DEFAULT NULL,
`provider` int(11) NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
EDIT 1
An example of a row
('2012-03-18 20:54:49', '5796', '0761100866', '103f0124ad510516f33cab132c0a695b', 'call-F1884808-6753-2F10-181C-3A#10.217.164.33', 308006367, '2012-03-18 20:55:05', '2012-03-18 20:55:51', '200 OK', 2, '', 0),
Thanks
You can use MySQL: INSERT ... SELECT Syntax to copy data from one table to the other.
Decide common fields in both and copy the same.
Example:
INSERT INTO TABLE2( COL1, COLx, ... ) SELECT colM, colY FROM TABLE1;
If the column sizes mismatch, data truncation takes place, and you can't overcome that but redefine the destination table columns.

Query exported from phpmyadmin does not work using mysql_query() in PHP

I have a very strange problem here. I am exporting a query through phpmyadmin and then when I run it using mysql_query I get an error saying that syntax of the query isn't correct.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `wp_cfs_events` ( `event_id` int(11) NOT NULL AUTO_' at line 14
The same query runs perfectly fine using phpmyadmin. What could be the problem?
Here is the SQL query
CREATE TABLE IF NOT EXISTS `wp_cfs_certificates` (
`certificate_id` varchar(8) DEFAULT NULL,
`parent_name` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`emergency_number` varchar(15) NOT NULL,
`email` varchar(45) NOT NULL,
`certificate_price` enum('40','50') NOT NULL,
`certificate_order_time` datetime DEFAULT NULL,
`is_used` tinyint(1) DEFAULT '0',
UNIQUE KEY `certificate_id_UNIQUE` (`certificate_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `wp_cfs_events` (
`event_id` int(11) NOT NULL AUTO_INCREMENT,
`event_name` varchar(60) DEFAULT NULL,
`event_desc` varchar(2048) DEFAULT NULL,
`event_date` date DEFAULT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `wp_cfs_events` (`event_id`, `event_name`, `event_desc`, `event_date`) VALUES
(1, 'Culture Night', 'all about culture', '2013-05-16'),
(2, 'Sports Night', 'all about sports', '2013-05-31'),
(3, 'Random Night', 'randomness overloaded', '2013-06-15'),
(4, 'Winter Fest', 'the awesome winter fest', '2013-11-20'),
(5, 'Archived Event', 'this event has been occured in past', '2013-04-02');
CREATE TABLE IF NOT EXISTS `wp_cfs_parents` (
`parent_id` int(11) NOT NULL,
`parent_name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `wp_cfs_registrations` (
`registration_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`certificate_id` varchar(8) NOT NULL,
`event_id` varchar(45) DEFAULT NULL,
`parent_name` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`emergency_number` varchar(15) NOT NULL,
`email` varchar(45) NOT NULL,
`child_name` varchar(45) DEFAULT NULL,
`child_age` int(11) DEFAULT NULL,
PRIMARY KEY (`registration_id`),
UNIQUE KEY `certificate_id_UNIQUE` (`certificate_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `wp_cfs_registrations`
ADD CONSTRAINT `certificate_id` FOREIGN KEY (`certificate_id`) REFERENCES `wp_cfs_certificates` (`certificate_id`) ON DELETE NO ACTION ON UPDATE CASCADE;
Its because mysql_query() doesn't support multiple queries
Its mentioned in the official PHP documentation
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

Categories