I try to create a stored procedure through php mysqli. I create another stored procedures and I didn´t have problems. This code:
$link = new mysqli("localhost", "root", "", "Question");
$link -> multi_query("DROP PROCEDURE IF EXISTS FacturaTerminarFactura;
CREATE DEFINER=`root`@`%` PROCEDURE `FacturaTerminarFactura`(IN
id_factura INT(40), Total_factura DOUBLE, Total_IVA DOUBLE)
BEGIN
UPDATE `tb_factura`
SET
`tb_factura`.`intId_factura_resolucion_dian` = (SELECT
MAX(`tb_resolucion_dian`.`intFactura_Actual`) FROM `tb_resolucion_dian`
WHERE `tb_resolucion_dian`.`intEstado` = 1 LIMIT 1),
`tb_factura`.`dblTotal_factura` = Total_factura,
`tb_factura`.`dblIva` = Total_IVA
WHERE `tb_factura`.`intid_factura` = id_factura;
UPDATE `tb_resolucion_dian`
SET `tb_resolucion_dian`.`intFactura_Actual` =
tb_resolucion_dian`.`intFactura_Actual` + 1
WHERE `tb_resolucion_dian`.`intEstado` = 1;
SELECT `tb_factura`.`intId_factura_resolucion_dian` AS ID
FROM `tb_factura`
WHERE `tb_factura`.`intid_factura` = id_factura;
END;");
$log = "";
if ($mysqli -> warning_count) {
if ($result = $link -> query("SHOW WARNINGS;")) {
$row = $result -> fetch_row();
$log .= $row[0]."-".$row[1]."-".$row[2]."
";
$result -> close();
}
}
This is the error
"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 '`.`intFactura_Actual` 1 WHERE
`tb_resolucion_dian`.`intEstado` = 1; ' at line 14
I see that the '+' is the problem, but I don't know how I can resolve.
Thanks and excuse me for my English.
You have a typo Here:
tb_resolucion_dian`.`intFactura_Actual` + 1
You're missing the backtick before tb_resolucion_dian
Also, according to mysql docs for UPDATE you don't need to name the table name in the SET clause. The example given was UPDATE t1 SET col1 = col1 + 1; where you have UPDATE t1 SET t1.col1 = t1.col1 + 1, which might also cause issues.
An alternative to UPDATE is INSERT INTO ... SELECT.
simple fix try this
`intFactura_Actual` + 1
TO
`intFactura_Actual` = `intFactura_Actual` + 1
Thanks for comments I see i did not read the code right (Looks so weird here) there is a spelling mistake in the code just like the other guy said.