1

I have an issue in updating only date in datetime field type in MYSQL.

I am taking the input from the user (dd/mm/yyyy) using PHP and changing only the date using UPDATE mysql statement. But the datetime field show only 0000-00-00 00:00:00.

Below code is only the php file which updates the table after taking the input from php which passes value using XMLHttprequest.

Pl see my code below:

<?php include 'accesscontrol.php'; ?>
<?php
$q4=$_GET["q4"];

$user = $_SESSION['user'];

$date_array = explode( "/", $q4 );
$new = sprintf( '%4d-%02d-%02d', $date_array[2], $date_array[1], $date_array[0] );


$con = mysql_connect("localhost","tst","tst!@#");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

mysql_query("UPDATE INVHDR_edit SET Invdate= concat ('$new', time(Invdate)) WHERE usr='$user'");


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con);
?>

3 Answers 3

2

try this

<?php include 'accesscontrol.php'; 
$q4 = $_GET["q4"];
$user = $_SESSION['user'];
$dateSql = date("Y-m-d H:i:s",strtotime($q4));
$con = mysql_connect("localhost","tst","tst!@#");
if (!$con){
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("UPDATE INVHDR_edit SET Invdate= '$dateSql' WHERE usr='$user'");
if (!mysql_query($sql,$con)){
  die('Error: ' . mysql_error());
}
mysql_close($con);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Even this does't work, it stores this value 1970-01-01 05:30:00 in the field. I think because my $q4 value would be in 04/05/2012 format.
mysql only allows you to insert in Y-m-d H:i:s format in datetime field.
True, if you see my php code above i am formatting the date and then storing it in Y-m-d format
1

Try with this query: UPDATE INVHDR_edit SET Invdate='".$new." 00:00:00' WHERE usr='$user'");

if you need time to append use following.

$new = $new.date(" H:i:s");
mysql_query("UPDATE INVHDR_edit SET Invdate='$new' WHERE usr='$user'");

1 Comment

In your response you have given time as 00:00:00, in that case, it will update 00 instead of keeping the existing time. Pl check
0

Try this ::

UPDATE INVHDR_edit SET Invdate= STR_TO_DATE(Invdate, '%d-%b-%y') WHERE usr='$user'");

If using a variable:

UPDATE INVHDR_edit SET Invdate= STR_TO_DATE(@var1, '%d-%b-%y') WHERE usr='$user'");

5 Comments

I want to take the date value from the variable, in your response, that is missing, kindly check
Tried this, it gives value NULL in the field (datetime)
Correct me if I am wrong, you need to update the column with a variable having a data as date? You dont want the column to be updated with the current time?
Yes you are right only date to be updated and hold the time which is already present in the datetime field.
Please store the date into a variable, and try to print that variable before the query execution, then by seeing the format, I will updated my query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.