I’m trying to make the “Edit Profile” page so a user can edit his/her profile info. But I screwed up somewhere and It is not working. I am assuming that I did a mistake in the query part.
My controller (API):
export const editUser = (req, res) => {
const q = "SELECT * FROM users WHERE id = ?";
const userId = req.params.userId;
db.query(q, [userId], (err, data) => {
if (err) return res.status(409).json(err);
const q = "UPDATE users SET (`name`, `surname`, `headline`) VALUES (?)";
const values = [req.body.name, req.body.surname, req.body.headline];
db.query(q, [values], (err, data) => {
if (err) return res.status(500).json(err);
return res.status(200).json("User has been updated.");
});
});
};
My route (API):
const router = express.Router();
router.post("/edit-profile/:userId", editUser);
export default router
My client site code:
const [err, setErr] = useState(null);
const [inputs, setInputs] = useState({ name: "", surname: "", headline: "" });
const handleChange = (e) => {
setInputs((prev) => ({ ...prev, [e.target.name]: e.target.value }));
console.log(inputs);
};
const handleClick = async (e) => {
e.preventDefault();
try {
await axios.post("http://localhost:8800/api/users/edit-profile/" + userId, inputs);
} catch (err) {
setErr(err.response.data);
}
};
HTML:
<form action="a">
<div>
<p>Name:</p>
<input
type="text"
placeholder={error ? "Something Went wrong" : isLoading ? "Loading" : data.name}
name="name"
onChange={handleChange}
/>
</div>
<div>
<p>Surname:</p>
<input
type="text"
placeholder={error ? "Something Went wrong" : isLoading ? "Loading" : data.surname}
name="surename"
onChange={handleChange}
/>
</div>
<div>
<p>Headline:</p>
<input
type="text"
placeholder={error ? "Something Went wrong" : isLoading ? "Loading" : data.headline}
name="headline"
onChange={handleChange}
/>
</div>
</form>
To make it simpler I tried to POST in Insomnia :
{
"name": "asd",
"surname": "asd",
"headline": "asd"
}
But I am getting an error:
{
"code": "ER_PARSE_ERROR",
"errno": 1064,
"sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(`name`, `surname`, `headline`) VALUES ('asd', 'asd', 'asd')' at line 1",
"sqlState": "42000",
"index": 0,
"sql": "UPDATE users SET (`name`, `surname`, `headline`) VALUES ('asd', 'asd', 'asd')"
}
I want to edit the user name, surname, and headline of a user.