Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am trying to insert the data in table using node js. but it is not formatted.

What I have tried:

below is insert statement :

INSERT INTO table1 (col1, col2, col3) values ('" + req.body.aa + "', '" + req.body.bb + "', '" + req.body.cc + "')";


i checked in sql probiler and i found that its creating query like below:

INSERT INTO table1 (col1, col2, col3) values (2,aa,bb)


actually it should be like below:
INSERT INTO table1 (col1, col2, col3) values ('2','aa','bb')


can any one please help..

Thanks....
Posted
Updated 17-Dec-18 22:19pm

1 solution

Quote:
can any one please help..

No, we can't really help you on this because
JavaScript
INSERT INTO table1 (col1, col2, col3) values ('" + req.body.aa + "', '" + req.body.bb + "', '" + req.body.cc + "')";

is creating
JavaScript
INSERT INTO table1 (col1, col2, col3) values ('2','aa','bb')

If you don't, something else happen in your code, but we can't guess.

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900