php - MySQL: Unknown column 'price_USD' in 'field list' - Update operation - correct query (it runs on phpMyAdmin)
On an update query under Codeigniter framework, I got this error:
Query error: Unknown column 'price_USD' in 'field list' - Invalid query: UPDATE
offers_prices_history
SETprice
= '37',price_per_unit_of_active_ingredient
= 0.061666666666667,unit_of_measure
= 'GBP / mg',currencies_id
= '23',price_USD
= 46.93339,price_per_unit_of_active_ingredient_USD
= 0.078222316666667,last_modified
= '2019-06-11 18:44:38' WHEREid
= '21'
If I copy and paste the entire query in phpmyadmin and run it, it works properly.
Here the DUMP of the used table:
CREATE TABLE `offers_prices_history` (
`id` int(11) UNSIGNED NOT NULL,
`offers_id` int(11) UNSIGNED NOT NULL,
`price` float NOT NULL,
`price_per_unit_of_active_ingredient` float UNSIGNED NOT NULL,
`price_USD` float DEFAULT NULL,
`price_per_unit_of_active_ingredient_USD` float DEFAULT NULL,
`unit_of_measure` varchar(255) NOT NULL,
`currencies_id` int(11) UNSIGNED NOT NULL,
`creation` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dump dei dati per la tabella `offers_prices_history`
--
INSERT INTO `offers_prices_history` (`id`, `offers_id`, `price`, `price_per_unit_of_active_ingredient`, `price_USD`, `price_per_unit_of_active_ingredient_USD`, `unit_of_measure`, `currencies_id`, `creation`, `last_modified`) VALUES
(6, 16, 25, 0.0416667, NULL, NULL, 'GBP / mg', 23, '2019-06-11 11:26:15', '2019-06-11 11:26:15'),
(7, 16, 26, 0.0433333, NULL, NULL, 'GBP / mg', 23, '2019-06-11 11:28:03', '2019-06-11 11:28:03'),
(8, 16, 27, 0.045, NULL, NULL, 'GBP / mg', 23, '2019-06-11 11:28:46', '2019-06-11 11:28:46'),
(9, 16, 28, 0.0466667, NULL, NULL, 'GBP / mg', 23, '2019-06-11 11:37:58', '2019-06-11 11:37:58'),
(10, 16, 29, 0.0483333, NULL, NULL, 'GBP / mg', 23, '2019-06-11 11:45:53', '2019-06-11 11:45:53'),
(11, 16, 30, 0.05, NULL, NULL, 'GBP / mg', 23, '2019-06-11 11:52:10', '2019-06-11 11:52:10'),
(12, 16, 31, 0.0516667, NULL, NULL, 'GBP / mg', 23, '2019-06-11 11:53:16', '2019-06-11 11:53:16'),
(13, 16, 32, 0.0533333, 40.591, 0.0676517, 'GBP / mg', 23, '2019-06-11 15:42:19', '2019-06-11 15:42:19'),
(14, 16, 32, 0.0533333, 40.591, 0.0676517, 'GBP / mg', 23, '2019-06-11 15:44:17', '2019-06-11 15:44:17'),
(15, 16, 32, 0.0533333, 40.591, 0.0676517, 'GBP / mg', 23, '2019-06-11 15:45:12', '2019-06-11 15:45:12'),
(16, 16, 33, 0.055, 41.8595, 0.0697659, 'GBP / mg', 23, '2019-06-11 15:56:07', '2019-06-11 15:56:07'),
(17, 16, 34, 0.0566667, 43.128, 0.07188, 'GBP / mg', 23, '2019-06-11 16:33:30', '2019-06-11 16:33:30'),
(18, 16, 33, 0.055, 41.8595, 0.0697659, 'GBP / mg', 23, '2019-06-11 17:30:50', '2019-06-11 17:30:50'),
(19, 16, 33, 0.055, 41.8595, 0.0697659, 'GBP / mg', 23, '2019-06-11 17:54:22', '2019-06-11 17:54:22'),
(20, 16, 35, 0.0583333, 44.3965, 0.0739941, 'GBP / mg', 23, '2019-06-11 18:15:37', '2019-06-11 18:28:04'),
(21, 16, 36, 0.06, 45.6649, 0.0761082, 'GBP / mg', 23, '2019-06-11 18:33:47', '2019-06-11 18:33:47');
--
-- Indici per le tabelle scaricate
--
--
-- Indici per le tabelle `offers_prices_history`
--
ALTER TABLE `offers_prices_history`
ADD PRIMARY KEY (`id`),
ADD KEY `ref_currency_id_idx` (`currencies_id`),
ADD KEY `offers_prices_history_to_vendors_offers_idx` (`offers_id`);
--
-- AUTO_INCREMENT per le tabelle scaricate
--
--
-- AUTO_INCREMENT per la tabella `offers_prices_history`
--
ALTER TABLE `offers_prices_history`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=22;
--
-- Limiti per le tabelle scaricate
--
--
-- Limiti per la tabella `offers_prices_history`
--
ALTER TABLE `offers_prices_history`
ADD CONSTRAINT `offers_prices_history_to_vendors_offers_idx` FOREIGN KEY (`offers_id`) REFERENCES `phar263d_PharmaComparisonDB`.`vendors_offers` (`id`);
COMMIT;
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */;
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */;
/*!40101 SET [email protected]_COLLATION_CONNECTION */;
The function that performs the update simply use an associative array
function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table_name, $data);
}
and the array is populated as stated in the error message. I cannot understand what attempt to do since the error shows a correct query, the column that gives an error is existing. I searched for similar posts, read a lot with the same title, but not found similar trouble that I can summarize in this way
- update operation
- not existing column error for an existing column
- column names correctly quoted in the query string
- the query shown in the error message runs correctly when copied and pasted in phpMyAdmin
Answer
Solution:
in set price='37' try to this SET price=37.