I am trying to use FullCalendar v4 and cannot post data to MySQL. I have narrowed the problem down to $start and $end having UTC on the end and MySQL won't take it even though my datatype is TIMESTAMP. If I manually assign standard datetime data (without UTC) to $start and $end it will post to table. I have the statements commented out in the event.php that work, by overriding the data in the $POST.
My thought is I have something askew in MySQL that is causing the TIMESTAMP datatype to actually be a DATETIME datatype. I have deleted and created the table with SQL Statement shown below.
Running -> MySQL 8.01.5, Windows Server 2016, PHP 7.2.7, using jQuery
...
CREATE TABLE calendarsystem.events (
id int(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(45) NOT NULL ,
start TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
end TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
resourceId VARCHAR(45) NOT NULL ,
PRIMARY KEY (id)
);
...
The code to add_event.php:
<?php
$title = $_POST['title'];
$start = $_POST['start'];
$end = $_POST['end'];
$resourceId = $_POST['resourceId'];
//$title = 'wtf';
//$start = '2019-03-25 16:00:00';
//$end = '2019-03-25T17:00:00';
//$resourceId = 'b';
try {
require "db_config.php";
} catch(Exception $e) {
exit('Unable to connect to database.');
}
$sql = "INSERT INTO events (title, start, end, resourceId) VALUES (:title, :start, :end, :resourceId )";
$q = $bdd->prepare($sql);
q->execute(array(':title'=>$title,':start'=>$start,':end'=>$end,':resourceId'=>$resourceId));
?>
...
If I open MySQL Workbench and try to add the data with the UTC copied from the output window of chrome I get the following error when applying:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
INSERT INTO calendarsystem.events (start, end, title, resourceId) VALUES ('2019-03-25T14:00:00-05:00', '2019-03-25T15:00:00-05:00', 'xxx', 'b');
ERROR 1292: 1292: Incorrect datetime value: '2019-03-25T14:00:00-05:00' for column 'start' at row 1
SQL Statement:
INSERT INTO calendarsystem.events (start, end, title, resourceId) VALUES ('2019-03-25T14:00:00-05:00', '2019-03-25T15:00:00-05:00', 'xxx', 'b')
Sorry the post formatting is crappy
I think MySQL isn't recognizing the 'T' character or the trailing time offset in the string value.
'2019-03-25T15:00:00-05:00'
^ ^^^^^^
One way to fix the problem would be to remove that T character and the offset
'2019-03-25 15:00:00'
^
We expect MySQL to recognize a string in that format.
Alternatively, we could use STR_TO_DATE function with appropriate format model so MySQL can interpret datetime/timestamp values from strings in different formats.
The relevant topic in the MySQL Referenced Manual is here:
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html
Related
I have a datetime(6) column in MySQL database with fractional seconds. For example, an example entry in my database has the time value: 2022-12-22 12:29:04.602000.
CREATE TABLE `test_data` (
`sampletime_utc` datetime(6) DEFAULT NULL,
`project_uuid` int(11) DEFAULT NULL,
`item_id` int(11) DEFAULT NULL
);
When we read this time in MATLAB, the default type for the time variable is char which looses the fractional seconds part. I found the following solution to the problem:
https://www.mathworks.com/matlabcentral/answers/602656-sql-datetime-query-is-there-a-faster-way?s_tid=prof_contriblnk
The source code in the above answer is given below:
datasourceName = "mysqlJdbc"; % The name of JDBC datasource for MySQL
username = "USERNAME";
password = "PASSWORD";
conn = database(datasourceName, username, password);
opts = databaseImportOptions(conn, tablename);
columnNames = {'col1', 'col2'}; % The column names you want to change the import options
opts = setoptions(opts, columnNames, 'Type', 'datetime'); % Change from char to datetime
sqlquery = "SELECT * FROM YOUR_TABLENAME";
data = fetch(conn, sqlquery, opts, 'MaxRows', 10);
The above solution works only for a simple select statement. If I use a complicated statement like with where and and clauses, the following error is thrown:
Error using database.jdbc.connection/fetch (line 197)
Unable to use database import options with
'SELECT data_value, sampletime_utc FROM db1.tb1 dd WHERE (dd.project_uuid = 'b7d0-cf70b35e32cb' AND dd.item_id = 131 AND dd.sampletime_utc >= '2022-04-22 20:45:52.000' and dd.sampletime_utc <= '2022-04-22 21:45:52.000' ) ORDER BY dd.sampletime_utc ASC'.
Can anyone please guide how to get the time values from mysql table with fractional seconds or as a datetime variable in MATLAB.
I have this field in my model:
created_at = models.DateTimeField(auto_now_add=True)
but when I try to save data to mysql with this command:
INSERT INTO unprocessed(provider_id, record, type ) VALUES (1, 1, 1);
this error appear:
ERROR 1364 (HY000): Field 'created_at' doesn't have a default value
I searched the INTERNET but i couldn't find a way to solve it
Nafees Anwar is right. If your class name is unprocessed, maybe you can use this way like
tmp = dict(provider_id=1, record=1, type=1)
data = unprocessed(**tmp)
data.save()
I have a MySQL table (simplified):
CREATE TABLE `tokens` (
`token` BINARY(16) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And I try to insert a row like this:
JdbcTemplate jdbcTemplate; // org.springframework.jdbc.core.JdbcTemplate
...
String sql = "INSERT INTO `tokens` (`token`) VALUES (?)";
String token = "123e4567e89b12d3a456426655440000"; // UUID
jdbcTemplate.update(sql, new Object[]{token.getBytes()});
But I get this exception:
Data truncation: Data too long for column 'token' at row 1
What do I do wrong? Thanks.
Edit: see my answer, I missed the hexadecimal conversion (token is an UUID).
Here is the solution to store the token (which is an UUID):
jdbcTemplate.update(sql, new Object[]{DatatypeConverter.parseHexBinary(token)});
Let us know if there are other ways...
I'm getting this error, because cakephp 1.3.11 creates an INSERT statement with 'CURRENT_TIMESTAMP' in quotes. Similar thing worked in 1.3.9. What might I be doing wrong?
SQL Error: 1292: Incorrect datetime value: 'CURRENT_TIMESTAMP' for column 'time_posted' at row 1 [CORE\cake\libs\model\datasources\dbo_source.php, line 684]
This is the context query:
$sql = "INSERT INTO `my_table` (`time_posted`, `version`, `provider`, `date`) VALUES ('CURRENT_TIMESTAMP', 0, 'provider', '2011-08-03 16:11:00')"
I'm trying to create a new record in database from cakephp using this code:
class MyTable extends AppModel
{
...
function blah() {
...
$this->create()
$ret=$this->save(array('MyTable'=>array('provider'=>$provider,'date'=>$datetime)));
...
here's the stack:
DboSource::showQuery() - CORE\cake\libs\model\datasources\dbo_source.php, line 684
DboSource::execute() - CORE\cake\libs\model\datasources\dbo_source.php, line 266
DboSource::create() - CORE\cake\libs\model\datasources\dbo_source.php, line 752
Model::save() - CORE\cake\libs\model\model.php, line 1342
MyTable::add() - APP\models\my_table.php, line 1288
As Dunhamzzz said, there must be a place causing CURRENT_TIMESTAMP to be inserted.
Once you find it, you can use date('Y-m-d H:i:s') to save using the current time.
Or, if you want to do it using SQL, you can use DboSource::expression('NOW()').
ie.
array('MyTable'=>array('time_posted' => DboSource::expression('NOW()')))
I'm trying to figure out how I can insert a NULL value into a column of newly inserted row.
I'm working on "post icons" which will display a small icon for the thread on the thread list. The icons use RADIO buttons in the form. By default it is set to No Icon. I was wondering what to do for the value of that radio button to make it null? I tried setting value as NULL but it just inserts the word "NULL" into the database.
$thread_sql = "
INSERT INTO forum_threads (
user_id,
forum_id,
thread_postdate,
thread_lastpost,
thread_title,
thread_description,
thread_icon
) VALUES (
'$_SESSION[user_id]',
'$_GET[f]',
'$date',
'$date',
'$_POST[topictitle]',
'$_POST[topicdescription]',
'$_POST[posticon]'
)
";
$thread_query = #mysqli_query ($db_connect, $thread_sql);
With MySQL:
insert into your_table (nulled_column) values (null);
If you are getting a 'NULL' literal instead of a null value, it's probably because you are not issuing the right command from your client (PHP, C#, Java program). You will need to share the code you are using for your insertion in order to get more help.
UPDATE:
According to your recently edited question, just get rid of the '' when you are inserting NULL values. If you use '', then you'll get 'NULL' literals inserted as you mentioned.
I would also suggest you use PreparedStatements in MySQL in order to avoid SQL injection attacks.
Just do something like this (PDO):
$stmt = $db->prepare('INSERT INTO foo(bar) values (?);');
$stmt->execute(array($value=='NULL'? NULL,$value));
Or with mysql:
mysql_query('INSERT INTO foo(bar)
values ('.($value=='NULL'? 'NULL',"'".mysql_real_escape_string($value)."'").')';
This works. Had to use an IF
if (isset($_POST['submit'])) {
$thread_sql = "
INSERT INTO forum_threads (
user_id,
forum_id,
thread_postdate,
thread_lastpost,
thread_title,
thread_description,
thread_icon
) VALUES (
'$_SESSION[user_id]',
'$_GET[f]',
'$date',
'$date',
'$_POST[topictitle]',
'$_POST[topicdescription]',
IF('$_POST[posticon]'='NULL',NULL,'$_POST[posticon]')
)
";
$thread_query = #mysqli_query ($db_connect, $thread_sql);