Error save field type data null, not set field as notnull

Asked

Viewed 1,068 times

0

I am trying to save a possible "client" however I am not defining for him an admission date field, only that when I do it the field is not set and still gives error.

Follow my PHP code, the database all fields are null valid.

<?php

// incluindo o arquivo que faz a conexao com o banco
include ("../includes/conexao.php");
include ("../includes/suc_validacao.php");
include ("../includes/suc.php");

$nome = $_POST['TXT_NOMEX_CLIEN'];
$apelido = $_POST['TXT_APELI_CLIEN'];
$mensagem = $_POST['MEM_APRES_CLIEN'];
$naturalidade = $_POST['TXT_NATUR_CLIEN'];
$nacionalidade = $_POST['TXT_NACIO_CLIEN'];

if (strcmp($_POST['DAT_NASCI_CLIEN'], "") == 1) {
    $dtnasc = $_POST['DAT_NASCI_CLIEN'];
    $dtnasc = implode("-", array_reverse(explode("/", $dtnasc)));
} else {
    $dtnasc = null;
}
$ocupacao = $_POST['TXT_OCUPA_ATUAL'];
$clube = $_POST['TXT_NOMEX_CLUBE'];

if (strcmp($_POST['TXT_DATAX_ADMIS'], "") == 1) {
    $desde = $_POST['TXT_DATAX_ADMIS'];
} else {
    $desde = null;
}
$desde = implode("-", array_reverse(explode("/", $desde)));

if (strcmp($_POST['TXT_ALTUR_CLIEN'], "") == 1) {
    $altura = $_POST['TXT_ALTUR_CLIEN'];
    $altura = str_replace(",", ".", $altura);
} else {
    $altura = null;
}

if (strcmp($_POST['TXT_PESOX_CLIEN'], "") == 1) {
    $peso = $_POST['TXT_PESOX_CLIEN'];
    $peso = str_replace(",", ".", $peso);
} else {
    $peso = null;
}

$gostede = $_POST['TXT_GOSTO_CLIEN'];
$naogostade = $_POST['TXT_NGOST_CLIEN'];
$twitter = $_POST['TXT_ENDER_TWITR'];
$facebook = $_POST['TXT_ENDER_FACEB'];
$youtube = $_POST['TXT_ENDER_YOUTB'];
$menuvinc = $_POST['P_COD_IDENT_MENUX'];
$usurLoga = $_SESSION['UsuarioID'];

//echo $altura.','.$desde.','.$peso.','.$dtnasc; die;

$query = "INSERT INTO tbl_CLIENTES (COD_IDENT_MENUX, TXT_NOMEX_CLIEN, TXT_APELI_CLIEN, MEM_APRES_CLIEN, FLG_TIPOX_CLIEN, COD_IDULT_ATUAL, DAT_ULTIM_ATUAL) VALUES";
$query .= "('$menuvinc', '$nome','$apelido','$mensagem','F', '$usurLoga', now())";

$inserir = mysql_query($query)
        or die(mysql_error());

$COD_IDENT_ULTIM_CLIEN = mysql_insert_id();

$query2 = "INSERT INTO tbl_CLIENTES_PF (COD_IDENT_CLIEN, TXT_NATUR_CLIEN, TXT_NACIO_CLIEN, DAT_NASCI_CLIEN, TXT_OCUPA_ATUAL, TXT_NOMEX_CLUBE, TXT_ALTUR_CLIEN, TXT_PESOX_CLIEN, TXT_ENDER_TWITR, TXT_ENDER_FACEB, TXT_ENDER_YOUTB, TXT_DATAX_ADMIS, TXT_GOSTO_CLIEN, TXT_NGOST_CLIEN, COD_IDULT_ATUAL, DAT_ULTIM_ATUAL) VALUES";
$query2 .= "('$COD_IDENT_ULTIM_CLIEN','$naturalidade','$nacionalidade', '$dtnasc' ,'$ocupacao','$clube','$altura','$peso','$twitter','$facebook','$youtube','$desde','$gostede','$naogostade', '$usurLoga', now())";

//executando a query
$inserir = mysql_query($query2)
        or die(mysql_error());

$response = array("success" => true);

//fechando a conexao com o banco
mysql_close($conn);

header("Location: listaClientes.php");
exit; // Redireciona o visitante
?>

This is the error that occurs: Incorrect date value: '' for column 'DAT_NASCI_CLIEN' at Row 1

  • Have you tried print_r() in your query, with die() and see what happens?

  • below $query2.= " INSERT..."; echo "<pre>"; print_r($query2); die();

  • 1

    have to see what kind of data is configured for that field in the table. If it accepts null. And if date comes in hue_BR format "20/01/2015" your explode/implode will leave "20-01-2015" and usually in the database format eh Y-m-d

  • mysql_query is being deprecated, you should use mysqli_query, soon this type of query will be discontinued.

  • Put a var_dump($dtnasc) to see what value is being passed in the query. You can also echo the query itself to help debug. If $dtnasc differs from yyyy-mm-dd as already quoted by Adir Kuhn, it will fail.

1 answer

1


You need to replace the empty variable with the NULL string:

$dtnasc = 'NULL';

Browser other questions tagged

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