MySQL Trigger that writes to multiples tables - php

I have a trigger that I cannot get to work properly.
I have at table called _WorkOrderDump where data is input via a form. There is a trigger on this table that inserts some of the data into a table called _RefTable. The second part of this trigger is to insert some of the data into another table called __XClient.
I cannot get the second insert into the __XClient table to work, it does not insert any data into this table. The first part of the trigger inserting into Ref table is however working perfectly.
What is wrong with my query?
Thanks for any help provided!
CREATE TRIGGER WorkOrderTrigger
AFTER INSERT on _WorkOrderDump
BEGIN
Declare EntryDate DATETIME;
Declare RefIDDump INT;
SET EntryDate = CURDATE();
Set RefIDDump = LAST_INSERT_ID();
CASE WHEN (EntryDate) > 0
THEN INSERT INTO `_RefTable`( `WOID`, `ClientID`, `ProjectScope`, `ProjectID`, `ClientRef`, `SiteID`, `SiteName`, `RegionID`, `SiteAddressLine`, `SiteAddressSuburb`, `SiteAddressState`, `SiteAddressPostcode`, `SiteAddressCountry`, `SiteContactName`, `SiteContactMobile`, `SiteContactPhone`, `SiteContactEmail`, `RefEntryDate`, `CreatedBY`)
VALUES
( new.WOID, new.ClientID, new.ProjectScope, new.ProjectID, new.ClientRef, new.SiteID, new.SiteName, new.RegionID, new.SiteAddressLine, new.SiteAddressSuburb, new.SiteAddressState, new.SiteAddressPostcode, new.SiteAddressCountry, new.SiteContactName, new.SiteContactMobile, new.SiteContactPhone, new.SiteContactEmail, EntryDate, new.CreatedBY);
WHEN (new.ClientID) = 1
THEN INSERT INTO `__XClient`( `RefID`, `ITContactName`, `ITContactPhone`, `ITContactEmail`, `XScope32`, `XScope43`, `XScopeMediaPlayer`, `XScopeInstallDate`, `XScopeInstallTime`, `XScopeComments`)
VALUES
( RefIDDump, new.Dump_1, new.Dump_3, new.Dump_2, new.Dump_4, new.Dump_5, new.Dump_6, new.Dump_7, new.Dump_8, new.Dump_9);
END CASE;
END

Related

Adding dynamical options to PHP populated Dropdown from SQLServer Database

need some input
i know how to populate a php dropdown list from MSSQLServer Datebase and save the selected value in the datebase, but i want to add new dynamical entrys in the dropdown menu and save them into the database. Here is the code:
some help?
I would recommend doing all this in the SQL level, try this
it is modified from a stored procedure I use in a similar fashion
Create Procedure [dbo].[MergeStatus](
#StatusID int,
#Status Varchar(8000)
)
AS
BEGIN
set nocount on;
Merge [Master.dbo.Status] as Target
using (select #StatusID As ID) As source
On
(Target.StatusID = source.ID)
When Matched Then
INSERT INTO Master.dbo.AllgemeineAngaben (StatusID) VALUES (#StatusID ) -- we have a match ID is Greater than 0 so insert into new tabke
When Not Matched Then
Insert (
#Status -- no match add new to Master.dbo.Status
)
Values(
#Status
);
IF #StatusID IS NULL or #StatusID = 0 -- our ID is not waht we expected so we need to insert
BEGIN
SET #Id = CAST(SCOPE_IDENTITY() as [int]);--getting ID od record we just inserted
INSERT INTO Master.dbo.AllgemeineAngaben (StatusID) VALUES (#StatusID )
END
END

MySql insert Trigger Not working

I have written a before insert trigger that will not insert the data in the tables if the 'ratings' > 5 and inserts it on a table 'rating_check' with a message .For 'ratings' <= 5 the data is inserted on all the tables.But its not working nor am I getting any errors. Here is the trigger code-
DELIMITER $$
CREATE
TRIGGER `mytrigger` BEFORE INSERT
ON `review`
FOR EACH ROW BEGIN
IF NEW.ratings > 5 THEN
SET #trigger_desc = 'You cannot rate greater than 5!';
INSERT INTO `rating_check` (`trig_id`, `trigger_desc`) VALUES ('', #trigger_desc);
ELSE
INSERT INTO `developer_details` (`developer_id`,`developer_name`,`developer_email`) VALUES ('$appDevId','$appDevName','$appDevEmail');
INSERT INTO `apps` (`app_id`,`app_name`,`app_icon`,`dev_id`) VALUES (LAST_INSERT_ID(),'$appName','$appIcon','$appDevId');
INSERT INTO `app_details` (`app_id`,`description`,`version`,`release_date`,`size`) VALUES (LAST_INSERT_ID(),'$appDesc','$appVersion','$appDate','$appSize');
INSERT INTO `app_specs` (`app_id`,`downloads`,`category_name`) VALUES (LAST_INSERT_ID(),'$appDownloads','$appCategory');
INSERT INTO `review` (`app_id`,`ratings`) VALUES (LAST_INSERT_ID(),'$appRatings');
END IF;
END$$
'trig_id' is auto incremented here.Please help me with this!

trigger after insert on table 1, insert on table 2 with where condition column from table 1

My trigger is not working at all.
DELIMITER $$
CREATE TRIGGER leave_credit_nt
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO nonteaching_leavecredit (IdNum, Date_Hired, Emp_Stat, Date_Leave_Ac, Vac_Leave, Sick_Leave)
VALUES (new.IdNum, new.Date_Hired, new.Emp_Stat, new.Date_Hired + INTERVAL 1 YEAR, 0, 0)
WHERE employees.Emp_Type = 'NON-TEACHING';
END $$
DELIMITER
I want that the trigger will insert values into my second table only those with employees.emp_type = 'NON-TEACHING'. Can someone help me get this trigger working?
emp_type is a column in my table employees.
Your INSERT query is wrong. MySQL INSERT Syntax does not support the WHERE clause so your query as it stands will fail.
You cant use WHERE condition with INSERT query. If you are using WHERE condition, it means you already have that row in the table. So , you should use UPDATE query instead of INSERT.
You can add conditional logic to your trigger with:
if new.Emp_Type = 'NON-TEACHING' then
INSERT INTO nonteaching_leavecredit (IdNum, Date_Hired, Emp_Stat, Date_Leave_Ac, Vac_Leave, Sick_Leave)
VALUES (new.IdNum, new.Date_Hired, new.Emp_Stat, new.Date_Hired + INTERVAL 1 YEAR, 0, 0);
end if;

calling mysql procedure using codeigniter [Error: result consisted more than one row]

I have table status and status_stage . After insert in status table, I want to add it to the status_stage table. I have this procedure:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `addStatus`(status VARCHAR(45), stage int (8))
BEGIN
DECLARE status_id INT DEFAULT NULL;
INSERT INTO status (status)
VALUES (status);
SELECT id INTO status_id FROM status WHERE status = status;
INSERT INTO stage_status (stage,status)
VALUES (stage,id);
END
And I call that function with this:
$result= $this->db->query("call addStatus('$status', $stage)");
return ($result->num_rows()>0);
When I tried it it gives me this Error:
Result consisted of more than one row
Your SELECT ... INTO statement got more than one result because of your WHERE clause with status = status, this equals forever
you need to change the param name to distinguish it from the table column name (eg. status_param)

How to Parse PHP Searilize data in Mysql Stored Procedure & Looping it

I have below data in Mysql Column (storing all data in serialize form - with comma separated into column) and i want to get/fetch this column data in Mysql stored procedure and want to loop for each data and insert into another trans table.
So if my data like below then i want to insert 7 record in trans table.
{"FormBuilderId":"5","vAnswer":"Develeop"},
{"FormBuilderId":"15","vAnswer":"Search Engine"},
{"FormBuilderId":"13","vAnswer":"10-15"},
{"FormBuilderId":"6","vAnswer":"Tester entered"},
{"FormBuilderId":"1","vAnswer":"Female"},
{"FormBuilderId":"14","vAnswer":"Moon.jpg"},
{"FormBuilderId":"12","vAnswer":"TV,dancing and modeling"}
My table structure & data is like below in table:
CREATE TABLE IF NOT EXISTS `idea_history` (
`iIdeaHistoryId` int(11) NOT NULL AUTO_INCREMENT,
`iApplicationId` tinyint(11) unsigned DEFAULT '0',
`tAnswerData` text COMMENT 'all answer data store here in json format',
`dAddedDate` datetime NOT NULL,
PRIMARY KEY (`iIdeaHistoryId`),
KEY `iApplicationId` (`iApplicationId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='history comes here' AUTO_INCREMENT=57 ;
--
-- Dumping data for table `idea_history`
--
INSERT INTO idea_history (
iIdeaHistoryId,
iApplicationId,
tAnswerData,
dAddedDate
)
VALUES
(
53,
2,
'{"FormBuilderId" : "2","vAnswer":"Environmental Group"},{"FormBuilderId" : "11","vAnswer":"Satelite"},{"FormBuilderId" : "3","vAnswer":"HB"},{"FormBuilderId" : "4","vAnswer":"Dev"},{"FormBuilderId" : "7","vAnswer":"HB"},{"FormBuilderId" : "8","vAnswer":"Balaji Satellite"},{"FormBuilderId" : "10","vAnswer":""}',
'2014-07-05 19:20:56'
),
(
54,
1,
'{"FormBuilderId":"5","vAnswer":"Hello krishna|kanth double"},{"FormBuilderId":"15","vAnswer":"Website"},{"FormBuilderId":"6","vAnswer":"need to check"},{"FormBuilderId":"13","vAnswer":"20-25"}',
'2014-07-05 19:20:56'
),
(
55,
2,
'{"FormBuilderId":"11","vAnswer":"comapnay"},{"FormBuilderId":"8","vAnswer":"here am|chw "},{"FormBuilderId" : "10","vAnswer":""},{"FormBuilderId":"9","vAnswer":"Business"}',
'2014-07-05 19:20:56'
) ;
I will pass iIdeaHistoryId in stored procedure and it will fetch value of tAnswerData field and part this value and insert into another trans table.
Could you please guide what i have to change in stored procedure ?
you can do it like this
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'{"',-1),'":"',1) INTO arg_1;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-2),'","',1) INTO arg_2;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'","',-1),'":"',1) INTO arg_3;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-1),'"}',1) INTO arg_4;
INSERT INTO trans VALUES (arg_1,arg_2,arg_3,arg_4);
or if you want to do it in store procedure add your data here
DELIMITER $$
DROP PROCEDURE IF EXISTS trans$$
CREATE PROCEDURE trans()
BEGIN
DECLARE arg_1,arg_2,arg_3,arg_4 VARCHAR(100);
DECLARE finished INTEGER DEFAULT 0;
DECLARE cursor_name CURSOR FOR YOUR_SELECT_statement;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN cursor_name;
my_loop: LOOP
FETCH cursor_name INTO your_variable;
IF finished = 1 THEN
LEAVE my_loop;
END IF;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'{"',-1),'":"',1)
INTO arg_1 FROM your_table;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-2),'","',1)
INTO arg_2 FROM your_table;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'","',-1),'":"',1)
INTO arg_3 FROM your_table;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-1),'"}',1)
INTO arg_4 FROM your_table;
INSERT INTO trans VALUES (arg_1,arg_2,arg_3,arg_4);
END LOOP my_loop;
CLOSE cursor_name;
END$$
DELIMITER ;
for one column you use this
DELIMITER $$
DROP PROCEDURE IF EXISTS trans$$
CREATE PROCEDURE trans(IN in_iIdeaHistoryId INT)
BEGIN
DECLARE arg_1 VARCHAR(100);
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(`tAnswerData`,' ',''),'{"',-1),'":"',1)
INTO arg_1 FROM `idea_history` WHERE iIdeaHistoryId=in_iIdeaHistoryId;
INSERT INTO trans VALUES (arg_1);
END$$
DELIMITER ;
and call it like this
CALL `trans`(53);
This looks like JSON. Use php function json_decode() to store the json data into an array and then you can use foreach loop on that array. Inside the loop you can place condition and when its met, you could use insert query to store it in your database.
Does this help you?
If you can explain a bit further what you're trying to do exactly and what are your condition, then i Can probably give you a better answer.

Categories