Here is my code for updating the date field for all records that have the field WorkoutID = $currentWorkoutID. When I run the query the dates change to 0000-00-00 and not to the current date. How can I fix this? Btw DB::getInstance() executes the query. I think something is wrong with the actual query?
$currentWorkoutID = $_SESSION['GlobalWorkoutID'];
echo $currentWorkoutID;
$date = date("y/m/d");
echo $date;
$sql = "UPDATE workout SET Date = ".$date." WHERE WorkoutID = ".$currentWorkoutID."";
DB::getInstance()->query($sql);
include single quotes ' for date
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
^ ^
i agrre with the answer
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
because maybe the type of Date is String ,
hope to help you,thank you
Try this
$sql = "UPDATE workout SET `Date` = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
Because date is a reserved keyword
Related
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();
$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
This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 1 year ago.
$NOW = new DateTime();
$date = $NOW->format('Y-m-d'); // return 2018-05-17
I want to update date in database to now date.
Query:
$sql = "UPDATE table SET date = $date WHERE id = $id";
But it update time like this => 0000-00-00
The type of this column is DATE
Why? what I have done wrong?
You can try mysql now() function to update date.
$sql = "UPDATE table SET date = now() WHERE id = $id";
Or you should add single qoutes in query
$sql = "UPDATE table SET date = '".$date."' WHERE id = $id";
Simply add your date value in quotes then try. Change your query to :
$sql = "UPDATE table SET date = '$date' WHERE id = $id";
I don't know why this is happening but I have already quoted my date and everytime I run this code, instead of updating dates, it's just subtracting them and returning:
0000-00-00
Here is my code:
$format_date = date("Y-m-d", strtotime($date));
$newinfo = "UPDATE
tickets
SET
`date` = '".mysqli_real_escape_string($connct,$format_date)."'
AND `boolean` = '".mysqli_real_escape_string($connct,$booleancheck)."'
WHERE
`id` = '".mysqli_real_escape_string($connct,$id)."'
";
$update_this = mysqli_query($connct, $newinfo);
EDIT:
Thanks to #Barmar, I used ,instead of AND and it worked.
$newinfo = "UPDATE
tickets
SET
`date` = '".mysqli_real_escape_string($connct,$formatar_data)."',
`boolean` = '".mysqli_real_escape_string($connct,$booleancheck)."'
WHERE
`id` = '".mysqli_real_escape_string($connct,$id)."'
";
I am having a problem inserting DATETIME into MySQL using PHP.
First, I am fetching a date from the news table:
$statement = $db->query("SELECT utcPublishedDate FROM News WHERE idNews = $newsID");
$row = $statement->fetch(PDO::FETCH_ASSOC);
$utcPublishedDate = $row["utcPublishedDate"];
Then I am doing some date manipulation on that date due to some code requirements, eventually this date is changing to something else.
Now, I have to update the table, so:
$statement = $db->query("UPDATE News SET utcPublishedDate = $utcPublishedDate WHERE idNews = $newsID");
This is causing an error. If I do this:
$statement = $db->query("UPDATE News SET utcPublishedDate = NOW() WHERE idNews = $newsID");
The statement will execute without a problem.
Now, how do I solve the error I am getting? Obviously It is something to do with the fact there are spaces in my DATETIME field. Suggestions please? Thanks.
try changing:
$statement = $db->query("UPDATE News SET utcPublishedDate = $utcPublishedDate WHERE idNews = $newsID");
to:
$statement = $db->query("UPDATE News SET utcPublishedDate = '$utcPublishedDate' WHERE idNews = $newsID");
Convert the DATETIME via php first so that it will be Mysql valid datetime. Use this:
$utcPublishedDate = date('Y-m-d H:i:s', strtotime($utcPublishedDate)); $statement = $db->query("UPDATE News SET utcPublishedDate = '$utcPublishedDate' WHERE idNews = $newsID");
Cheers
I have this table :
I would like to delete same rows. For example first five rows are the same, my table should have only one row that includes this data : 40.792274 29.412994 2011-12-21 17:19:52.
So I used the following code :
$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$date = $row['date'];
$lat = $row['latitude'];
$lon = $row['longitude'];
$query = "SELECT * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon'";
$re = mysql_query($query);
$number = mysql_num_rows($re);
$number--;
$query = "DELETE * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon' LIMIT $number";
mysql_query($query);
}
But this code doesn't work.. What should I do ?
Edited :
I solved my question :
$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$date = $row['date'];
$lat = $row['latitude'];
$lon = $row['longitude'];
$query = "SELECT * FROM table WHERE date='$date' AND latitude=$lat AND longitude=$lon";
$re = mysql_query($query);
$number = mysql_num_rows($re);
$number--;
$query = "DELETE FROM table WHERE date='$date' AND latitude=$lat AND longitude=$lon LIMIT $number";
mysql_query($query);
}
Query lines were incorrect in my first question.
To remove the duplicate elements, you would use something like this:
$q = "SELECT date FROM table GROUP BY date"
$r = mysql_query($r);
$date = '';
while($row = mysql_fetch_array($r)){
$date = $row['date'];
$q = "SELECT date FROM mytable WHERE date='$date'";
$re = mysql_query($q);
$num = mysql_num_rows($re);
$num--;
$q = "DROP FROM mytable WHERE date='$date' LIMIT $num";
mysql_query($q);
}
Should do the trick. More specifically, when creating your $date value, you have to provide PHP with a time to use. date() defaults to using the current time, but you can provide it with a custom time as the second argument.
I suggest you take a look at the strtotime() manual at php.net as well (To translate times in your db to timestamps that can be used with date() ).
EDIT: The Answer above has been edited to remove all duplicate entries.
Try changing $dateOfNewData = date('Y-m-d H:i:s');
to
$dateOfNewData = date('Y-m-d 00:00:00'); //or change the first 00 to H if you need it to match by hour, second 00 to i if you need to match minutes and the same with seconds.
or $dateOfNewData = date('Y-m-d') which is pretty much the same and works with datetime field types
And you also need to modify your query to something like this unless you need an exact time:
"SELECT * FROM mytable WHERE date = '$dateOfNewData'" // you might also want the end date if you're working with the past in your database.
Well you can try like "Ignas" suggest but you cal also try this:
First just get the date (year, month, day) without hour, minutes and seconds. If you use full date format then you need to match exactly the same time. (to second the same) which is not really what you are looking for i guess. So you can use this:
$dateOfNewData = date('Y-m-d'); //just get year, month, day in right format (2011-12-20)
Then run a query. Here you have more options but i think the easier is something like that:
"SELECT * FROM mytable WHERE date_col LIKE '$dateOfNewData%' GROUP BY date_col"
This will group the same dates together and will display just once and will match all the rows where 'date_col starts with example: 2011-12-20% (thats why i use LIKE and $dateOfNewData%)
$dateOfNewData contains current date in this format:year-month-day (2011-12-20) and in Mysql query dont forget to use % at the end of the date. It's like * in windows for example.
'mytable' replace with your table name and 'date_col' with date column.
date() you have used will give current date time , so try to use mktime() to get extact date time you want.
you have to change your query little bit, I have modified query below,
$query = mysql_query("SELECT * FROM mytable WHERE date='$dateOfNewData'");
In mysql Date or datetime coulmn should be within ''.