Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
How should I insert data to MYSQL database from a HTML form?

I know that node runs on the backend and while it's not a problem to connect to the SQL database via the server.js file, I cannot understand how I could do that from within a js linked to a form element (that's definitely client-side js, and I cannot make the connection from inside there). I know this maybe confusing, so I would very grateful for any help. I am a beginner, so please keep it simple. Youtube videos are also fine. Thanks in advance :)

    const mysql = require("mysql");
    //create connection to sql database
    const connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "",  
        database: "blog",
      });

      connection.query('select * from posts', () => {
          console.log('done');
      })
//this works when i put it inside server.js (using express) but not inside some other file- says cannot use require.
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Plain and simple , this is the initialization of the database connection. In order to use this connection object in other files you need to export it or to incorporate it in "req" object supported by express. I suggest that in the end of the file you add

module.exports ={
     connection : mysql.createConnection(config) 
} 

and then use the connection object to make queries elsewhere. Apart from this i would suggest that you start using Sequelize since it is a powerful ORM for nodejs. First learn sql logic and test your queries directly in the database through Mysql workbench , pgadmin , etc and then dive deep into sequelize documentation to take yourself to the next level. Hope this answer will solve your problem!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!