PHP, how to write blank Date field in database?

Asked

Viewed 465 times

0

my situation, is the following on localhost, bd, ignores the letters of the date field (dd/mm/yyyy), but when I go up to the server, it error, because I’m sure it tries to save the letters of these date fields.

well, the problem is that I use a form just to register and change, so loco I use the value field to return the value of the table when I change, and so I can’t use the value to put for example the default values like 00/00/0000, and I couldn’t find another way to make the date field not FILLED record the ZEROS IN the table instead of giving error.

in the database the field date, this to accept the values as follows 0000-00-00, (the date inversions are working correctly).

and the field in the form is so:

<input class="form-control" type="date" name="data_dc" id="data_dc" value="<?=$obreiro_busca['data_dc']?>" style="width:360px">

like this one in the comic book:

 `data_dc` date DEFAULT '0000-00-00',

and this is the mistake:

Incorrect date value: '' for column 'data_dc' at Row 1

I can’t get the field to have ZEROS instead of letters.

I just can’t use the value field.

  • There is a special value in the database for "unfilled" value, this value would be NULL. Then just test if the field is not filled in PHP and insert NULL if empty. Ex.: $date = $_POST['data_dc'] ?: null;. It is just an example, in your case it would be necessary to validate this date and sanitize the user input.

  • excuse my ignorance, rsrs, so if I add this condition there in my code, I will have to check before recording right? or only this condition serves to verify that this null? if you can give more details, thank you, if you need I show more code if necessary, I will add like this in the BD.

  • You must set the database table to accept that the data_dc field accepts null/null values. On the condition that @fernandosavio spoke you will save the date that came from the form if it has been sent and if it has not been sent it will save null. So when date is not given you save null in the date column. Another thing that could be done is to change to the column in the bank to have a default value of 0000-00-00 when not informed, but I do not recommend after all what the sense of saving 0000-00-00.

  • so friend @fajuchem this last suggestion, was even working on localhost, but the server did not give, it still does not accept to record even with the standard 0000-00-00.

  • Well, and it’s up to you, you can start by accessing the server database and see how this table is and also take the query generated by your code and try to enter manually to see if you can get more information. Not much to help you with current information.

  • for now thanks for the help, I’m trying all these alternatives, and as soon as I can, already put here, the solution

Show 1 more comment
No answers

Browser other questions tagged

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