SQL Input from Glitch site?

I’m just wondering if anyone knows how to make it so I can input entries to a SQL DB from a glitch site with the options selected inside of the site, such as text boxes or dropdown menus. Let me know!

You need to make a POST request with ajax in your client side.

But first you should make a hello-express glitch project.

With a POST handler and connection to your MySQL database

server-side

const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');

// express configuration
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// MySQL configuration
var sql = mysql.createConnection({
  host     : 'your-mysql-host',
  user     : 'your-mysql-user',
  password : process.env.MYSQLPASSWORD, //add to your .env file MYSQLPASSWORD=passwordHere 
  database : 'your-mysql-database-name'
});

sql.connect();

// POST to https://your-glitch-project-name.glitch.me/here
app.post('/here', (req, res) => {
 // do the SQL query
  sql.query('YOUR-SQL-QUERY-HERE', (error, results, fields) => {
    if(error) throw error;
    console.log('Done');
    res.send(req.body); // response
  })
});

// start the server
const listener = app.listen(process.env.PORT, function() {
	console.log('Your app is listening on port ' + listener.address().port);
});

That is an example of the server side. Then you should look for make the request to the post https://your-glitch-project-name.glitch.me/here with a html form and ajax

1 Like