How to insert data into MySQL table using Node and express?
Try like this:
var sql = `INSERT INTO user
(
Name, Email, Address, City, Country, password
)
VALUES
(
?, ?, ?, ?, ?, ?
)`;
connection.query(sql, [name , email, address, city, country , password], function (err, data) {
if (err) {
// some error occured
} else {
// successfully inserted into db
}
});
Sending data in a html form to mysql data base using express results in undefined database columns
You're using express.json()
but your form data is not in JSON format.
You can install express-formidable
with npm in your app folder
Then you need to create the middleware using it, like so:
// in app.js
const formidableMiddleware = require('express-formidable');
//...
// Note that req.fields will be instead of req.body due to middleware
// used to handle json, forms, and uploading files
app.use(formidableMiddleware(
{
encoding: 'utf-8',
// means multi files (array of files) in one request
multiples: true,
}
));
You will need to replace app.use(express.json());
with above snippet
You can also check an example from ExpressJS repo on github for multipart forms
Posting Form data to MySQL using nodejs w/ Express
It 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);
});
Related Topics
Provide an Image for Whatsapp Link Sharing
Html Video Not Playing in Safari Browser
Html - How to Scroll Vertical in Select Box
How to Position Elements Next to Each Other in a Div
How to Hide Link Information At the Bottom Left/Right of the Browser on Hover
Displaying Comma Separated String in Angular 6
How to Make Transparent Color White Instead of Black in Html2Canvas
Maintain the Aspect Ratio of a Div With Css
How to Remove Blue Highlight on Hover in Select, Option Inputs HTML for Chrome
Change Arrow Colors in Bootstraps Carousel
Error Error: Formcontrolname Must Be Used With a Parent Formgroup Directive
How to Arrange HTML Label and Input Side by Side
Zoom to Fit: Pdf Embedded in HTML
Whatsapp Use Only HTML to Share Url Link by a Descriptive Text
Htmlspecialchars() Expects Parameter 1 to Be String Array Given