Null value in MySQL with PHP

1

Ajax is used to insert data, and although at the time of inserting, it works correctly, the problem is when they are inserted in null.

It should be noted that the data to be inserted is of the date type in the database.

when inserting an empty field, it appears in the BD as 0000: 00: 00

How can I get that vslor inserted in NULL?

function insertarIn(){               
    $.ajax({            
        type : 'POST',
        url  : '../indicador/Ininsert.php',
        data: $('#Inform').serialize(),
        cache: false,
        success :  function(data) {                 
            $("#Inresult").html(data).fadeIn();},
        complete: function(){
             setTimeout(function() {
              $('#Inresult').fadeOut();}, 5000);}})}; 
<div id="formulario">  
<center><div id="Inresult"></div></center> 
<form method="POST" id="Inform" >
  <input type="text" name="predio" id="predio" readonly style="display:; ;" >
  <center><label>Inicio proceso</label>
  <input type="text" name="fechaIn" style="margin-top: 12px;" id="fechaIn" readonly></center> 
  <br>
<div class="row">
   
  <div class="col-md-3 col-md-offset-3" style="margin-left: 50px; text-align: justify;">

        <input type="checkbox" name="expediente" id="expediente" onclick="Generacion()" style="margin: 10px 0px 18px 20px" >Generacion de expediente
        <br>

        <input type="checkbox" name="escritura" id="escritura" onclick="Anteproyecto()" style="margin: 10px 0px 18px 20px" > Anteproyecto de escritura
        <br>

        <input type="checkbox" name="rev"  id="rev" onclick="Revision()" style="margin: 10px 0px 18px 20px" >Revisión de expediente
        <br>
        
        <input type="checkbox" name="OAG"  id="OAG" onclick="Solicitud()" style="margin: 10px 0px 18px 20px" >Solicitud OAG y Autorizacion RR
        <br>
        
        <input type="checkbox" name="cheque"  id="cheque" onclick="Elaboracion()" style="margin: 10px 0px 18px 20px" >Solicitud de elaboración de cheque
        <br>
        
        <input type="checkbox" name="pago"  id="pago" onclick="Afectado()" style="margin: 10px 0px 18px 20px" >Pago al afectado
        <br>
        
        <input type="checkbox" name="firma"  id="firma" onclick="Residente()" style="margin: 10px 0px 18px 20px" >Firma del residente
        <br>
        
        <input type="checkbox" name="seg"  id="seg" onclick="Seguimiento()"  style="margin: 10px 0px 18px 20px" >Seg. obtencion de escrituras
        <br>

        <input type="checkbox" name="entrega"  id="entrega" onclick="Operativa()"  style="margin: 10px 0px 18px 20px" >Entrega área operativa
        <br>
 
        </div>

  <div class="col-md-3 col-md-offset-1" style="text-align: justify;">

  <label>fecha recepcion</label>
  <input type="text" name="fechaG"  style="margin-top: 12px;" id="fechaG" readonly>
  <br>

  <label>fecha recepcion</label>
  <input type="text" name="fechaA"  style="margin-top: 12px;" id="fechaA" readonly>
  <br>

  <label>fecha recepcion</label>
  <input type="text" name="fechaR" style="margin-top: 12px;" id="fechaR" readonly>
  <br>

  <label>fecha recepcion</label>
  <input type="text" name="fechaS" style="margin-top: 12px;" id="fechaS" readonly>
  <br>

  <label>fecha recepcion</label>
  <input type="text" name="fechaSo" value="<?php echo $formularioIn['TIND_PROC6']; ?>" style="margin-top: 12px;" id="fechaSo" readonly>
  <br>

  <label>fecha recepcion</label>
  <input type="text" name="fechaP"  style="margin-top: 12px;" id="fechaP" readonly>
  <br>

  <label>fecha recepcion</label>
  <input type="text" name="fechaF"  style="margin-top: 12px;" id="fechaF" readonly>
  <br>

  <label>fecha recepcion</label>
  <input type="text" name="fechaSe"  style="margin-top: 12px;" id="fechaSe" readonly>
  <br>

  <label>fecha recepcion</label>
  <input type="text" name="fechaE" style="margin-top: 12px;" id="fechaSe" readonly>
  <br>
</div>

  <div class="col-md-2 col-md-offset-2" style="text-align: justify;">

  <label>Dias</label>
  <input type="text" name="diaG" id="diaG" style="margin-top:13px; width: 25%" readonly>
  <br>
  
  <label>Dias</label>
  <input type="text" name="diaA" id="diaA" style="margin-top:13px; width: 25%" readonly>
  <br>

  <label>Dias</label>
  <input type="text" name="diaR" id="diaR" style="margin-top:13px; width: 25%"  readonly>
  <br>

  <label>Dias</label>
  <input type="text" name="disS" id="diaS" style="margin-top:13px; width: 25%"  readonly>
  <br>

  <label>Dias</label>
  <input type="text" name="diaSo" id="diaSo" style="margin-top:13px; width: 25%"  readonly>
  <br>

  <label>Dias</label>
  <input type="text" name="diaP" id="diaP" style="margin-top:13px; width: 25%"  readonly>
  <br>

  <label>Dias</label>
  <input type="text" name="disF" id="diaF" style="margin-top:13px; width: 25%"  readonly>
  <br>

  <label>Dias</label>
  <input type="text" name="diaSe" id="diaSe" style="margin-top:13px; width: 25%"  readonly>
  <br>

  <label>Dias</label>
  <input type="text" name="diaE" id="diaE" style="margin-top:13px; width: 25%"  readonly>
  <br>

  </div>

</div>
<center><br>

<label>Justificacion</label><br>
<textarea id="justi" rows="5" cols="80" readonly></textarea><br>

</form>

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php


require("../conexion.php");

  
 $fechaG= $_POST['fechaG'];
 $fechaE= $_POST['fechaE'];  
 $fechaA= $_POST['fechaA']; 
 $fechaR= $_POST['fechaR']; 
 $fechaS= $_POST['fechaS']; 
 $fechaSo= $_POST['fechaSo']; 
 $fechaP= $_POST['fechaP']; 
 $fechaF= $_POST['fechaF']; 
 $fechaSe= $_POST['fechaSe'];
 $predio= $_POST['predio'];
 $fechaIn= $_POST['fechaIn'];  


$fechaA = !empty($fechaA) ? "'$fechaA'" : "NULL";

 				$comprobar="SELECT * FROM tindicador WHERE TIND_IDPRE='$predio';";
 				$compro=$mysqli->query($comprobar);
 				


 					switch ($compro)  {
 						case ($compro->num_rows==0):

			 				$consulta = "INSERT INTO tindicador (
							TIND_IDPRE,TIND_PROC1,TIND_PROC2,TIND_PROC3,TIND_PROC4,TIND_PROC5,TIND_PROC6,TIND_PROC7,TIND_PROC8,TIND_PROC9,TIND_PROC10) 
							VALUES ('$predio','$fechaIn','$fechaG','$fechaA','$fechaR','$fechaS','$fechaSo','$fechaP','$fechaF','$fechaSe','$fechaE');";

							if($resultado = $mysqli->query($consulta)){ 

									echo "<h5 style='color:#06A322;'>Registro guardado correctamente</h5>";

							} else {

									printf("Errormessage: %s\n", $mysqli->error);
							}


 							
 							break;
 						
 						case($compro->num_rows>=1):
 							
 							$consultaD="UPDATE tindicador SET TIND_PROC1='$fechaIn',TIND_PROC2='$fechaG',TIND_PROC3='$fechaA',TIND_PROC4='$fechaR',
 										TIND_PROC5='$fechaS',TIND_PROC6='$fechaSo',TIND_PROC7='$fechaP',TIND_PROC8='$fechaF',TIND_PROC9='$fechaSe',
 										TIND_PROC10='$fechaE'
										WHERE TIND_IDPRE='$predio';";


							if($resultadoD = $mysqli->query($consultaD)){

								echo "<h5 style='color:#0280BF;'>Registro modificado correctamente</h5>";

							} else {

								echo "<h5 style='color:#A30606;'>Imposible modificar, ocurrio un error</h5>";
						    
							}
 							break;
 					}
 				

				$mysqli->close();
				?>

</body>
</html>
    
asked by Erik Raúl González Páez 20.09.2018 в 19:39
source

3 answers

1

Thanks to those who participated.

Even if I tried each of the options they wrote to me, in the end only the one of Oswell worked ... Even if there were another way, without changing the date to text type, I'll be aware

    
answered by 20.09.2018 / 19:56
source
1

In your database in the structure of the table you can define default data in case nothing is inserted, there you can include the null, or failing that to your variable define it so $ date = NULL ;, in case that does not bring anything.

    
answered by 20.09.2018 в 19:44
1

change the structure of the column and mark Null. But do not set a default value because fill it with 0000: 00: 00

When doing an insert simply do not call that column in the statement, Mysql will save the field directly as NULL.

Or if you want to keep the column in your sentence, do it like that.

Example: Insert into mytabla (column 1, column2, col_date) Values ('$ value1', '$ value2', null);

    
answered by 21.09.2018 в 13:04