0

I am trying to update a row in my table where the date in a column is equal to today's date.

I have today's date in a variable $currentDate and when I echo this out it is displayed on the screen in the following format

2020-05-08

Which looks same format as the Db table, but I still get the error invalid datetime format.

Below is the code I'm using. Any help please

$currentDate = date("Y-m-d");
$currentTemp = 33;

echo $currentDate;

$sql = "UPDATE weather_station SET currentTemp = $currentTemp WHERE date = $currentDate";
$stmt = $pdo->prepare($sql);
$stmt->execute();
  • 3
    Use parameterized queries. – sticky bit May 08 '20 at 10:36
  • 1
    You are using the right method with prepared statements but you still inject raw variables value in your query instead of using parmeters. Please see [this page](https://www.w3schools.com/php/php_mysql_prepared_statements.asp) that explains how to use parameters. – Noah Boegli May 08 '20 at 10:36
  • 2
    2020 minus 5 minus 8 = 2007 – Strawberry May 08 '20 at 10:39
  • https://stackoverflow.com/questions/3822648/how-do-i-query-between-two-dates-using-mysql – Random Dude May 08 '20 at 10:40
  • Yes it was parameters, I though I had used this and it was still not working but must made a mistake somewhere –  May 08 '20 at 10:44

1 Answers1

1
$sql = "UPDATE weather_station SET currentTemp = :temp WHERE date = :currenDate";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":temp", $currentTemp);
$stmt->bindParam(":currenDate", $currentDate);
$stmt->execute();

This works now