How to insert NULL value on Date - php

I need to be able to set a Null value in DB Oracle if the user wont choose a date.
$createdon = date('m/d/Y', strtotime($_POST['createdon']));
INSERT INTO purchase (CREATEDON) VALUES(to_date('$createdon','mm/dd/yy'))
I am able to insert date if a user choose a date but if the user wont the date that is save on the database is "01/01/1970". Also, if i dont use "to_date" tag i cant insert record on the database.
Any suggestion on what should i do and how?
Thank you.

TO_DATE is OK because - if you're passing a string (which represents a date value), it is a preferable way so that YOU have control over it. Because, if you pass '01.02.20', which is which? Is 01 day or month? Is 02 day, year, ... month? Who knows. Don't depend ond defaults. So, if you say to_date('01.02.20', 'rr.mm.dd'), then everyone (Oracle included) knows what to do.
As of 01/01/1970: if you insert null but still see that value in a column, it means that
column has a default value
database trigger inserted it
Check both of these on that table.

Related

MySQL custom Timestamp value

I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!

Php/Mysql date saved as '0000-00-00'

I have a Mysql database where i keep dates of the clients orders.
Date is generated by a php function date('Y-m-d'). Two records in my database have values '0000-00-00'. One was created by a client from Australia and another by a client from Italy. Other 5000 orders are fine.
My question is how is it possible to generate such date ? Could it be something with individual browser settings ?
Thanks for help
The only reason what i can find is either your client has entered improper date format or he has entered an empty string.
Remember the date format in Mysql is yyyy-mm-dd.
I noticed I was having the same exact issue, and it turned out it was simply that I didn't put single quotes around my $date variable when inserting it, thus it was inserting a null or blank value which appeared as 0000-00-00 in the database. Once I put '$date' instead of $date in my insert statement, the value appeared as expected.
It means MySQL was passed an invalid value including no value at all. Check your code to make sure that it always has a valid value for that date.
Check this post default date '0000-00-00 00:00:00' or null. It will answer your question. Probably, year, month and day variables at their client side code have not been initialized.
EDIT
If that's not the case, then it seems you have recently changed the Date-related column in the database from VARCHAR or TEXT to DATE.
MySql changes all NULL values in this case to '0000-00-00'. So, if some time back, date has not been a compulsory field on the UI and if it was passed as NULL, then the above thing has happened.
It could be the case that you have purely programmed or outdated mysql function. For instance: CURDATE() + 5 will insert the date correctly in the DB, however if the user does it on 2016-12-28 the DB will show 0000-00-00. It will work fine if it the current date is 2016-12-20. The absolutely correct function would be CURDATE() + INTERVAL 5 DAY.
You write at the beginning of Jan, so maybe u have similar issue.

Month automatically increasing in phpmyadmin database table

Is there any way to increase a table field named month every month..or I should say to the latest month...Just need to update the month value..i.e. In, it should be 1
march 3
July 7
etc.
Help me.
Closest you'll get without manually inserting idate('m') with every new record and without using fancy mysql... is by using mysql default TIMESTAMP field. Check this out:
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
But this a date so you'll need to use MONTH(Field) to get the month.

php mysql - phpmyadmin DATE

i'm new to databases. I have just created my first database, so far so good. Now I looking to add time in to this.
I have certain table, when it's displayed I need it to show the table info + the date that info was added. Also possible the difference in between (5day 5hours ago) or something of the sort.
Do i need to add another column to the data table where the time will be stored? If so is there a way to auto increment that to current date? How do I go about doing that?
Not really sure where to start can someone please point me in the right direction perhaps a tutorial somewhere? Cant seem to find anything solid on this topic
Thanks a bunch!:)
For each record in your DB, you want to know when it has been created?
You have to add a column of type "timestamp". You can then set the default to "current_timestamp".
Note also that you can use the "on update current_timestamp". Instead of saving current time on insertion, it will do so each time your record is updated.
If you need the 2 dates (created_at and updated_at), you'll need 2 columns. EDIT : well you can't :p
There is a datetime as well as a timestamp field. If you use a timestamp field you can have the field automatically populate with the timestamp of when the row was inserted.
http://dev.mysql.com/doc/refman/5.0/en/datetime.html
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
I suggest trying out http://sqlzoo.net/ for a gentle introduction to SQL.

Mysql 2 date type

Can I use 2 date type in 1 mysql table? because for some reason when I insert records for 2 different dates it doesnt work, only the first date is working the other one shows 00-00-00?
Thank you for your time and help
I will assume you are using a TIMESTAMP data type, and you want it to default to CURRENT_TIMESTAMP.
Only one TIMESTAMP column in a table can automatically be set on INSERT or UPDATE - and by default, MySQL goes with the first one.
If you would like to have more than one TIMESTAMP column get updated automatically on INSERT or UPDATE, you will need to turn to triggers.
The docs on the TIMESTAMP data type are a good read!
Edit: if this educated guess does not answer your rather vaguely-phrased question, you will probably want to update your question with an example of the queries you are running, and what exactly is happening/not happening afterward.

Categories