admin 管理员组文章数量: 1086019
I've done some research and I'm getting conflicting answers, so here it goes. Do you have to pass JS variables into PHP first or can you insert them directly into a MySQL table? I have this running in node.js, thanks!
var part = [];
var des = [];
var price = [];
var request = require('request');
var cheerio = require('cheerio');
var mysql = require('mysql');
var con = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "",
database: "parts"
});
request('URL', function(error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
getPartNumber();
getDescription();
getPrice();
con.connect(function(err) {
if (err) throw err;
for (var i = 0; i<part.length; i++){
var sql = "INSERT INTO data_9_17 (partNumber, description, price) VALUES (part[i], des[i], price[i])";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted, ID: " + result.insertId);
});
}
});
function getPartNumber() {
$("input[name = 'sku']").each(function() { part.push($(this).val()) });
}
function getDescription() {
$(".ellipsis_text").each(function() { des.push($(this).text()) });
}
function getPrice() {
$(".sellprice, .dbl").each(function() { price.push($(this).text()) });
}
}
});
I've done some research and I'm getting conflicting answers, so here it goes. Do you have to pass JS variables into PHP first or can you insert them directly into a MySQL table? I have this running in node.js, thanks!
var part = [];
var des = [];
var price = [];
var request = require('request');
var cheerio = require('cheerio');
var mysql = require('mysql');
var con = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "",
database: "parts"
});
request('URL', function(error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
getPartNumber();
getDescription();
getPrice();
con.connect(function(err) {
if (err) throw err;
for (var i = 0; i<part.length; i++){
var sql = "INSERT INTO data_9_17 (partNumber, description, price) VALUES (part[i], des[i], price[i])";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted, ID: " + result.insertId);
});
}
});
function getPartNumber() {
$("input[name = 'sku']").each(function() { part.push($(this).val()) });
}
function getDescription() {
$(".ellipsis_text").each(function() { des.push($(this).text()) });
}
function getPrice() {
$(".sellprice, .dbl").each(function() { price.push($(this).text()) });
}
}
});
Share
Improve this question
edited Sep 8, 2017 at 17:46
J Johnson
1451 silver badge15 bronze badges
asked Sep 8, 2017 at 16:56
Garrett PeGarrett Pe
311 gold badge3 silver badges10 bronze badges
2
- If you are on Node, you can do it directly. – Hackerman Commented Sep 8, 2017 at 16:58
- But not that way...remember that with node you have server side javascript... – Hackerman Commented Sep 8, 2017 at 16:58
3 Answers
Reset to default 3your query string should go like this:
"INSERT INTO data_9_17 (partNumber, description, price) VALUES ('" + part[i] + "', '" + des[i] + "', '" + price[i] + "')";
You can concenate values by using +
. Check this out.
Edit: I made a mistake. Didn't put '
in. Fixed now...
Anyways, J Johnson is right. It's better to use Prepared Statements because of SQL Injection.
You can use prepared statement, like this:
var query = 'INSERT INTO data_9_17 (partNumber, description, price) VALUES (?, ?, ?)';
con.query(query, [part[i], des[i], price[i]], function(err, results) ... )
// You will put your variables inside []
If you want more information, refer to the mysql
npm module:
https://www.npmjs./package/mysql#escaping-query-values
You need to concatenate your query string with the values of the variables:
var sql = "INSERT INTO data_9_17 (partNumber, description, price) VALUES (" + part[i] + ", " + des[i] + ", " + price[i] + ")";
For security reasons, I remend you to use the prepared statement:
var sql = mysql.format("INSERT INTO data_9_17 (partNumber, description, price) VALUES (?, ?, ?)", (part[i], des[i], price[i]));
con.query(sql, function (err, result) {
.....
.....
.....
}
本文标签: jqueryHow to pass Javascript variables in MySQL queryStack Overflow
版权声明:本文标题:jquery - How to pass Javascript variables in MySQL query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744085487a2531045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论