我在PhpMyadmin中有一个表,它有一列可以获取url值。我不想从PHP代码/前端进行验证。如何使用触发器/程序实现此目的?我试过跟踪触发器但得到错误:“#1241操作数应包含3列”
CREATE TRIGGER `validate_url_after_insert` BEFORE INSERT ON `table_name`
FOR EACH ROW BEGIN
DECLARE str1 varchar(10);
DECLARE str2 varchar(10);
SET str1='http://';
SET str2='https://';
if(substring_index(substring_index(substring_index(REPLACE(new.url,str2,str1), '/',3),str1,-1),'.',-1) != 'com',substring_index(substring_index(substring_index(REPLACE(new.url,str2,str1), '/',3),str1,-1),'.',-3),substring_index(substring_index(substring_index(REPLACE(new.url,str2,str1), '/',3),str1,-1),'.',-2)) != '' then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Can not insert';
END IF;
END
答案 0 :(得分:3)
好的,我自己得到了答案。
CREATE TRIGGER `validate_work_url` BEFORE INSERT ON `table_name`
FOR EACH ROW BEGIN
IF new.url REGEXP "^(https?://|www.)[.A-Za-z0-9-]+.[a-zA-Z]{2,4}" THEN
SET new.url = new.url;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = "INCORRECT URL" ;
END IF;
END