mysql date xammp - php

i've set a field as date in xammp server. but when i'm inserting a values, its saving like 0000-00-00.. i've even input it manually like 2010-10-10 but still saveing 000... is the problem with my code or the xammp server??? or is there any way to configure the date format in xammp???
$today = date('Y-m-d');
"/>
update.php
$date = $_GET['datee'];
$qry = "INSERT INTO course_detail(userid, course_id, hours_complete, week_no, date) VALUES('$member_id','$fname','$hour','$week', '$date')";
$result = #mysql_query($qry);

It's probably with your code. Have you quoted the date string when inserting?
INSERT INTO mytable SET datefield='2010-10-10'

This is definitely not a XAMPP issue. I highly recommend you take a look at this link MySQL Documentation on Datetime
MySQL Date has a fixed syntax and it generally falls along the lines of YYYY-MM-DD or YYYYMMDD. I'm sure you're using a presentation layer over MySQL such as *.NET (ASP,winforms), you can reformat the date generated by MySQL to match your locale. Here's how you do it on the Microsoft stack MSDN Globalization Step-by-Step, similar methods are available for whatever else technology you may be using.

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

Date insert php

I guess I have it partially working, but it inserted a random year,day & month rather than the actual date now.
This is in my "Post Article" .php file.
<?php
$date = date('Y-m-d', $timestamp);
What I have setup for my SQL table is this:
however it ends up showing this date:
any ideas?
If you don't pass a second parameters to date() it defaults to now.
$date = date('Y-m-d');
It would be even easier to do this in your SQL (and simultaneously make your PHP code simpler and easier to maintain). You can use several MySQL functions like NOW() and CURDATE():
INSERT INTO tablename (date) VALUES(NOW())
INSERT INTO tablename (date) VALUES(CURDATE())
FYI, I can't say for sure since I can't see your code, but I suspect $timestamp doesn't exist in your code. That will cause you to get the value you see in your database.

Displaying date time with Php and Oracle. Time fails?

I have a small problem. I'm using PHP with Oracle (new to the Oracle by the way).
In my database there's a DATE field called NEXT_START_DATE and it's value is
25.12.2013 04:05:01
as you can see below.
The thing is I can get date values just fine in my web page, but couldn't see anything like a time, if you can see below image, it only returns 25/12/2013.
I know that people suggested to use pl/sql functions like
to_date()
or
to_char()
but is this possbile using just php? I really can't interfere the SQL. Any help would be awesome, Thanks in advance.
In ADOdb, make sure to set the format before connecting:
$db = ADONewConnection("oci8");
// $db->debug = true;
// Date format is set before connecting.
$db->NLS_DATE_FORMAT = 'DD.MM.YYYY HH24:MI:SS';
Courtesy: http://board.issociate.de/thread/192412/OCI_ignoring_NLS_DATE_FORMAT_parameter.html
You can use SQL functions in your queries as well.
For example,
SELECT TO_CHAR(next_start_date, 'YYYY-MM-DD HH24:MI:SS') FROM mytable
and you'll get the date in the format you specified.
As #Maheswaran Ravisankar pointed out, there is NLS_DATE_FORMAT as well, but if you set it, that format is used for all queries (that do not specify to_char). I always use to_char in all my selects, because it allows me to specify an individual format for each query.

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

Changing the date format for MySQL within a p4a field

I am using the p4a application framework and I have build several databases one of which needs to gather the date of a booking, I understand that there Isn't a way to do this through MySQL but I haven't found anything useful on the p4a forums on this so anyone that uses the p4a framework that could help, I would be grateful,
I have my local set as en_GB which sets the date within the p4a field as dd-mm-yyyy but I need it to be yyyy-mm-dd to actually write the data into the database,
the present code for this operation is:
$this->build("p4a_field","date")
->setlabel("Date")
->setType('date')
$location = $this->AreaName->getNewValue();
$date = $this->date->getNewValue();
$merono = $this->merono->getNewValue();
$p4a = p4a::singleton();
$p4a->i18n->autoUnformat($date, "shortdate");
p4a_db::singleton()->query("INSERT INTO meetingrooms(location, date, merono)
VALUES
('$location', '$date', '$merono')");
Any help would be appreciated, I was planning on intercepting the function using afterClick but I need to know the syntax required first.
Thanks,
Steve
If I understand correctly, you have some dates in a non-MySQL-friendly format, but you want to use them for INSERTs, right?
How about letting MySQL convert them for you?
SELECT STR_TO_DATE('31-12-2012','%d-%m-%Y');
-> '2012-12-31'
EDITS:
It looks like you have the date:
$date = $this->date->getNewValue();
so you need to use the formula in your SQL:
p4a_db::singleton()->query("INSERT INTO meetingrooms(location, date, merono)
VALUES
('$location', STR_TO_DATE('$date','%d-%m-%Y'), '$merono')");
I'm not a p4a guy, so hopefully that'll work.
Note that, in most languages, you'll be exposed to SQL injection with code like that. Does p4a cover that for you, or provide for positional parameters?
Good luck.

Categories