Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Input into MySQL - Date field

Guest
Aug 14, 2009 Aug 14, 2009

Hi everyone,

I've got a simple form with a date input field, the data goes into a MySQL database, column formatted to 'DATE'.

GetSQLValueString($_POST['projstart'], "date"),

However, when I try to submit the form,it tells me:

Incorrect date value: '04/08/2009' for column 'projstart' at row 1

I'm guessing this is due to '04/08/2009', being viewed as a string, not a date.

How do I change this to input into the database, as a DATE.???

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO tbl_projects (projtitle, projstart, projdue, FK_custid, FK_langsid, projcontact, projanalysis, projtype, projstatus, projurl) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['projtitle'], "text"),
                       GetSQLValueString($_POST['projstart'], "text"),
                       GetSQLValueString($_POST['projdue'], "text"),
                       GetSQLValueString($_POST['FK_custid'], "int"),
                       GetSQLValueString($_POST['FK_langsid'], "text"),
                       GetSQLValueString($_POST['projcontact'], "text"),
                       GetSQLValueString($_POST['projanalysis'], "text"),
                       GetSQLValueString($_POST['projtype'], "text"),
                       GetSQLValueString($_POST['projstatus'], "text"),
                       GetSQLValueString($_POST['projurl'], "text"));

TOPICS
Server side applications
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Aug 16, 2009 Aug 16, 2009

Before the date gets inserted/updated, use this function:

function mysqlDate($date) {

$parts = explode('/',$date);

return "$parts[2]-$parts[1]-$parts[0]";

}

$_POST['date'] = mysqlDate($_POST['date']);

Translate
LEGEND ,
Aug 16, 2009 Aug 16, 2009

I'm guessing this is due to '04/08/2009', being viewed as a string, not a date.

No, a date is inserted into MySQL as a string, so that's not your problem. MySQL stores dates in one format only: the ISO-recommended format YYYY-MM-DD. You need to reformat the date as 2009-08-04 (assuming you mean 4 August),

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 16, 2009 Aug 16, 2009

Hi David,

I thought as much.

I can reformat my calendar picker to become: yyyy-mm-dd, which gets accepted by MySQL.

Is there a way of keeping the calendar picker as dd-mm-yyyy, so it looks correct when people are entering the dates, then converting it, either through PHP or a MySQL statement, which enters it in the database as yyyy-mm-dd?

Thanks for your help

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 16, 2009 Aug 16, 2009

Before the date gets inserted/updated, use this function:

function mysqlDate($date) {

$parts = explode('/',$date);

return "$parts[2]-$parts[1]-$parts[0]";

}

$_POST['date'] = mysqlDate($_POST['date']);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 17, 2009 Aug 17, 2009

Sorry for being dumb, but where in my insert query, would it go, where the two date fields are projstart and projdue?

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO tbl_projects (projtitle, projstart, projdue, FK_custid, FK_langsid, projcontact, projanalysis, projtype, projstatus, projurl) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['projtitle'], "text"),
                       GetSQLValueString($_POST['projstart'], "text"),
                       GetSQLValueString($_POST['projdue'], "text"),
                       GetSQLValueString($_POST['FK_custid'], "int"),
                       GetSQLValueString($_POST['FK_langsid'], "text"),
                       GetSQLValueString($_POST['projcontact'], "text"),
                       GetSQLValueString($_POST['projanalysis'], "text"),
                       GetSQLValueString($_POST['projtype'], "text"),
                       GetSQLValueString($_POST['projstatus'], "text"),
                       GetSQLValueString($_POST['projurl'], "text"));

  mysql_select_db($database_conndb2, $conndb2);
  $Result1 = mysql_query($insertSQL, $conndb2) or die(mysql_error());

Thanks,

Samuel

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 17, 2009 Aug 17, 2009

I added your function, but got this error when submitting the form:

Incorrect date value: '--' for column 'projstart' at row 1

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 17, 2009 Aug 17, 2009

I added your function, but got this error when submitting the form:

Incorrect date value: '--' for column 'projstart' at row 1

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 17, 2009 Aug 17, 2009

Hey man,

Just tested the function, which worked, thanks!

Only problem is, I've repeated the function for each date input on the form, but some of them are not required, and now I cannot submit the form.

How do I correct this?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 17, 2009 Aug 17, 2009
LATEST

insert function after if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

you could wrap the function in an if else statement so that it checks whether the value is null or not before running the function.

if ($_POST['date'] != "") {

function mysqlDate($date) {

$parts = explode('/',$date);

return "$parts[2]-$parts[1]-$parts[0]";

}

$_POST['date'] = mysqlDate($_POST['date']);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines