Ich habe diesen Code erstellt, um Daten in die Datenbank einfügen mit Nodejs und Mysql und das hat funktioniert und die Daten eingefügt, aber das Problem, die Seite immer noch neu geladen und nie nach dem Klicken auf Senden und Senden der gleicher Wert mehr als einmal in die Datenbanknodejs Seite nicht stoppen Neuladen
server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
bodyParser = require('body-parser');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
database: 'chmult',
user: 'root',
password: '',
});
users = [];
connections = [];
app.get('/', function(req, res){
res.sendFile(__dirname + '/');
});
app.use(bodyParser.urlencoded({
extended: true
}));
/**bodyParser.json(options)
* Parses the text as JSON and exposes the resulting object on req.body.
*/
app.use(bodyParser.json());
connection.connect();
app.post("/", function (req, res) {
console.log(req.body.user.username)
connection.query("Insert into tesko (username) VALUES ('"+req.body.user.username+"')")
});
app.listen(3231);
console.log('Example app listening at port:3000');
der hTML-Code
<html>
<form method="post" action="">
<input type="text" name="user[username]">
<input type="submit" value="Submit">
</form>
</html>
Blindes Verketten von Benutzereingaben in eine SQL-Abfrage erfordert einen Injektionsangriff. ** TUN SIE DIESES NICHT ** – mscdex