Abdul Aziz Al Basyir
Abdul Aziz Al Basyir

Reputation: 1246

Foreach for MySQL (In Trigger)

I want to create validate if that NEW.Some Field = ' ' (THAT IS NOT NULL JUST EMPTY INPUT) then text messange is "[table:person] - Some Field column is not valid", and i using this method now:

BEGIN
IF NEW.`Some Field1` = '' THEN
  SIGNAL SQLSTATE VALUE '45000'
  SET MESSAGE_TEXT = '[table:person] - `Some Field` column is not valid';
END IF;
IF NEW.`Some Field1` = '' THEN
  SIGNAL SQLSTATE VALUE '45000'
  SET MESSAGE_TEXT = '[table:person] - `Some Field` column is not valid';
END IF;
END;

and this method we cant do if too many validate, I still do not understand with NEW.Some Field, is it like an array in php and other prog. languages

and if same, can we use a method like in HP, I give an example:

$new = array('some1'=>'TEST','some2'=>'');
foreach($new as $field => $val){
   if($val == ''){
     echo "[table:person] - `{$field}` column is not valid";
     //and final we can use continue or return false
   }
}
//And that result is "[table:person] - `some2` column is not valid"

Upvotes: 0

Views: 1159

Answers (1)

P.Salmon
P.Salmon

Reputation: 17615

https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. OLD and NEW are MySQL extensions to triggers; they are not case sensitive. In an INSERT trigger, only NEW.col_name can be used; there is no old row. In a DELETE trigger, only OLD.col_name can be used; there is no new row. In an UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before it is updated and NEW.col_name to refer to the columns of the row after it is updated. A column named with OLD is read only

Upvotes: 1

Related Questions