Hello all: I am working on this node, express, mysql program that needs to upload an image and store its name in MySQL. The image gets uploaded just fine, but I cannot do the insert into MySQL. Sometimes I do not get an error, but the file name is not inserted into the database. Other times, I get the error that filename is undefined. Show Here is my index.html:
This is my server.js: Today we are going to explore the Express-FileUpload NPM package as we create a super simple user profile card and integrate our card with a MySQL database to retain the record. This article has been updated. – Removed Connection Pool Express-Handlebars code has been updated from 5.3.4 to 6.0.1 – 6.0.2. You can read the full Express-Handlebars Migration 6.0.1, 6.0.2 article here. If you are looking for an in-depth explanation please watch the full video tutorial. What you need:To complete this project you will need a MySQL Database & Node installed. I usually use XAMPP for local development, but you can use whatever you wish as long as the database is MySQL of course. Create a new projectTo create a new Node.js project all you have to do is to create a new project folder “user-management-system” and then run the Command line or PowerShell in the same directory. Once you do that to initialise a new project simply put the following command: npm install express express-handlebars express-fileupload mysql This will initialise a new project for you and it’s going to ask you a few questions about your project. The most important one is to give your package a name and then you can just keep pressing enter until the installation is over. Project StructureLet’s create the following folders and files, leaving node_modules, readme.md, package-lock and package-json as that should have been automatically generated by now. You can structure your project the way you like. I am just going to keep it simple and add everything into our app.js file 📂 node_modules 📂 public 📂 css 📜 main.css 📜 main.scss 📂 img 🖼 default.jpg 📂 views 📂 layouts 📜 main.hbs 📜 index.hbs 📜 README.md ⚓ .env 🌍 app.js 📜 package-lock.json 📜 package-json Let’s create our app.js file and each block of code will be explained with comments. (I didn’t use the connection pool correctly. Update coming soon – code still works) const express = require('express'); const exphbs = require('express-handlebars'); // updated to 6.0.X const fileUpload = require('express-fileupload'); const mysql = require('mysql'); const app = express(); const port = process.env.PORT || 5000; // default option app.use(fileUpload()); // Static Files app.use(express.static('public')); app.use(express.static('upload')); // Templating engine // app.engine('hbs', exphbs({ extname: '.hbs' })); // v5.3.4 // app.set('view engine', 'hbs'); // v5.3.4 // Update to 6.0.X const handlebars = exphbs.create({ extname: '.hbs',}); app.engine('.hbs', handlebars.engine); app.set('view engine', '.hbs'); // Connection Pool var connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'userprofile' }); app.get('', (req, res) => { connection.query('SELECT * FROM user WHERE id = "1"', (err, rows) => { if (!err) { res.render('index', { rows }); } }); }); app.post('', (req, res) => { let sampleFile; let uploadPath; if (!req.files || Object.keys(req.files).length === 0) { return res.status(400).send('No files were uploaded.'); } // name of the input is sampleFile sampleFile = req.files.sampleFile; uploadPath = __dirname + '/upload/' + sampleFile.name; console.log(sampleFile); // Use mv() to place file on the server sampleFile.mv(uploadPath, function (err) { if (err) return res.status(500).send(err); connection.query('UPDATE user SET profile_image = ? WHERE id ="1"', [sampleFile.name], (err, rows) => { if (!err) { res.redirect('/'); } else { console.log(err); } }); }); }); app.listen(port, () => console.log(`Listening on port ${port}`)); In our Views -> Layouts folder we need to create our Handlebars template. The {{{body}}} is where our index.hbs page will render: Document Now let’s create our index.hbs page, with our form and a loop to display the record from the database: How can we store image in MySQL database?Store Image File in Database (upload.. Check whether the user selects an image file to upload.. Retrieve the content of image file by the tmp_name using PHP file_get_contents() function.. Insert the binary content of the image in the database using PHP and MySQL.. Show the image uploading status to the user.. How to insert image in MySQL database using Javascript?Upload/store images in MySQL using Node.. Node.js upload/store image in MySQL overview.. Setup Node.js modules.. Configure MySQL database & Sequelize.. Initialize Sequelize.. Define the Sequelize Model.. Create middleware for uploading & storing image.. Create Controller for uploading Images.. How do I store an image in node JS?How to upload/store images in MongoDB using Node.. Setup Node.js modules.. Create View for uploading image.. Configure MongoDB database.. Create middleware for uploading & storing image.. Create Controller for the view.. Create Controller for uploading Images.. Define routes.. Create Express app server.. How to upload file in MySQL database using Node js?Node Js MySQL File Upload REST API Tutorial. Step 1 – Create Node Express js App.. Step 2 – Install Express + Mysql + Body parser + cors and Multer Library.. Step 3 – Create Database and Connect App to DB.. Step 3 – Create Server. js File.. Step 4 – Start Node Express Js App Server.. Step 5 – Upload File using Rest Api App.. |