PHP post 通过动态传递值请求
PHP post request by passing the values dynamically
我试图将名称和公司这两个值传递给服务器,但它总是将它们作为字符串传递。
app.post('/visitors/store', (req, res) => {
const name = req.body.name; (User1)
const company = req.body.company; (Google)
con.query("INSERT INTO visitors(name, company) VALUES(`name`,`company`)", (results) => {
res.json(results);
});
})
问题出在行的这一部分 "VALUES(name
,company
)"
数据库atm中的结果:name = name , company = company
我需要的结果是:name = User1,company = Google
感谢您的帮助
只需中断查询字符串并在其中包含实际变量:
app.post('/visitors/store', (req, res) => {
const name = req.body.name;
const company = req.body.company;
con.query("INSERT INTO visitors(name, company) VALUES('" + name + "','" + company + "')", (results) => {
res.json(results);
});
});
根据您的环境,您还应该转义查询值。
如果你使用 mysql 你应该可以这样:
app.post('/visitors/store', (req, res) => {
const name = req.body.name;
const company = req.body.company;
con.query("INSERT INTO visitors(name, company) VALUES(?,?)", [name, company], (results) => {
res.json(results);
});
或不转义:
app.post('/visitors/store', (req, res) => {
const name = req.body.name;
const company = req.body.company;
con.query(`INSERT INTO visitors(name, company) VALUES(${name}, ${company})`, (results) => {
res.json(results);
});
我试图将名称和公司这两个值传递给服务器,但它总是将它们作为字符串传递。
app.post('/visitors/store', (req, res) => {
const name = req.body.name; (User1)
const company = req.body.company; (Google)
con.query("INSERT INTO visitors(name, company) VALUES(`name`,`company`)", (results) => {
res.json(results);
});
})
问题出在行的这一部分 "VALUES(name
,company
)"
数据库atm中的结果:name = name , company = company
我需要的结果是:name = User1,company = Google
感谢您的帮助
只需中断查询字符串并在其中包含实际变量:
app.post('/visitors/store', (req, res) => {
const name = req.body.name;
const company = req.body.company;
con.query("INSERT INTO visitors(name, company) VALUES('" + name + "','" + company + "')", (results) => {
res.json(results);
});
});
根据您的环境,您还应该转义查询值。 如果你使用 mysql 你应该可以这样:
app.post('/visitors/store', (req, res) => {
const name = req.body.name;
const company = req.body.company;
con.query("INSERT INTO visitors(name, company) VALUES(?,?)", [name, company], (results) => {
res.json(results);
});
或不转义:
app.post('/visitors/store', (req, res) => {
const name = req.body.name;
const company = req.body.company;
con.query(`INSERT INTO visitors(name, company) VALUES(${name}, ${company})`, (results) => {
res.json(results);
});