Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert query using prepared-statement in mysql?

I have an stored procedure like this:

SET @query = CONCAT('insert into tblcommodity (id , idname , count)values (',p1, p2,p3,')');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

But when I want to run it, it has this error:

> Procedure execution failed 1136 - Column count doesn't match value
> count at row 1

and another thing is that when I just run the insert code it runs!

plz help me.

like image 272
Fatemeh Gharri Avatar asked Dec 07 '22 10:12

Fatemeh Gharri


2 Answers

With all due respect, the way you do it kindof defeats the whole purpose of prepared statements. I'd use this form instead:

SET @query = 'INSERT INTO tblcommodity (id, idname, count) VALUES (?, ?, ?)';
PREPARE stmt FROM @query;
EXECUTE stmt USING @p1, @p2, @p3;
like image 83
raina77ow Avatar answered Jan 13 '23 01:01

raina77ow


It should be

SET @query = CONCAT('insert into tblcommodity (id , idname , count)values (',',',p1,',', p2,',',p3,')');
like image 23
Ankur Avatar answered Jan 13 '23 01:01

Ankur