当尝试使用Node.js更新Mysql数据库时,我一直收到语法错误。我正在尝试更新表格。我不确定我的语法是否正确。
除更新请求外,我所有其他请求都可以正常工作,例如插入表单,从中检索数据,从表单中删除。我已经在下面尝试了两个(2)选项,但两个都不起作用。
我在做什么错?有没有其他更新表单的方法?
<OPTION 1>
app.post('/edit',(req,res)=>{
var sql = "UPDATE stores (ID, Station, Address, `Monthly C-Store
Sales`, Operator, `Top SKU`) \ VALUES (?, ?, ?, ?, ?, ?)"
id = req.body.ID;
station = req.body.Station;
address = req.body.Address;
monthlycstoresales = req.body.MonthlyCStoreSales;
operator = req.body.Operator;
topsku = req.body.TopSKU;
db.query(sql, [id, station, address, monthlycstoresales, operator,
topsku], (err, results, field) => {
if (err) {
console.log(err);
res.sendStatus(500);
return;
} else{
res.send(results);
};
});
});
``````````````````````````````````````````````````````````````````````````````
<OPTION 2>
app.post('/edit',(req,res)=>{
var params = [req.body, req.query.id]
db.query('UPDATE FROM stores WHERE id = ?', [params],(err, rows,
fields)=>{
if(!err)
res.redirect('/alldata');
//console.log('Update Successful');
else
console.log(err);
//return;
});
});
````````````````````````````````````````````````````````````````````````
CORRECTED UPDATED SYNTAX
app.post('/edit',(req,res)=>{
var sql ="UPDATE stores SET Station = ?, Address = ?, `Monthly C-Store
Sales` = ?, Operator = ?, `Top SKU` = ? WHERE id = ?"
station = req.body.Station;
address = req.body.Address;
monthlycstoresales = req.body.MonthlyCStoreSales;
operator = req.body.Operator;
topsku = req.body.TopSKU;
id = req.body.ID;
db.query(sql, [station, address, monthlycstoresales, operator, topsku,
id],
(err, results, field) => {
if (err) {
console.log(err);
res.sendStatus(500);
return;
} else{
res.send(results);
};
});
});
答案 0 :(得分:0)
您的update
语句的语法不正确。应该是这样的(当然,您当然需要按相应的顺序绑定值):
UPDATE stores
SET `Station` = ?,
`Address` = ?,
`Monthly C-Store Sales` = ?,
`Operator` = ?,
`Top SKU`=?
WHERE id = ?