Error adding" 0 " to a tinyint(1) field in the database

Asked

Viewed 122 times

1

In my database in the users table, I have a field called status, whose type is tinyint(1). If the status is "0" the user is enabled (can access the site), if the status is "1", disabled. But when doing an insert and setting the status ="0" of the error.

My doubt is, why this happens, and how to get him to accept 0.

The strange thing is that I have already developed other sites, whose use this same logic and accepts the " 0 " quietly

Follows the code:

Cógigo that is on the users page:

    $tabela = "usuarios";
    $dados = array(
        'usuario' => $p_usuario,
        'senha' => $hash_senha,
        'status' => "0",
        'permissao' => "0",
        'data_criacao' => $data_modificacao,
        'data_modificacao' => $data_modificacao
    );
    $sql_ins_usuarios_resultado = adicionar($tabela, $dados);

Code that is on another page called by Function:

// Armazenas os dados do array
$adc_campos = array_keys($adc_dados);
// contagem dos campos existentes
$adc_n_campos = count($adc_dados);
// Inicia a sintaxe
$adc_sintaxe = "INSERT INTO ".$adc_tabela." (";
//monta o resto da estrutura
for($adc_aux=0; $adc_aux<$adc_n_campos; $adc_aux++){
    $adc_sintaxe.= $adc_campos[$adc_aux].", ";
}
//retira a ultima virgula
$adc_sintaxe = substr($adc_sintaxe, 0, -2);
// fecha os campos e adciona o VALUES na sintaxe
$adc_sintaxe .= ") VALUES (";
//adiciona os valores na sintaxe
for($adc_aux=0; $adc_aux<$adc_n_campos; $adc_aux++){
    $adc_sintaxe.= ":".$adc_campos[$adc_aux].", ";
}
// Retira a ultima virgula
$adc_sintaxe = substr($adc_sintaxe, 0, -2);
// Fim da sintaxe
$adc_sintaxe .= ")";
// chama a função global para fazer conexão com o Banco de dados
global $conexaobd;
// prepara a sintaxe
$adc_preparado = $conexaobd->prepare($adc_sintaxe);
// seta os valores de cada campo
for($adc_aux=0; $adc_aux<$adc_n_campos; $adc_aux++){
    if(!$adc_dados[$adc_campos[$adc_aux]]){
        $adc_dados[$adc_campos[$adc_aux]] = NULL;
    }
    $adc_preparado -> bindParam(":".$adc_campos[$adc_aux], $adc_dados[$adc_campos[$adc_aux]]);
}
return $adc_preparado->execute();
//Para debugar o código (retirar o return do $adc_preparado->execute()
        /*-->*/  //  $adc_preparado->debugDumpParams();

When you start debugging UmpParams() the syntax is as follows::

INSERT INTO usuarios (usuario, senha, status, permissao, data_criacao, data_modificacao) VALUES (:usuario, :senha, :status, :permissao, :data_criacao, :data_modificacao) Params: 6 Key: Name: [8] :usuario paramno=-1 name=[8] ":usuario" is_param=1 param_type=2 Key: Name: [6] :senha paramno=-1 name=[6] ":senha" is_param=1 param_type=2 Key: Name: [7] :status paramno=-1 name=[7] ":status" is_param=1 param_type=2 Key: Name: [10] :permissao paramno=-1 name=[10] ":permissao" is_param=1 param_type=2 Key: Name: [13] :data_criacao paramno=-1 name=[13] ":data_criacao" is_param=1 param_type=2 Key: Name: [17] :data_modificacao paramno=-1 name=[17] ":data_modificacao" is_param=1 param_type=2
  • Show the INSERT code.

  • @bfavaretto made!

  • Putz, can you also include the SQL generated by this PHP? An example that is giving the cited error.

  • see if it helps you now

  • Maybe because the parameter type is string? Try to change "0" to 0 there in the array

  • @bfavaretto nothing changes :C

  • I wasn’t really trusting rs. What’s the error message?

  • @bfavaretto no error message appears, the strange thing is that if I put 1 instead of 0 works perfectly.

  • But saves what when you zero? Null? Send for, sets the field to not null

  • @bfavaretto it adds nothing, the field is already as not NULL

  • Well, the query is failing, it may be for several reasons. You have to find a way to get your program to spit out the database error message.

  • And if you set 0 as default in this field in Mysql and omit the field when you give an Insert?

  • 1

    It could be bullshit. but comment on this if(!$adc_dados[$adc_campos[$adc_aux]]){ $adc_dados[$adc_campos[$adc_aux]] = NULL; } condition and see if "0" will go to the database

  • @Andersonhenrique, because mine worked, but this line is necessary :C

  • 1

    @let’s Gabriel what’s going on is that there’s a Boolean condition right? then the moment you do it if(!value) when it drops zero, zero is false in Boolean and 1 is true, when it drops zero would be like if(0 == false ) because you put a ! before, ai entra dentro do if certo?

  • @Andersonhenrique, yes this had made a syntax similar to this one now. Anyway, ME helped, you can post your answer! : D

  • Posted Brother Thanks @Gabrielfilippi

  • @bfavaretto thanks for trying!!

Show 13 more comments

1 answer

2


what you could do is the following go back to 0 as String "0" equal here 'status' => "0", 'permission' => "0" and inside that if you did this check

if(!$adc_dados[$adc_campos[$adc_aux]]){ 


   if($adc_dados[$adc_campos[$adc_aux]] == "0" ){ 
      $adc_dados[$adc_campos[$adc_aux]] = 0 
    } 

 $adc_dados[$adc_campos[$adc_aux]] = NULL; 
 } 

and put this on

$adc_dados[$adc_campos[$adc_aux]] = NULL;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.