update database in SQL using a form with html/js

تبليغ
سؤال

يرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.

تبليغ
‎إلغاء

i’m trying to implement a “update/edit” form where you can alter existing data from a sql database. i will add the code for how i add data to the database as well for reference:

//add.html get
app.get(“/add”, (req, res) => {
res.render(“add.html”);
})
app.post(“/add”, (req, res) => {
//saving data in database
let sqlquery = “INSERT INTO devices (deviceName, deviceType, deviceStatus) VALUES (?,?,?)”;
//execute sql query
let newrecord = [req.body.deviceName, req.body.deviceType,req.body.deviceStatus];
db.query(sqlquery, newrecord, (err, result) => {
if (err) { //is there is an error in filling the form, console will return an error message
return console.error(err.message);
} else
// res.send(“This device has been added to database”);
});
});

(WHAT I AM TRYING TO ACHIEVE: [![ex from youtube][1]][1])
[![the add device page][2]][2]
[1]: https://i.stack.imgur.com/3eYjp.png
[2]: https://i.stack.imgur.com/ybq2Q.png

i am unsure how to connect my database to the form so when the user presses “edit” it will redirect them to the form where existing information will already be filled in. this is all i’ve gotten so far.. i’m not sure what to call in my var sql.. 🙁

// edit post
app.get(‘/edit/:id’, (req,res) => {
var sql = ‘UPDATE devices SET fields ? WHERE id ?’; (???)
var id = req.params.id;

db.query(sql, [id], function (err, result) {
if (err) {
throw err;
}
console.log(result.affectedRows + ‘ row(s) updated’);
});
});“`

[1]: https://i.stack.imgur.com/3eYjp.png
[2]: https://i.stack.imgur.com/ybq2Q.png

0
X 5 أشهر 2022-07-10T12:05:31+03:00 0 إجابة 2 مشاهدات مبتدئ 0

‫أضف إجابة

تصفح
تصفح

مجهول يجيب