Hey guys i'm doing a website that generates, multiple types of tournaments and i keep getting this error on phpmyadmin when i try to update the "punteggio(point)" column on my database so that it can automatically generate the next phase of the tournament, i believe the trigger is looping but i can't figure out where.
I'm using the phpmyadmin gui interface to create the triggers.
this error pops up only when i enable the second trigger, The first one works fine without the second one.
This is the error
#1442 - Can't update table 'Partecipa' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Translations:
Gara = match;
Torneo = tournament;
punteggio = point
vincitori = Winners
fase = phase
TRIGGER FUNCTION I(AFTER UPDATE ON Partecipa) :This function will be Triggered after a punteggio( or point) is updated
BEGIN
SET @garamodificata = NEW.Gara_id_Gara;
SET @punteggio = (SELECT Punteggio FROM Partecipa WHERE Gara_id_Gara = @garamodificata AND Punteggio != NEW.Punteggio );
SET @verifica = (SELECT Id_gara FROM Vincitori WHERE Id_gara = @garamodificata);
SET @verificaEliminazioneDiretta = (SELECT Tipo_torneo FROM Torneo WHERE Id_torneo = NEW.Gara_Torneo_Id_torneo);
IF(@verificaEliminazioneDiretta = "Eliminazione Diretta") THEN
IF(@verifica IS NULL ) THEN
IF(@punteggio > NEW.Punteggio) THEN
SET @giocatore = (SELECT Giocatore_id_Giocatore FROM Partecipa WHERE Gara_id_Gara = @garamodificata AND Punteggio = @punteggio);
INSERT INTO Vincitori(Id_gara,Id_giocatore)VALUES(@garamodificata,@giocatore);
ELSEIF(@punteggio < NEW.Punteggio) THEN
SET @giocatore = (SELECT Giocatore_id_Giocatore FROM Partecipa WHERE Gara_id_Gara = @garamodificata AND Punteggio = NEW.Punteggio);
INSERT INTO Vincitori(Id_gara,Id_giocatore)VALUES(@garamodificata,@giocatore);
END IF;
ELSE
IF(@punteggio> NEW.Punteggio) THEN
SET @giocatore = (SELECT Giocatore_id_Giocatore FROM Partecipa WHERE Gara_id_Gara = @garamodificata AND Punteggio = @punteggio);
UPDATE Vincitori SET Id_giocatore = @giocatore WHERE Id_gara = @garamodificata;
ELSEIF(@punteggio < NEW.Punteggio) THEN
SET @giocatore = (SELECT Giocatore_id_Giocatore FROM Partecipa WHERE Gara_id_Gara = @garamodificata AND Punteggio = NEW.Punteggio);
UPDATE Vincitori SET Id_giocatore = @giocatore WHERE Id_gara = @garamodificata;
END IF;
END IF;
END IF;
END
TRIGGER FUNCTION(AFTER INSERT TRIGGER ON Vincitori) : this function inserts in partecipa the new matches of the next phase and it is an AFTER INSERT TRIGGER ON Vincitori
BEGIN
SET @fase = (SELECT MAX(Fase)FROM Partecipa WHERE Gara_id_Gara = NEW.Id_gara); #phase of the tournament
SET @torneo = (SELECT Torneo_Id_torneo From Gara WHERE id_Gara = NEW.Id_gara); #id of the tournament
SET @expectedResults = (SELECT COUNT(*)FROM Partecipa WHERE Fase = @fase AND Gara_Torneo_Id_torneo = @torneo)/2; #number of results needed to generate next phase
SET @actualResults =(SELECT COUNT(*) FROM Vincitori INNER JOIN Partecipa ON Vincitori.Id_gara = Partecipa.Gara_id_Gara WHERE Partecipa.Gara_Torneo_Id_torneo = @torneo AND Partecipa.Fase = @fase)/2;
#numberof results in Vincitori
SET @i = 1;
SET @j = 2;
IF(@expectedResults = @actualResults) THEN
CREATE TEMPORARY TABLE gare(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, id_giocatore VARCHAR(45),id_gara INT(11));
INSERT INTO gare(id_giocatore,id_gara)
SELECT DISTINCT Vincitori.id_giocatore,Vincitori.id_Gara
FROM Vincitori INNER JOIN Partecipa ON Vincitori.Id_gara = Partecipa.Gara_id_Gara WHERE Partecipa.Gara_Torneo_Id_torneo = @torneo;
SET @garedagenerare = (@actualResults/2)+1;
SET @fase = @fase + 1;
WHILE (@i < @garedagenerare) DO
SET @idprossimagara = (SELECT DISTINCT MAX(id_gara) FROM gare);
SET @idprossimagara = (@idprossimagara + 1);
SET @datagara = (SELECT DISTINCT Data_gara FROM Gara WHERE id_Gara = @idprossimagara);
SET @Player1 = (SELECT DISTINCT id_giocatore FROM gare WHERE id = @i);
SET @Player2 = (SELECT DISTINCT id_giocatore FROM gare WHERE id = @j);
INSERT INTO Partecipa VALUES(@Player1,@idprossimagara,@torneo,0,@datagara,@fase);
INSERT INTO Partecipa VALUES(@Player2,@idprossimagara,@torneo,0,@datagara,@fase);
SET @i = @i+2;
SET @j = @j+2;
END WHILE;
END IF;
END