'Update values in React/NodeJS MySQL query using previous values if nothing entered
I am using React and NodeJS to make a front end to my MySQL workbench data for mock blood donation management system. I would like to make it that when a user updates their contact information, they only have to fill out the information they are updating and not all the columns.
The GUI looks like this: [1]: https://i.stack.imgur.com/FaWXL.png
Currently my UPDATE statement looks like this:
app.put("/api/update/:log_ID", (req, res) => {
const ID = req.params.log_ID;
const SSN = req.body.SSN;
const Name = req.body.Name;
const Phone = req.body.Phone;
const City = req.body.City;
const State = req.body.State;
const BloodType = req.body.BloodType;
const DonationType = req.body.DonationType;
const LastDonationDate = req.body.LastDonationDate;
const LastDonationType = req.body.LastDonationType;
const sqlUpdate =
'UPDATE `donor` SET SSN = ?, \
Name = ?, \
Phone = ?, \
City = ?, \
State_Abbr = ?, \
Blood_Type = ?, \
Donation_Type_ID = ?, \
Last_Donation_Date = ?, \
Last_Donation_Type = ? \
WHERE log_ID = ? ';
db.query(sqlUpdate, [SSN, Name, Phone, City, State, BloodType, DonationType, LastDonationDate, LastDonationType, ID], (err, result) =>{
if (err) console.log(err);
else console.log(result);
})
});
I have tried using the format coalesce(@Name, ?) or coalesce(Name, ?) for each one and it worked for a few of them but started to bug out when it got to the Donation_Type_ID because it couldn't retrieve the previous value so I am back to square one. TIA
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
