I am trying to write a simple server.js in Node, that posts form data from my html file, into MySQL. But I am getting a Syntax error. I will post the code and error below. I'm struggling to resolve this issue.
<head>
<title>Learning Node Server</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="header">
<h1>Learning Node and JS</h1>
<p>With Ninad</p>
</div>
<form action="/data" method="post">
<label for="name">Enter your name in the database</label>
<input type="text" name="name">
<input type="submit" value="submit" />
</form>
<div class="container"></div>
<script type="text/javascript" src="main.js"></script>
</body>
var express = require('express');
var app = express();
var mysql = require('mysql');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//app.use(express.json());
//app.use(express.urlencoded());
//app.use(app.router);
app.use(express.static('public'));
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '3748',
database : 'nodedb'
});
connection.connect();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/node.html');
});
app.post('/data', function(req, res){
var username=req.body.name;
connection.query("INSERT INTO `names` (name) SET ?", username.toString(), function(err, result){
if(err) throw err;
console.log("1 record inserted");
});
res.send(username);
});
//connection.end();
app.listen(3000, function () {
console.log('Example app listening on port 3000');
});
If I entered yahoo2 as the name, this is the error I get-
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 'SET 'yahoo2'' at line 1 at Query.Sequence._packetToError (/home/ninad/node-workspace/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
I am trying to write a simple server.js in Node, that posts form data from my html file, into MySQL. But I am getting a Syntax error. I will post the code and error below. I'm struggling to resolve this issue.
<head>
<title>Learning Node Server</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="header">
<h1>Learning Node and JS</h1>
<p>With Ninad</p>
</div>
<form action="/data" method="post">
<label for="name">Enter your name in the database</label>
<input type="text" name="name">
<input type="submit" value="submit" />
</form>
<div class="container"></div>
<script type="text/javascript" src="main.js"></script>
</body>
var express = require('express');
var app = express();
var mysql = require('mysql');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//app.use(express.json());
//app.use(express.urlencoded());
//app.use(app.router);
app.use(express.static('public'));
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '3748',
database : 'nodedb'
});
connection.connect();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/node.html');
});
app.post('/data', function(req, res){
var username=req.body.name;
connection.query("INSERT INTO `names` (name) SET ?", username.toString(), function(err, result){
if(err) throw err;
console.log("1 record inserted");
});
res.send(username);
});
//connection.end();
app.listen(3000, function () {
console.log('Example app listening on port 3000');
});
If I entered yahoo2 as the name, this is the error I get-
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 'SET 'yahoo2'' at line 1 at Query.Sequence._packetToError (/home/ninad/node-workspace/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
Share Improve this question asked Aug 26, 2017 at 4:43 sniktsnikt 6014 gold badges12 silver badges27 bronze badges2 Answers
Reset to default 2It should be this,
app.post('/data', function(req, res){
var username=req.body.name;
connection.query("INSERT INTO `names` (name) VALUES (?)", username.toString(), function(err, result){
if(err) throw err;
console.log("1 record inserted");
});
res.send(username);
});
Try this code. Create a views folder and place this jade file in it. This is my jade file I named it as
//index.jade
<html>
<head>
<title> test </title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<form action="/submit" method="POST">
<fieldset>
<label for="name">Name: </label>
<input type="text" id="name" name="name" />
<br/>
<label for="email">Email: </label>
<input type="email" id="email" name="email" />
<br/>
<label for="description">Description: </label>
<textarea id="description" name="description"></textarea>
<br/>
<input type="submit" value="create profile" />
</fieldset>
</form>
</body>
</html>
This is my server code save it as index.js
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var mysql = require('mysql');
var jade = require('jade');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
var app=express();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.set('view engine', 'jade');
app.get('/submit',function(req,res){
res.render('index');
});
app.post('/submit',urlencodedParser, function(req, res, next) {
console.log(req.body.name);
console.log(req.body.email);
console.log(req.body.description);
con.connect(function(err) {
if (err) throw err;
console.log("connected");
var sql = "INSERT INTO `form`(`name`,`email`, `description`) VALUES ('"+req.body.name+"','"+req.body.email+"','"+req.body.description+"')";
con.query(sql, function(err, result) {
if(err) throw err;
console.log("table created");
});
});
res.render('index', { title: 'Express' });
});
app.listen(3000,function(){
console.log("Sever listening on port 3000");
});
Before running the server create a database here I created my database with the name test
. And next create a table in it. Here I created a table with the name form.Run the node server as node index.js
. And in browser type http://localhost:3000/submit
. And next look at the database.Hope this helps...