I'm fairly new to MySQL and I've searched around as much as I can, but I was hoping someone could provide me with a definitive answer.
I've been looking at issues with a stored procedure in my MySQL database. It takes a single string parameter from an external source as a search value and returns results based on that. However, whenever a ' (single quote/apostrophe) is typed, it returns an error. Looking into it, I was advised that the SP as it is is vulnerable to SQL injection and that by resolving that the issues with the single quote will be resolved. This is the stored procedure:
BEGIN
SET @t1=CONCAT("SELECT `ID` as `Id`, `NAME` as `Name` FROM SEARCH_TABLE WHERE `NAME` like","'",searchString,"%'"," LIMIT 20");
PREPARE stmt3 FROM @t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END
Everything I've looked at says that the best way to avoid SQL injection is to use prepared statements, so is it still at risk? Additionally, to resolve the issue with the single quote, I've seen recommendations to add another single quote wherever one is typed (so ' will become ''). Is this the best way to fix the error?
Thanks for the help