Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:12:06+00:00 2026-06-08T15:12:06+00:00

I receive via POST the following variables, sometimes it must be a date and

  • 0

I receive via POST the following variables, sometimes it must be a date and sometimes it must be null (for example if that event has not happened yet)

$hora_entrada = $_POST['hora_entrada'];
$salida_comida = $_POST['salida_comida'];
$regreso_comida = $_POST['regreso_comida'];
$hora_salida = $_POST['hora_salida'];

so, I assign NULL into the variables if POST is empty:

if ($hora_entrada == '') {$hora_entrada = "NULL";}
if ($salida_comida == '') {$salida_comida = "NULL";}
if ($regreso_comida == '') {$regreso_comida = "NULL";}
if ($hora_salida == '') {$hora_salida = "NULL";}

now, I want to insert the values into a mysql table:

UPDATE  `nomina`.`registro_tiempo` SET  
`hora_entrada` =  '$hora_entrada',
`salida_comida` =  '$salida_comida',
`regreso_comida` =  '$regreso_comida',
`hora_salida` =  '$hora_salida',
WHERE  `registro_tiempo`.`id_tiempo` ='$id_tiempo';
") or die (mysql_error());

the problem is that when the variable is NULL the record says 00:00:00 and I want to keep it NULL

I tried to assign NULL in this two ways without success:

$variable = NULL;

$variable = "NULL";

NOTE: The MySql fields have the NULL value as Predetermined.

¿Do you know any other way to do this? I will apreciate your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:12:08+00:00Added an answer on June 8, 2026 at 3:12 pm

    The reason is 'NULL' and NULL are two different things to SQL. You are inserting 'NULL' (the literal string NULL) when you should be inserting NULL (The predetermined NULL value)

    UPDATE  `nomina`.`registro_tiempo` SET  
    `hora_entrada` =  'NULL',
    `salida_comida` =  'NULL',
    `regreso_comida` =  'NULL',
    `hora_salida` =  'NULL',
    WHERE  `registro_tiempo`.`id_tiempo` ='$id_tiempo';
    ") or die (mysql_error());
    

    It should be

    UPDATE  `nomina`.`registro_tiempo` SET  
    `hora_entrada` =  NULL,
    `salida_comida` =  NULL,
    `regreso_comida` =  NULL,
    `hora_salida` =  NULL,
    WHERE  `registro_tiempo`.`id_tiempo` ='$id_tiempo';
    ") or die (mysql_error());
    

    To do an easy fix is to put logic to wrap the contents with quotes if it isn’t NULL:

    <?php
    $var = $var != "" ? "'" . $var . "'" : "NULL";
    $sql = "INSERT INTO MyTable VALUES ('$id', $var)";
    ?>
    

    And remember to escape your content (especially if it is provided by the user!) See http://php.net/manual/en/security.database.sql-injection.php

    EDIT:

    <?php
    // If the variable is not null/empty, wrap quotes around it. Otherwise, store as NULL
    $hora_entrada = $_POST['hora_entrada'] != "" ? "'" . $_POST['hora_entrada'] . "'" : "NULL";
    $salida_comida = $_POST['salida_comida'] != "" ? "'" . $_POST['salida_comida'] . "'" : "NULL";
    $regreso_comida = $_POST['regreso_comida'] != "" ? "'" . $_POST['regreso_comida'] . "'" : "NULL";
    $hora_salida = $_POST['hora_salida'] != "" ? "'" . $_POST['hora_salida'] . "'" : "NULL";
    
    // You should also mysqli_real_escape_string them
    $hora_entrada = mysqli_real_escape_string($hora_entrada);
    $salida_comida = mysqli_real_escape_string($salida_comida);
    $regreso_comida = mysqli_real_escape_string($regreso_comida);
    $hora_salida = mysqli_real_escape_string($hora_salida);
    
    "UPDATE  `nomina`.`registro_tiempo` SET  
    `hora_entrada` =  $hora_entrada,
    `salida_comida` =  $salida_comida,
    `regreso_comida` =  $regreso_comida,
    `hora_salida` =  $hora_salida,
    WHERE  `registro_tiempo`.`id_tiempo` ='$id_tiempo';
    ") or die (mysql_error());
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that may receive data via various methods and in various
I'm trying to enable Ruby via the plugin manager, and I receive the following
I have a test script to receive an xml file via http post and
So I have a Sinatra app that receives an XML via a HTTP POST
I have the following Javascript snippet that receives an customerID via parameter (i'm going
I received the following error when committing via TortoiseSVN: Error: post-commit hook failed (exit
How to receive the post's ID, if post is found via this '.any' call?
This is a simplification, but I have an app that has the following methods:
I need to receive incoming emails as multipart-formdata via a POST request from Cloudmailin.
I have application that received data via HTTP POST requests. I'm trying to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.