Example : A Node.js program to Save the records in the Xampp MySql database.
(inside VIEW FOLDER of Node.js)
student.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<title><%= title %></title>
</head>
<body>
<br><br>
<center>
<fieldset style="width: 700px;">
<h1>Student Registration</h1>
<a href="/">Home</a> | <a href="/employee">Employee</a> | <a href="/customer">Customer</a>
<form action="/student" method="POST">
<br>
<table>
<tr>
<td>Name</td>
<td>:</td>
<td><input type="text" name="name1" required></td>
</tr>
<tr>
<td>Gender</td>
<td>:</td>
<td><input type="radio" value="Male" name="gen1">Male
<input type="radio" value="Female" name="gen1">Female
<input type="radio" value="Transgender" name="gen1">Transgender
</td>
</tr>
<tr>
<td>Mobile No</td>
<td>:</td>
<td><input type="number" name="mobile1" ></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input type="email" name="email1"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="pass1"></td>
</tr>
<tr>
<td>DOB</td>
<td>:</td>
<td><input type="date" name="date1"></td>
</tr>
<tr>
<td>Time</td>
<td>:</td>
<td><input type="time" name="time1"></td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td><textarea rows="4" cols="30" name="addr1"></textarea></td>
</tr>
<tr>
<td>Education</td>
<td>:</td>
<td><input type="checkbox" value="Non-Matric" name="nonmatric1">Non-Matric
<input type="checkbox" value="Matric" name="matric1">Matric
<input type="checkbox" value="Intermediate" name="inter1">Intermediate
<input type="checkbox" value="Graduation" name="grad1">Graduation
<input type="checkbox" value="PostGraduation" name="postgrad1">PostGraduation
<input type="checkbox" value="PhD" name="phd1">PhD
</td>
</tr>
<tr>
<td>Nationality</td>
<td>:</td>
<td>
<select name="nat1">
<option value="India">India</option>
<option value="USA">USA</option>
<option value="Bhutan">Bhutan</option>
<option value="Sri Lanka">Sri Lanka</option>
<option value="Russia">Russia</option>
<option value="Nepal">Nepal</option>
</select>
</td>
</tr>
<tr>
<td>Remarks</td>
<td>:</td>
<td><input type="text" name="rem1" value="N/A"></td>
</tr>
<tr></tr>
<tr></tr>
<tr>
<td></td>
<td></td>
<td><button type="submit" name="submit1">Submit</button>
<button type="reset" name="reset1">Reset</button>
<button type="submit" name="update1">Update</button>
<button type="submit" name="delete1">Delete</button>
<button type="submit" name="search1">Search</button>
<button type="submit" name="exit1">Exit</button>
</td>
</tr>
</table>
</form>
</fieldset>
</center>
</body>
</html>
(outside VIEW FOLDER but as Root file of Node.js)
server.js
const express = require("express");
//const mysql = require("mysql");
const mysql = require("mysql2"); //Installed mysql version
const bodyParser = require("body-parser");
const app = express();
const PORT = 3000;
// Set EJS as the template engine
app.set("view engine", "ejs");
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
// Connect to MySQL Database code
const db = mysql.createConnection({
host: "localhost",
user: "root", // Default XAMPP MySql username
password: "", // Default XAMPP password is empty
database: "ejs_forms_db" // MySql Database Name
});
db.connect((err) => {
if (err) {
console.error("Database connection failed: " + err.stack);
return;
}
console.log("Connected successfully to MySQL database");
});
//------------------------------------------------------------------------------
// Render Student Form
app.get("/student", (req, res) => {
res.render("student", { title: "Student Registration" });
});
// Handle form submission
app.post("/student", (req, res) => {
const { name1, gen1, mobile1, email1, pass1, date1, time1, addr1, nat1, rem1 } = req.body;
const nonmatric1 = req.body.nonmatric1 ? 1 : 0;
const matric1 = req.body.matric1 ? 1 : 0;
const inter1 = req.body.inter1 ? 1 : 0;
const grad1 = req.body.grad1 ? 1 : 0;
const postgrad1 = req.body.postgrad1 ? 1 : 0;
const phd1 = req.body.phd1 ? 1 : 0;
// Debug log to check all values
console.log(" Form data received:", {
name1, gen1, mobile1, email1, pass1, date1, time1, addr1, nonmatric1, matric1, inter1, grad1, postgrad1, phd1, nat1, rem1});
// Add all 16 columns and 16 values
const sql = `INSERT INTO students(name5, gen5, mobile5, email5, pass5, date5, time5, addr5,nonmatric5, matric5, inter5, grad5, postgrad5, phd5, nat5, rem5)VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
const values = [name1, gen1, mobile1, email1, pass1, date1, time1, addr1, nonmatric1, matric1, inter1, grad1, postgrad1, phd1, nat1, rem1];
db.query(sql, values, (err, result) => {
if (err) {
console.error(" Error inserting data:", err);
res.render("student", {
title: "Student Registration",
message: " Error saving data!"
});
} else {
console.log(" Student record added successfully");
res.render("student", {
title: "Student Registration",
message: " Student record added successfully!"
});
}
});
});
// Start Server
app.listen(PORT, () => {
console.log(` Server running at http://localhost:${PORT}`);
});
0 Comments