Hello community! I've written these functions to get information comming trough an html form and store it in the database, but when I try to do this, the data aren't stored successfully. I was trying to implement different methods to do this but didn't work.
My controller:
const mysqlConnection = require('../database');
getIndex = (req, res) => {
console.log('POST /');
console.log(req.body);
const {
id, solicitante, telefono, mensaje, caracteristicas,
tipoMasa, saborMasa, cobertura, tamano, abono, precio,
horaPedido
} = req.body;
console.log(req.body);
const query = `
SET @id = ?;
SET @solicitante = ?;
SET @telefono = ?;
SET @mensaje = ?;
SET @caracteristicas = ?;
SET @tipoMasa = ?;
SET @saborMasa = ?;
SET @cobertura = ?;
SET @tamano = ?;
SET @abono = ?;
SET @precio = ?;
SET @horaPedido = ?;
CALL addOrEditPedido(@id, @solicitante, @telefono, @mensaje, @caracteristicas, @tipoMasa, @saborMasa,
@cobertura, @tamano, @abono, @precio, @horaPedido);
`
mysqlConnection.query(query, [id, solicitante, telefono, mensaje, caracteristicas, tipoMasa, saborMasa, cobertura, tamano, abono, precio, horaPedido], (err, rows, fields) => {
if(!err){
console.log('done');
res.json({status: 'Order saved!'});
}else{
console.log(err);
}
});
}
module.exports = {getIndex};
procedure:
USE `jean001`;
DROP procedure IF EXISTS `addOrEditPedido`;
DELIMITER $$
USE `jean001`$$
CREATE PROCEDURE `addOrEditPedido` (
IN _id INT,
IN _solicitante VARCHAR(30),
IN _telefono VARCHAR(20),
IN _mensaje VARCHAR(200),
IN _caracteristicas VARCHAR(200),
IN _tipoMasa VARCHAR(30),
IN _saborMasa VARCHAR(30),
IN _cobertura VARCHAR(30),
IN _tamano VARCHAR(20),
IN _abono INT,
IN _precio INT,
IN _horaPedido VARCHAR(5)
)
BEGIN
IF _id = 0 THEN
INSERT INTO pedidos (solicitante, telefono, tipoMasa, saborMasa, cobertura,
tamano, caracteristicas, mensaje, abono, precio, horaPedido, estado)
VALUES (_solicitante, _telefono, _tipoMasa, _saborMasa, _cobertura,
_tamano, _caracteristicas, _mensaje, _abono, _precio, _horaPedido, _estado);
SET _id = LAST_INSERT_ID();
ELSE
UPDATE pedidos
SET
solicitante = _solicitante,
telefono = _telefono,
tipoMasa = _tipoMasa,
saborMasa = _saborMasa,
cobertura = _cobertura,
tamano = _tamano,
caracteristicas = _caracteristicas,
mensaje = _mensaje,
abono = _abono,
precio = _precio,
horaPedido = _horaPedido,
estado = 1
WHERE _id = id;
END IF;
SELECT _id AS id;
END;$$
DELIMITER;
I hope u can help me Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire