Estoy intentando guardar en una tabla utilizando $wpdb->insert
, mi código es este:
$wpdb->insert('ordenes',
array(
'user_id'=>11,
'seller_id'=>22,
'site_id'=>'USA',
'id_orden'=>333,
'items_id'=>444,
'titulo'=>'Prueba',
'cantidad'=>1,
'currency_id'=>'USD',
'precio'=>10,
'net_received_amount'=>8,
'date_created'=>'20-3-2019',
'date_closed'=>'20-3-2019',
'last_updated'=>'20-3-2019',
'expiration_date'=>'20-3-2019',
'order_status'=>'paid',
'total_amount'=>10,
'total_amount_with_shipping'=>10,
'paid_amount'=>10,
));
Para mi sorpresa se guarda 0 en site_id
. Agrego:
$wpdb->show_errors();
$wpdb->print_error();
Y me devuelve esto:
Error en la base de datos de WordPress: []
INSERT INTO `ordenes` (`user_id`, `seller_id`, `site_id`, `id_orden`,
`items_id`, `titulo`, `cantidad`, `currency_id`, `precio`,
`net_received_amount`, `date_created`, `date_closed`, `last_updated`,
`expiration_date`, `order_status`, `total_amount`,
`total_amount_with_shipping`, `paid_amount`) VALUES (11, '22', 0, '333',
'444', 'Prueba', '1', 'USD', '10', '8', '20-3-2019', '20-3-2019', '20-3-2019',
'20-3-2019', 'paid', '10', '10', '10')
El único campo que me guarda mal es site_id
. En la tabla es un varchar
de 3, igual que currency_id
.
Lo cursioso es que lo guarda perfectamente cuando hago:
$wpdb->get_results("INSERT into ordenes (user_id, seller_id, site_id, id_orden, items_id, titulo, cantidad, currency_id, precio, net_received_amount, date_created, date_closed, last_updated, expiration_date, order_status, total_amount, total_amount_with_shipping, paid_amount) values ('1', '22', 'USA', '3', '4', 'Prueba', '1', 'USD', '10', '10', '20-3-2019', '20-3-2019', '20-3-2019', '20-3-2019', 'paid', '10', '10', '10')");
Al hacer SHOW CREATE TABLE ordenes
me devuelve:
CREATE TABLE `ordenes` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`seller_id` int(11) NOT NULL,
`site_id` varchar(3) NOT NULL,
`id_orden` bigint(20) NOT NULL,
`items_id` varchar(15) NOT NULL,
`titulo` varchar(60) NOT NULL,
`cantidad` int(2) NOT NULL,
`currency_id` varchar(3) NOT NULL,
`precio` float(10,2) NOT NULL,
`net_received_amount` float(10,2) DEFAULT NULL,
`date_created` datetime NOT NULL,
`date_closed` datetime NOT NULL,
`last_updated` datetime NOT NULL,
`expiration_date` datetime NOT NULL,
`order_rating` varchar(10) NOT NULL,
`order_rating_message` text DEFAULT NULL,
`order_status` varchar(18) NOT NULL,
`total_amount` float(10,2) NOT NULL,
`total_amount_with_shipping` float(10,2) NOT NULL,
`paid_amount` float(10,2) NOT NULL,
`orden_md5` char(32) DEFAULT NULL,
`notificado` varchar(1) DEFAULT NULL,
`entregado` datetime DEFAULT NULL,
`regalo_enviado` varchar(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1
¿Como puedo solucionar este error?. Gracias!
SHOW CREATE TABLE ordenes;
?INSERT
que le mandas es exactamente el que presentas al inicio de la pregunta (usando valores directamente) o bien estás usando variables para setear los valores?¿Si intentas elINSERT
directamente en el manejador de BD ocurre lo mismo, te pone ese campo en0
?TRIGGER
que cambie el valor a0
. ¿Si intentas otro valor, como por ejemploAAA
también lo cambia por0
?