PDO bindValue isn't inserting a date - php

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' )

Related

Storing the $date variable into mysql database using php

I have a problem in storing the $date variable in the database column called data of type varchar(50)
This is the code of the date variable
$date = date("Y-m-d");
echo $date;
and this is the code that stores it into the database (notice that the date is the same one)
what is the problem with my code
$sql="INSERT INTO
Students(FirstName, LastName,gender,Major,Favorite_courses,GPA,date)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[sex]','$_POST[major]',
'$_POST[favorite]','$_POST[GPA]','$date')";
Change the column type to DATE and then use SYSDATE as the value to input the current date/time on the SQL server.
Also, read up on SQL injection!
Technically your code should work, but in practice it will undoubtedly fail.
For starters you should never put POST data directly into your database. Depending on what's in that data, it will at the least break your SQL statement. It could also destroy your database if someone entered some SQL into a POST variable.
Don't do it that way. You need to sanitize any data coming from the outside world before inserting it into the database. There are several PHP database classes that do this for you. I like PDO.
Also, write better PHP by using $_POST['favorite'] instead of $_POST[favorite]. What happens if you do this in your code somewhere define('favorite', 'foobar')?
What happens is that your code will than look for $_POST['foobar'] instead of $_POST['favorite'].
You really need to work on your knowledge of PHP and SQL before rolling anything out into the wild, or you're going to have problems. But keep plugging along, you'll get it.
Aside from the, already several times mentioned, SQL injection: use date('c') (or more specifically: ISO8601 notation). That will result in code like:
$query = "insert into mytable (myfield) values ('" . date('c') . "')";
Which will result in a query like:
insert into mytable (myfield) values ('2013-06-03T22:20:32+02:00')
This is an unambigious notation and should always work (Y-m-d will work fine too, as per your question, it only stores a date without any time). When using any other notation there's always the problem for the RDBMS that it has to know wether it has to interpret 02/12/1977 as February 12th 1977 or December 2nd 1977. Also, make sure that myfield (in my example) is of type DateTime or Date and not varchar and that you correctly escape reserved words like date in querystrings:
select foo, bar, `date`, foobar from mytable....
However, MySQL seems to 'allow' date (because of "MySQL permits some keywords to be used as unquoted identifiers because many people previously used them." wich is a stupid reason). It's best to just stick to escaping always:
select `foo`, `bar`, `date`, `foobar` from `mytable` ....
Please note that I did not use any sort of MySQLi or PDO prepared statements in this example; you should go read up on SQL injection and then on those topics and then go back to your code.
You can use the php class Date and use his format function

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'))

How to bind dates and other variables to php sql query? (MySql)

I have several db calls in my site with bind_variables that works fine. But, I can find the correct sign for Date in the documentation, for the command:
$query->bind_param("ssi",...);
I don't want to do something like:
$db->query('SELECT item FROM table WHERE something='.$something);
Since this is string manipulation, not binding. (In binding the query is left with the "?" and that makes the queries faster because the DB sees them the same only with different cariables.)
If I wasn't very clear, I want to do the same as this only with a date variable type.
Some extra information to my comment given above:
If I do understand correctly have a
look at:
Using Mysqli bind_param with date and time columns?.
It looks like you can just treath it
as a string.
If you want to do it with bind_param it is the only way I know to do it and I don't see any problems. If mysql receives a wrong formatted date it will insert a 0000-00-00 value to your table.
Can you tell me what you think could be a problem? If you insert it as a normal query you also use the same syntax as a String.
For dates, you will have to format them before calling bind_param:
$query->bind_param('s', $date->format('Y-m-d H:i:s')); // assuming $date is a DateTime object
Dates should be bound as strings in a format MySQL accepts (yyyy-mm-dd).

MySQL Insert not working with Date column

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().

Properly formatted MySQL date insert statement returns all 0's

I've created a PHP script that parses an HTML page, creates a MySQL query, and inserts the parsed data into a MySQL table. Everything works fine, except for one thing : the date field. When PHP runs the MySQL statement, the dates in the table read 0000-00-00 . If I echo the MySQL query, it results in the following :
INSERT INTO dispatch_table (dispatch_date, dispatch_time, dispatch1, dispatch2, dispatch3, dispatch4, dispatch5, dispatch6, dispatch7, dispatch8, dispatch9, dispatch10, dispatch11, dispatch12, dispatch13, dispatch14, dispatch15, dispatch16)VALUES ('2010-02-02', '10:46:17', '31.90' , '32.15','32.24','32.39','33.46','35.18','39.33','39.39','40.92','41.79','41.82','44.35','45.47','46.89','47.13','67.59');
If I copy and paste the statement into the MySQL table, it inserts the date just fine, but when PHP trys to insert it, I am given all 0's. The dispatch_date field is a "date" field in MySQL. Before inserting the dates with the PHP code, the date to insert is converted to a string (I thought this might solve my problem). Can anyone give me any insight on this?
From tizag.com
When you enter dates that are out of
the range or in the wrong format for a
given date type, MySQL will often just
enter in the default value of all
zeros.
You are storing them in the wrong format.
2010-02-02 (%Y-%m-%d) is the default format but it can be changed. Run this query to learn what the values are in your server:
SELECT ##date_format, ##datetime_format;
If they don't match, you'll have to either change your queries or change the ##date_format and ##datetime_format variables. They can be changed in a per-session basis:
SET ##session.date_format='%Y-%m-%d';
SET ##session.datetime_format='%Y-%m-%d %H:%i:%s';

Categories