MySQL Insert not working with Date column - php

I am having an issue with a simple insert query into a table.
I have this PHP Code
$T_MEMBER = "INSERT INTO T_MEMBER (MEMBER_IDENTIFIER,LAST_NAME,FIRST_NAME,BIRTH_DATE) VALUES ('$memberID','$last','$first','$birthdate')";
mysql_query($T_MEMBER) or die(mysql_error());
Here are a few examples of what the query looks like if i echo it:
INSERT INTO T_MEMBER
(MEMBER_IDENTIFIER,LAST_NAME,FIRST_NAME,BIRTH_DATE)
VALUES
('2007','Hayes','Karin','1958-30-10')
INSERT INTO T_MEMBER
(MEMBER_IDENTIFIER,LAST_NAME,FIRST_NAME,BIRTH_DATE)
VALUES
('2020','Long','Peggy','1968-29-5')
INSERT INTO T_MEMBER
(MEMBER_IDENTIFIER,LAST_NAME,FIRST_NAME,BIRTH_DATE)
VALUES
('2021','Torres','Diane','1968-30-8')
BIRTH_DATE is a date type column.
The problem is, after i do any of these queries, the date shows up as 000-00-00!!!! I have been wracking my brain and i cannot seem to find the issue.
Thanks,
Ian

The date needs to be in YYYY-MM-DD format. Yours is in YYYY-DD-M(thanks juliano) format by the way.
So instead of 1958-29-05, use 1968-05-29

You might also want to consider passing in the date as a variable, and first formatting it using mktime() and date().

Related

PDO bindValue isn't inserting a date

For work, i have to make an oracle database and mysql one communicate.
On Oracle, i have a vrp table with (among others) a DATECREATION column, in DATE format (alas, it's what SQL developer tells me when i click it). It stores dates in the DD/MON. /YY format.
I have to copy these values over to a mysql database. At first, i tried to ignore them and used them as strings; but i will have to manipulate them so i need to have them as date.
So in PHP, i do date('Y-m-d', strtotime($row[26])) and it works fine, turning '24-OCT-19' into 2019-10-24
I then try to insert this in my Mysql database, doing the following :
$stmt = $conn->prepare("INSERT INTO [...] VALUES (?, ? [...]);
$stmt->bindValue(27, date('Y-m-d', strtotime($row[26]))); (yes there are a lot of columns)
$stmt->execute($row);
if i echo the data it looks fine, i didn't mixed up my indexes since the adjacents columns don't get the date inserted. However, the 27th column receives a 0000-00-00.
No errors or warning are raised, so i don't even know how to debug this.
Thank you.
Rewrite your oracle select statement.
And use a following text conversion, to get a mysql date format as string
TO_CHAR( SYSDATE, 'YYYY-MM-DD HH24:MI:SS' )

selecting rows from database between two dates giving wrong results

Selecting rows from database between two dates giving wrong results, the below query not working for me. I tried some answers, but this one not giving the correct results.I think, i am missing somewhere.
SELECT * FROM Table WHERE Date BETWEEN '07/10/2015' AND '07/14/2015'
changed to
SELECT * FROM Table WHERE Date BETWEEN '07-10-2015' AND '07-14-2015'
still not working!
That's right, you can not use BETWEEN statement when the data type format is not a DATE or DATETIME, you must change the data type first.
BTW I just realized that even data type is a DATE/DATETIME format you can't use / in the SQL statement itself when using MySQL, versus SQL you can use / when the column data type is DATE/DATETIME. Just correct me if I'm wrong...
Use the default date format YYYY-MM-DD
SELECT * FROM your_Table
WHERE Date BETWEEN '2015-07-10' AND '2015-07-14'
Erm, you're searching on "Date" but are you meant to be searching on "CreateDate"? That's what your image shows.

error in save date in database with php

want to be able to store the current date from PHP in to a mysql table.
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."','".$date."','".$yes."')";
Date's column type in database is DATE.
but date save in database like this: 0000-00-00
help me.
try this
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."',".$date.",'".$yes."')";
or
if you want to inset today's date use
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
I need to store Date field in database for example VARCHAR(10) becouse, DATE type in mysql server don't support persian (farsi) date.
If you are getting the current date, just use one of the built-in MySQL functions for that:
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
You won't ever have to worry about formatting or any sort of funny inputs and you can still do all sorts of addition, subtraction and the like using one of the many other functions.
Edit: I have just looked up some information on the date system and you will run into problems trying to squeeze that data into a date field. The first six months have 31 days, the next 5 have 30 days, then the last has 29 - unless it is a leap year in which case it has 30. The date field simply won't LET you insert these values into a date field.
You might need to convert your data to a timestamp and write a function to encode/decode it into the format you need.

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 - Putting a date into a MySQL table

I have what is most likely a very simple question.. I am designing a simple blogging system and I am trying to put the current date into the table where the blog post is stored whilst waiting for administrator approval. but the method I have used puts 0000-00-00 into the date column! What I am using is as follows:
$query = "INSERT INTO blogentry VALUES ('".$mnam."','".date('d-m-Y h:m:s') ."\n"."','".$mcom."','".$approve."')";
I am relatively new to php so stumble accross errors like this all the time... but I cant seem to google this one!
Thanks guys!
So the easiest way to do this is just let MySQL handle it with the NOW() function:
INSERT INTO blogentry VALUES( ..., NOW(), ... )
Another option is to use TIMESTAMPs by changing your table - set the column to type TIMESTAMP with DEFAULT CURRENT_TIMESTAMP, and you can just ignore that column when inserting - it will automatically be filled with the current time. You will need to specify the columns you're inserting to in order to skip a column:
INSERT INTO blogentry( column1, column2 ) VALUES( column1value, column2value )
Finally, you NEED to sanitize your inputs. Preferably using prepared statements and PDO (http://php.net/manual/en/pdo.prepared-statements.php), or at least using mysql_real_escape_string.
From the MySQL manual on DATE, DATETIME
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
This means you have to insert the dates in YYYY-MM-DD format. You are using date('d-m-Y h:m:s') format. Change that to date('Y-m-d') and it should insert correctly.
If you want the time as well, then you need to change the column datatype to DATETIME and then insert using the format date('Y-m-d H:i:s').
As other mention, you can use an INT column type instead and store a Unix timestamp which is stored in UTC so it is more portable. You can then easily manipulate the timestamp to output the date any way you would like.
Try just storing a strtotime() result. It creates a unique timestamp, which can then be parsed however you need it in the future.
You might need to give the timestamp to the date function:
date('d-m-Y h:m:s', strtotime('now'))
Also, to do a standard datetime format:
date('Y-m-d H:i:s', strtotime('now'))

Categories