Answer the question
In order to leave comments, you need to log in
How to insert json into mysql?
When adding an array converted to a string to a database, the server crashes. The server is made on Node.js.
Here is an example code:
T[0]=1;
T[1]=2;
T[2]="3";
T[3]=4;
T[4]="5";
T=JSON.stringify(T);
connection.query('INSERT INTO `tasks` (`login`,`description`,`type`,`time`) VALUES ("'+req.body.login+'","'+T+'","'+"3"+'","'+st+'")');
Answer the question
In order to leave comments, you need to log in
0) It is better to frame the SQL query string with double quotes in order to safely use single quotes inside the query to denote text constants.
1) Study the documentation for the module you are using, everything is described in detail and accessible there.
2) Don't insert parameters directly into the request. Never .
var T = [1, 2, '3', 4, 5];
connection.query("INSERT INTO `tasks` SET ?", {
login: req.body.login,
description: JSON.stringify(T),
type: 3,
time: new Date()
}, function(err) {
if (err) throw err;
});
var T = [1, 2, '3', 4, 5];
connection.query("INSERT INTO ?? SET ?", [
'tasks',
{
login: req.body.login,
description: JSON.stringify(T),
type: 3,
time: new Date()
}
], function(err) {
if (err) throw err;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question