Jaguar83 Posted February 6, 2010 Share Posted February 6, 2010 Hi, Thanks for taking the time to read my question. I'm writing a relatively simple (I hope!!!) trigger to update a field in a row in the table 'coinpack' (below) when the table 'pad' (below) gets a new row inserted. Basically I want the inserted pad.pad_70 to be added to the existing coinpack.coinpack_70 value (when coinpack.coinpack_id=pad.coinpack_id). I have no initial value in for coinpack.coinpack_70 (value = NULL?) so this may be the problem. The tables are: coinpack: +------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+----------------+ | coinpack_id | int(10) | NO | PRI | NULL | auto_increment | | order_id | int(11) | NO | MUL | NULL | | | coin_cdn | varchar(15) | NO | MUL | NULL | | | coinpack_qty | int(11) | NO | | NULL | | | coinpack_service | varchar(30) | YES | MUL | NULL | | | coinpack_comm | varchar(250) | YES | | NULL | | | coinpack_70 | int(11) | YES | | NULL | | | coinpack_69 | int(11) | YES | | NULL | | | coinpack_68 | int(11) | YES | | NULL | | | coinpack_67 | int(11) | YES | | NULL | | | coinpack_66 | int(11) | YES | | NULL | | | coinpack_65 | int(11) | YES | | NULL | | | coinpack_64 | int(11) | YES | | NULL | | | coinpack_63 | int(11) | YES | | NULL | | | coinpack_rejects | int(11) | YES | | NULL | | +------------------+--------------+------+-----+---------+----------------+ pad: +-------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+----------------+ | pad_id | int(11) | NO | PRI | NULL | auto_increment | | coinpack_id | int(11) | NO | MUL | NULL | | | pad_qty | int(11) | YES | | NULL | | | pad_70 | int(11) | YES | | NULL | | | pad_69 | int(11) | YES | | NULL | | | pad_68 | int(11) | YES | | NULL | | | pad_67 | int(11) | YES | | NULL | | | pad_66 | int(11) | YES | | NULL | | | pad_65 | int(11) | YES | | NULL | | | pad_64 | int(11) | YES | | NULL | | | pad_63 | int(11) | YES | | NULL | | | pad_rejects | int(11) | YES | | NULL | | | pad_grader | int(11) | YES | MUL | NULL | | | pad_edition | varchar(20) | YES | | NULL | | +-------------+-------------+------+-----+---------+----------------+ My trigger below seems a bit long winded...I went with the 'add everything to see if it works' approach. Trigger: CREATE TRIGGER update_grades BEFORE INSERT ON pad FOR EACH ROW BEGIN DECLARE old_70 INTEGER; SELECT coinpack_70 INTO old_70 FROM coinpack WHERE coinpack.coinpack_id=NEW.pad_70; IF old_70 = "" THEN IF NEW.pad_70 > 0 THEN UPDATE coinpack SET coinpack_70=NEW.pad_70 WHERE coinpack.coinpack_id=NEW.coinpack_id; END IF; ELSE IF NEW.pad_70 > 0 THEN UPDATE coinpack SET coinpack_70=old_70+NEW.pad_70 WHERE coinpack.coinpack_id=NEW.coinpack_id; END IF; END IF; END MySQL accepts the trigger fine but when I insert a new row into pad, coinpack.coinpack_70 does not get updated (continues read NULL). Can anyone spot my error? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/191120-first-trigger-problems/ Share on other sites More sharing options...
jskywalker Posted February 7, 2010 Share Posted February 7, 2010 SELECT coinpack_70 INTO old_70 FROM coinpack WHERE coinpack.coinpack_id=NEW.pad_70; could give more than 1 result, because the index is not unique? or..... Quote Link to comment https://forums.phpfreaks.com/topic/191120-first-trigger-problems/#findComment-1008453 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.