SQL_MONGO_CODE
SQL_MONGO_CODE
Frontend:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<title>User Management</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body class="container mt-5">
<h2>User Management</h2>
function fetchUsers() {
axios.get('http://localhost:3000/users')
.then(response => {
userTableBody.innerHTML = '';
response.data.forEach(user => {
userTableBody.innerHTML += `
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.mob_no}</td>
<td>${user.address}</td>
<td>
<button class="btn btn-info" onclick="populateForm(${JSON.stringify(user)})">Edit</button>
</td>
</tr>
`;
});
});
}
function populateForm(user) {
document.getElementById('userId').value = user.id;
document.getElementById('name').value = user.name;
document.getElementById('age').value = user.age;
document.getElementById('mob_no').value = user.mob_no;
document.getElementById('address').value = user.address;
}
function insertUser() {
const name = document.getElementById('name').value;
const age = document.getElementById('age').value;
const mob_no = document.getElementById('mob_no').value;
const address = document.getElementById('address').value;
axios.post('http://localhost:3000/users', { name, age, mob_no, address })
.then(() => {
fetchUsers();
document.getElementById('userForm').reset();
});
}
function updateUser() {
const id = document.getElementById('userId').value;
const name = document.getElementById('name').value;
const age = document.getElementById('age').value;
const mob_no = document.getElementById('mob_no').value;
const address = document.getElementById('address').value;
axios.put(`http://localhost:3000/users/${id}`, { name, age, mob_no, address })
.then(() => {
fetchUsers();
document.getElementById('userForm').reset();
});
}
function deleteUser() {
const id = document.getElementById('userId').value;
axios.delete(`http://localhost:3000/users/${id}`)
.then(() => {
fetchUsers();
document.getElementById('userForm').reset();
});
}
fetchUsers();
</script>
</body>
</html>
Interface:
Backend MySQL:
Result:
Backend MongoDB:
app.use(cors());
app.use(bodyParser.json());
try {
await user.save();
res.sendStatus(201);
} catch (err) {
res.status(500).send(err);
}
});
try {
await User.findByIdAndUpdate(id, { name, age, mob_no, address });
res.sendStatus(200);
} catch (err) {
res.status(500).send(err);
}
});
try {
await User.findByIdAndDelete(id);
res.sendStatus(204);
} catch (err) {
res.status(500).send(err);
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
MongoDB Result: