Wednesday, September 7, 2022

Mencegah Query Invalid Masuk ke MySQL Dengan Trigger

Cara mencegah query invalid masuk ke database, Anda dapat mencoba trigger berikut ini.


DELIMITER $$

CREATE
   
    TRIGGER `sms`.`hapusinvalid` BEFORE INSERT ON `sms`.`outbox`
    FOR EACH ROW BEGIN
		IF NEW.wa_file != '' AND (NEW.wa_media = '' OR ISNULL(NEW.wa_media)) THEN BEGIN
			SET NEW.wa_mode = 0;
			SET NEW.wa_file = NULL;
			SET NEW.wa_media = NULL;
			-- DELETE FROM jbssms.outbox WHERE ID = NEW.id;
			-- SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Ada pesan Invalid!';
		END; END IF;
    END$$

DELIMITER ;

Dengan trigger diatas, maka setiap ditemukan field wa_file terisi dan field wa_media ditemukan kosong, maka trigger akan menset nilai baru berupa nilai NULL.

How to Increase MySQL/MariaDB Connections (max_connections)

Post a Comment