MySQL: Syntax error when creating trigger - php

Why my sql syntax is throwing an error?
CREATE TRIGGER info
AFTER INSERT ON inbox
FOR EACH ROW BEGIN if SUBSTRING(new.TextDecoded,1,6)='telkom' then
INSERT INTO outbox ( DestinationNumber, Coding, TextDecoded, CreatorID )VALUES ( new.SenderNumber, 'Default_No_Compression', (SELECT nama from data_dosen WHERE kode = SUBSTRING(new.TextDecoded,8,10)), '1');
else
INSERT INTO outbox (DestinationNumber, Coding, TextDecoded, CreatorID) VALUES (new.SenderNumber, 'Default_No_Compression', 'Maaf format sms Anda salah. Ketik telkom<spasi>kode dosen', '1');
end if;
END$$
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 '' at line 4

You don't have delimiter definition before your trigger. Delimiter informs MySQL which characters will end up your trigger. Without it, MySQL assumes semicolon as default delimiter and fails upon first semicolon in your code. Check out this:
DELIMITER $$
CREATE TRIGGER info
AFTER INSERT ON inbox
FOR EACH ROW BEGIN if SUBSTRING(new.TextDecoded,1,6)='telkom' then
INSERT INTO outbox ( DestinationNumber, Coding, TextDecoded, CreatorID )VALUES ( new.SenderNumber, 'Default_No_Compression', (SELECT nama from data_dosen WHERE kode = SUBSTRING(new.TextDecoded,8,10)), '1');
else
INSERT INTO outbox (DestinationNumber, Coding, TextDecoded, CreatorID) VALUES (new.SenderNumber, 'Default_No_Compression', 'Maaf format sms Anda salah. Ketik telkom<spasi>kode dosen', '1');
end if;
END$$
DELIMITER ;

Related

MySQL basic trigger - not working

I'm trying to create following trigger:
CREATE TRIGGER clean
AFTER INSERT ON `mecze_druzyny`
begin
DELETE * FROM bramki;
END;
DELIMITER ;
Which should be ran just after following:
$sql2 = "INSERT INTO mecze_druzyny (id_druzyny, id_meczu, gospodarz) VALUES (
:team2,
:lastmatch,
:gospodarz)";
$stmt = $db->prepare($sql2);
$stmt->bindParam(':team2', $id2, PDO::PARAM_INT);
$stmt->bindParam(':lastmatch', $last_match, PDO::PARAM_INT);
$stmt->bindParam(':gospodarz', $a=0, PDO::PARAM_STR);
$stmt->execute();
Unfortunately I'm getting an error:
1064 - 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 'begin
DELETE * FROM bramki' at line 3
It's an error with your delete syntax.
Get rid of the * so you're left with
DELETE FROM bramki
A typical delete will be in the format
DELETE FROM TableName WHERE ... -- the WHERE clause is optional here
Add Delimiters to make it correct. Here is the correct syntax to do so:
DELIMITER //
CREATE TRIGGER clean
AFTER INSERT
ON mecze_druzyny FOR EACH ROW
BEGIN
DELETE FROM bramki;
END; //
DELIMITER ;

Error in trigger

DELIMITER $$
USE `airdb`$$
DROP TRIGGER /*!50032 IF EXISTS */ `updateprodaja`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `updateprodaja` BEFORE UPDATE ON `prodaja`
FOR EACH ROW BEGIN
IF ( SELECT podmireno FROM prodaja WHERE id = NEW.id ) > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: you got an error'
END IF;
END;
$$
DELIMITER ;
Got given error
Error Code: 1064
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 'END IF;
END' at line 7
try with this you forget to terminate sql query before end if
DELIMITER $$
USE `airdb`$$
DROP TRIGGER /*!50032 IF EXISTS */ `updateprodaja`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `updateprodaja` BEFORE UPDATE ON `prodaja`
FOR EACH ROW BEGIN
IF ( SELECT podmireno FROM prodaja WHERE id = NEW.id ) > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: you got an error';
END IF;
END;
$$
DELIMITER ;

Multiple IF in Mysql Trigger is not working

I'm trying to log changes of a table into another table. But my trigger is not working. Need help:
// Mysql code below
create trigger upd_stu_info
before update
on kisalg_student
for each row
begin
IF OLD.stu_name != NEW.stu_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type,change_head, old_value,new_value,user,date_time,session)
VALUES
(NEW.adm_no, 'stu_info','Change In Student Name ', OLD.stu_name, NEW.stu_name, NEW.user, NEW.date_time, NEW.session);
END IF;
IF OLD.fat_name != NEW.fat_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type, change_head, old_value, new_value, user, date_time, session)
VALUES
(NEW.adm_no, 'stu_info','Change In Father Name ', OLD.fat_name, NEW.fat_name, NEW.user, NEW.date_time, NEW.session);
END IF;
end;
I just tried the trigger on mysql and using the following I did not get any syntax error
delimiter //
create trigger upd_stu_info
before update
on kisalg_student
for each row
begin
IF OLD.stu_name != NEW.stu_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type,change_head, old_value,new_value,user,date_time,session)
VALUES
(NEW.adm_no, 'stu_info','Change In Student Name ', OLD.stu_name, NEW.stu_name, NEW.user, NEW.date_time, NEW.session);
END IF;
IF OLD.fat_name != NEW.fat_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type, change_head, old_value, new_value, `user`, date_time, session)
VALUES
(NEW.adm_no, 'stu_info','Change In Father Name ', OLD.fat_name, NEW.fat_name, NEW.`user`, NEW.date_time, NEW.session);
END IF;
end; //

Insert query in mysql using stored procedure

I have to insert datas using stored procedure in mysql.Here is my SP and function call to that SP.But I can't insert values using them. Can you please verify and give me what to do?
SP is:
delimiter ; ;
DROP PROCEDURE IF EXISTS sp_insertUserDetails ; ;
CREATE PROCEDURE sp_insertUserDetails( )
BEGIN
INSERT INTO tbl_userDetails
( strEmail,bitAllowClicktoFBProfile,bitIsAbbreviateLastName, strAboutMe )
VALUES ($strEmail, $bitAllow, $bitAbbreviate, $strAbout);
END
Call to this SP from php file:
$query = mysql_query("CALL sp_insertUserDetails($strEmail, $bitAllow, $bitAbbreviate, $strAbout)");
You dont define any arguments to your SP - did you even try call it straight from SQL eg:
mysql> call sp_insertUserDetails("foo", "bar", "dave", "str");
ERROR 1318 (42000): Incorrect number of arguments for PROCEDURE test.sp_insertUserDetails; expected 0, got 4
You need something like this
DROP PROCEDURE IF EXISTS sp_name;
delimiter ;;
CREATE PROCEDURE sp_name(fname varchar (20), lname varchar(20))
BEGIN
SELECT concat('Hello ', fname, ',', lname);
END
;;
delimiter ;
just do this
HERE i am assuming that all values are in varchar
DELIMITER //
DROP PROCEDURE IF EXISTS sp_insertUserDetails;
CREATE PROCEDURE sp_insertUserDetails(
IN strEmail varchar(255),
IN bitAllow varchar(255),
IN bitAbbreviate varchar(255),
IN strAbout varchar(255)
)
BEGIN
INSERT INTO tbl_userDetails
( strEmail,bitAllowClicktoFBProfile,bitIsAbbreviateLastName, strAboutMe )
VALUES (strEmail, bitAllow, bitAbbreviate, strAbout);
END //
DELIMITER ;
and call like this
mysql_query( CALL sp_insertUserDetails($strEmail, $bitAllow, $bitAbbreviate, $strAbout) )

Trigger Mysql Error Near the "

I'm trying to create a trigger and its giving me an error that I can't understand the reason of it. In my local server it works perfectly and in the remote doesn't. Both MySQL version are the same, MySQL 5.1.48.
Here is the trigger code:
CREATE TRIGGER actualizar_cliente
BEFORE UPDATE ON cliente FOR EACH ROW BEGIN
IF NEW.password = "" THEN
SET NEW.password = OLD.password;
ELSEIF NEW.password IS NULL THEN
SET NEW.password = (MD5(NEW.password));
END IF;
END
Here is the error message:
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 '' at line 4
DELIMITER $$
CREATE TRIGGER actualizar_cliente
BEFORE UPDATE ON cliente FOR EACH ROW BEGIN
IF NEW.password = "" THEN
SET NEW.password = OLD.password;
ELSEIF NEW.password IS NULL THEN
SET NEW.password = (MD5(NEW.password));
END IF;
END
$$
DELIMITER ;
and you probably need IS NOT NULL:
ELSEIF NEW.password IS NOT NULL THEN

Categories