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()')))
Related
I am trying to insert some data into my Postgres table. There is a column which includes some json Data. But every time I try to insert it shows some error
Below is my query
INSERT INTO settings.tbl_settings
(sin_product_id,
vhr_sys_module_name,
vhr_grouping_name,
vhr_settings_sys_name,
vhr_label,
vhr_value,
arj_select_items,
txt_remarks,
vhr_widget_type,
sin_settings_category,
int_sys_action_id,
fk_created_user_id,
dtm_created)
VALUES
(1,
'XO_PURCHASE',
'XO Purchase',
'NEED_XO_PURCHASE_SUPPLIER_SIDE_POSTING',
'Need XO Purchase Supplier Side Posting',
'NO_POSTING',
'[{"strSysName":"NO_POSTING","strLabel":"No Posting"} ,{"strSysName":"POSTING","strLabel":"Posting"}]'::JSONB[],
'',
'SELECTBOX',
2,
0,
1,
TO_TIMESTAMP('27-08-2022 17.24.34', 'DD/MM/YYYY HH24:MI:SS')
);
It Returns Given Error
ERROR: malformed array literal: "[{"strSysName":"NO_POSTING","strLabel":"No Posting"} ,{"strSysName":"POSTING","strLabel":"Posting"}]"
LINE 27: '[{"strSysName":"NO_POSTING","st...
^
DETAIL: "[" must introduce explicitly-specified array dimensions.
SQL state: 22P02
Character: 1024
Your mistake is that when you perform the insert operation, you convert the string into a jsonb array (::JSONB[]), but you just need to jsonb, since the type of your arj_select_items column is most likely defined as jsonb.
Demo in dbfiddle
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 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
I was trying to use the ON UPDATE DUPLICATE KEY clause for the first time, following this link
SQL - IF EXISTS UPDATE ELSE INSERT INTO
and I'm getting an error in my sql syntax:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ''AJAY KUMAR')'
at line 2 The SQL being executed was: INSERT INTO fee_acc_balance
(guardian_name, account_no, paid_amount, due, days_overdue,
total_fees, updated_on) VALUES ('AJAY KUMAR', '10', 0, 12550, 0,
12550, '2017-02-10 21:28:05') ON DUPLICATE KEY UPDATE guardian_name =
VALUES ('AJAY KUMAR') Error Info: Array ( [0] => 42000 [1] => 1064 [2]
=> You have an error..
The unique key in my case is account_no, and this is my sql :
INSERT INTO fee_acc_balance (guardian_name, account_no, paid_amount, due, days_overdue, total_fees, updated_on)
VALUES ('$father_name', '$account->account_no', $payments, $sum, 0, $sum,'$now')
ON DUPLICATE KEY UPDATE guardian_name = VALUES ('$father_name')
Where does the error lie?
You cannot specify an absolute value in ON DUPLICATE KEY UPDATE:
ON DUPLICATE KEY UPDATE guardian_name = VALUES ('$father_name')
Try with
ON DUPLICATE KEY UPDATE guardian_name = VALUES(guardian_name)
Notice that the right part of the assignment is the new field coming in from VALUES, and the left side is the extant record; "UPDATE a = VALUES(a)" means "put the a from VALUES into the record", not "leave everything as it is".
Also, you may want to write variables in curly brackets:
...VALUES ('{$father_name}', '{$account->account_no}', {$payments}, {$sum}, 0, {$sum}, '{$now}')
or even better use PREPAREd statement with PDO:
$stmt->prepare("INSERT... VALUES(?, ?, ?, ?, 0, ?, ?)");
$stmt->execute([
$father_name,
$account->account_no,
$payments,
$sum,
$sum,
$now
]);
and, better still, bound parameters.
Otherwise, strange things might happen if the guardian name is Ajay Al'Kumar (note the quote mark) or a string value is passed instead of an integer one.
I am using an Insert trigger on a table where I copy one record from one table to another.
Everything is ok. It works fine except on datetime columns. When I add a datetime column into the inserted values, then I get an error:
Invalid column name
My code is
ALTER TRIGGER [dbo].[Insert_MoinitoringBasic]
ON [dbo].[M0_BasicInfo]
FOR INSERT
AS
BEGIN
MERGE [MonitoringROSCII].[dbo].[MonitorBasicInfo] AS d
USING (SELECT DistrictID, upazilaID, LC_ID, AcademicYear, Trimester, RepID,
CASE VisitType
WHEN 'Initial validation' THEN 1
WHEN 'Full validation' THEN 2
WHEN 'Compliance monitoring' THEN 3
END AS VisitTp
FROM INSERTED) AS s ON s.DistrictID = d.DistrictID
AND s.upazilaID = d.upazilaID
AND s.LC_ID = d.LCID
AND s.AcademicYear = d.LCVisitYr
AND s.Trimester = d.Trimister
AND s.RepID = d.MOID
AND s.VisitTp = d.VisitType
WHEN MATCHED THEN
UPDATE
SET DistrictID = S.DistrictID
WHEN NOT MATCHED THEN
INSERT (DistrictID, UpazilaID, LCID, VisitType, LCVisitYr, Trimister, MOID,
LCStatus, IfCloseWhy, OthersSpecify,LC1stVstDt)
VALUES (DistrictID, UpazilaID, Lc_ID, VisitTp, AcademicYear, Trimester, RepId,
2, 'No', 'No', FirstVisitDate);
END
Here the last line FirstVisitDate which is a datetime column. Without this column, it worked nice but when I include this column, it shows the error mentioned above.
Can anybody help me with this?
Thanks
As the error text suggests, you are refering to a column that does not exist.
In the SELECT part of your MERGE statement, there is no output column named FirstVisitDate, which is why you're getting the error...