Home > Net >  problem Insert Data Into the Table Using Node.js and MySQL
problem Insert Data Into the Table Using Node.js and MySQL

Time:05-28

I have a problem inserting data in the table from my HTML form on the site. if you could help me I'll be thankful. here are my codes:

users.js:

const express = require("express");
const router = express.Router();
const db = require("../database");

router.get("/form", (req, res, next) => {
  res.render("users.ejs");
});

router.post("/create", (req, res, next) => {
  let userDetails = req.body;
  let sql = "insert into users set ?";
  db.query(sql, userDetails, (err, results, fields) => {
    if (err) throw err;
    console.log("user data inserted successfully");
  });
 
  res.redirect("back");
});

module.exports = router;

users.ejs:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>
<body>
    <div class='user-detail'>
        <h2>create user data</h2>
        <form action="/users/create" method="post">
            <label>Full Name</label>
            <input type="text" placeholder="enter fullname" name="fullName" required>
            <label>Email Address</label>
            <input type="email" placeholder="enter email address" name="emailAddress" required>
            <label>City</label>
            <input type="text" placeholder="enter full city" name="city">
            <label>Country</label>
            <input type="text" placeholder="enter full country" name="country">
            <button type="submit">Submit</button>
        </form>
    </div>
</body>
</html>

error:

PS C:\Users\yaran\Desktop\mysql.ejs.node> node app.js server is running on port=3000.. database connected successfully! C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\protocol\Parser.js:437 throw err; // Rethrow non-MySQL errors ^

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1 at Query.Sequence._packetToError (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14) at Query.ErrorPacket (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\protocol\sequences\Query.js:79:18) at Protocol._parsePacket (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\protocol\Protocol.js:291:23) at Parser._parsePacket (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\protocol\Parser.js:433:10) at Parser.write (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\protocol\Parser.js:43:10) at Protocol.write (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\protocol\Protocol.js:38:16) at Socket. (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\Connection.js:88:28) at Socket. (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\Connection.js:526:10) at Socket.emit (node:events:527:28) at addChunk (node:internal/streams/readable:315:12) -------------------- at Protocol._enqueue (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\protocol\Protocol.js:144:48) at Connection.query (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\mysql\lib\Connection.js:198:25) at C:\Users\yaran\Desktop\mysql.ejs.node\routes\users.js:12:6 at Layer.handle [as handle_request] (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\express\lib\router\route.js:144:13) at Route.dispatch (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\express\lib\router\route.js:114:3) at Layer.handle [as handle_request] (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\express\lib\router\layer.js:95:5) at C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\express\lib\router\index.js:284:15 at Function.process_params (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\express\lib\router\index.js:346:12) at next (C:\Users\yaran\Desktop\mysql.ejs.node\node_modules\express\lib\router\index.js:280:10) { code: 'ER_PARSE_ERROR', errno: 1064, sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1", sqlState: '42000', index: 0, sql: 'insert into users set ?' }

CodePudding user response:

In case your req.body really hold only one variable, you have to put it in square brackets.

const express = require("express");
const router = express.Router();
const db = require("../database");
const bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true }));

router.get("/form", (req, res, next) => {
  res.render("users.ejs");
});

router.post("/create", (req, res, next) => {
  let userDetails = req.body;
  console.log(userDetails);
  let sql = "insert into users set ?";
  db.query(sql, [userDetails], (err, results, fields) => {
    if (err) throw err;
    console.log("user data inserted successfully");
  });
 
  res.redirect("back");
});

module.exports = router;
  • Related