I got a project from my friend, the project uses React.js, Node.js, Express.js and MySQL technologies. And when I work on the CRUD section, I only managed to make the input update, but I can't make changes to the database.
This is my React code:
import React, { Component } from "react";
import axios from "axios";
import { InputText } from "primereact/inputtext";
import { Button } from "primereact/button";
export class EditFK extends Component {
constructor() {
super();
this.state = {
id: "",
nama: null,
fakultas: []
};
}
componentDidMount() {
axios.get("http://localhost:3210/fakultas").then(getdata => {
this.setState({
fakultas: getdata.data
});
});
}
saveData() {
axios
.put(`http://localhost:3210/fakultas/edit/${this.params}`)
.then(postdata => {
this.setState({
dataTableValue: postdata.data
});
});
}
render() {
return (
<div className="p-grid p-fluid">
<div className="p-col-12 card card-w-title">
<h1>Edit Fakultas</h1>
</div>
<div className="p-col-6">
<div className="card card-w-title">
<h4>Ubah Fakultas</h4>
<div className="p-grid">
<div className="p-col-12 p-md-6">
<InputText placeholder="Nama" type="text" />
</div>
</div>
</div>
</div>
<div className="p-col-12">
<div className="p-col-3">
<Button
label="Update"
icon="pi pi-pencil"
onClick={this.saveData()}
/>
</div>
</div>
</div>
);
}
}
This is my backend code:
//get fakultas by id
app.get("/fakultas/:id", (req, res) => {
var sql = `SELECT * FROM fakultas WHERE id = ${req.params.id}`;
db.query(sql, (err, result) => {
if (err) {
throw err;
} else {
res.send(result);
}
});
});
//edit fakultas by id
app.put("/fakultas/edit/:id", (req, res) => {
console.log(req.body);
var data = {
id: req.body.id,
nama: req.body.nama
};
var sql = `UPDATE fakultas set ? WHERE id = ${req.params.id}`;
db.query(sql, data, (err, result) => {
if (err) {
throw err;
} else {
res.send({
status: `Data Berhasil di Update`,
id: req.body.id,
nama: req.body.nama
});
}
});
});
The link below is column from fakultas table: