Date via PHP get data won't insert into MySQL database after conversion to date

Viewed 54

The date coming into the page is in the following format: 2022-09-15 23:53:21

I made sure to format it correctly externally, so tried inserting it directly as it was, but no insert happened.

The mysql database I am inserting to is a Linode managed database, therefore I don't have access to the error logs.

Trying to load the page directly via the web and print the error to the screen fails as the page just gives a 500 error when the part to insert the date is in the sql query. So I am not able to access what the error is that way.

I researched a lot on this website to try and find the issue and found many answers saying the string needs to be formatted as a date, so below is the multiple ways I tried to convert to a date format.

Code I have tried:

$date = date_create(trim(mysqli_real_escape_string($conn,$_GET["d"])));
$sql = "UPDATE `users` SET `webID` = '$webID', `webIDdate` = '$date' WHERE `userID` = '$uuid'";
$conn->query($sql);
$date = date('Y-m-d H:i:s', strtotime(trim(mysqli_real_escape_string($conn,$_GET["d"]))));
$sql = "UPDATE `users` SET `webID` = '$webID', `webIDdate` = '$date' WHERE `userID` = '$uuid'";
$conn->query($sql);
$date = trim(mysqli_real_escape_string($conn,$_GET["d"]));
$sql = "UPDATE `users` SET `webID` = '$webID', `webIDdate` = STR_TO_DATE($date, '%Y-%m-%d %h:%i:%s') WHERE `userID` = '$uuid'";
$conn->query($sql);

I have confirmed that the record being written to exists, the webID will insert on it's own if the parts for the date are removed from the query, but the date part causes the whole insert to fail.

EDIT: I decided to change the table structure to make the 'webIDdate' column varchar, just to conclusively check there was no naming issues anywhere, and the data inserted fine. I then switched the column back to being a datetime and the data is now inserting.

I still don't understand why it wasn't inserting previously, as I changed nothing else except the data type of the column, then changed it back again.

I am not sure why the suggested duplicate has been given as it doesn't even closely resemble my question.

0 Answers
Related