NULL value in an int field becomes 0 in a onAfterInsert event.

Hello ,
i have a table com_discussioni_dest with a field cod_gruppo INT(11)NULL DEFAULT NULL;
In an event onAfterInsert of a form application i make some insert in this table but when i insert a NULL value in the database it becomes 0 value (i have a variable $v_cod_gruppo tha i use to make the insert). When there is a real value for cod_gruppo all works fine. If i make the insert with an explicit NULL value without a variable it works
What could be the problem?
Regards
Gianpaolo

This is my code

$query=“select cod_utente,cod_gruppo”." from com_categorie_utgr"." where id_cat= ‘{id_cat}’";

sc_lookup(rs, $query);
$i=0;
while (isset({rs[$i][0]}))
{
if (isset({rs[$i][0]})) // Row found
{ // SQL statement parameters

$v_cod_utente={rs[$i][0]};
$v_cod_gruppo={rs[$i][1]};

if (is_null($v_cod_gruppo)) {$tipo_dest='U';} else {$tipo_dest='G';}    

$insert_table = ‘com_discussioni_dest’; // Table name
$insert_fields = array(
‘id_dis’ => “’{id}’”,
‘tipo’ => “’$tipo_dest’”,
‘cod_utente’ => “’$v_cod_utente’”,
‘cod_gruppo’ =>"’$v_cod_gruppo’",
‘utente_mod’ => “’{utente_mod}’”,
‘data_mod’ => “’{data_mod}’”
);

// Insert record
$insert_sql = ‘INSERT INTO ’ . $insert_table
. ’ (’ . implode(’, ‘, array_keys($insert_fields)) . ‘)’
. ’ VALUES (’ . implode(’, ', array_values($insert_fields)) . ‘)’;

sc_exec_sql($insert_sql);

}

$i=$i+1; }

That error was reported some years ago and still is not fixed. I had the same error and i fixed creating a trigger. But i am waitng Sc fix it soon.
In this Thread was reported too. http://www.scriptcase.net/forum/forum/scriptcase-8/bugs-aa/63974-solved-null-default-if-left-blank-on-update.

Hello,
i solved changing the sql script in this way :

$query = “INSERT INTO com_discussioni_dest”."(id_dis,tipo,cod_utente,cod_gruppo,utente_mod,data_mod) " ." VALUES(’{id}’, ‘$tipo_dest’, ‘$v_cod_utente’,".(($v_cod_gruppo==’’)?“NULL”:("’".$v_cod_gruppo."’")).",’{utente_mod}’,’{data_mod}’)";

sc_exec_sql($query);

Instead of $v_cod_gruppo i used “.(($v_cod_gruppo==’’)?“NULL”:(”’".$v_cod_gruppo."’"))."

Regards
Gianpaolo

The problem is with form, when it do insert automaticaly. (SC generates the SQL form Insert).