mysql.js
const mysql = require('mysql');
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '104679',
database: 'funtime'
});
module.exports = connection;
register.js
const connection = require('../lib/mysql');
let {phone, digits, username, password} = req.body;
connection.query(`insert into users (phone, username, password) values (${phone}, ${username}, ${password})`, function(err, results, fields) {
if(err) throw err;
console.log(results[0]);
console.log(fields);
});
涓嬮潰鏄姤閿欎俊鎭細
throw err; // Rethrow non-MySQL errors
^
Error: ER_BAD_FIELD_ERROR: Unknown column 'dreamsline' in 'field list'
at Query.Sequence._packetToError (F:\fun-time\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (F:\fun-time\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (F:\fun-time\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (F:\fun-time\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (F:\fun-time\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (F:\fun-time\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (F:\fun-time\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (F:\fun-time\node_modules\mysql\lib\Connection.js:525:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
--------------------
at Protocol._enqueue (F:\fun-time\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (F:\fun-time\node_modules\mysql\lib\Connection.js:201:25)
at router.post (F:\fun-time\routes\register.js:85:18)
at Layer.handle [as handle_request] (F:\fun-time\node_modules\express\lib\router\layer.js:95:5)
at next (F:\fun-time\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (F:\fun-time\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (F:\fun-time\node_modules\express\lib\router\layer.js:95:5)
at F:\fun-time\node_modules\express\lib\router\index.js:281:22
at Function.process_params (F:\fun-time\node_modules\express\lib\router\index.js:335:12)
at next (F:\fun-time\node_modules\express\lib\router\index.js:275:10)
[nodemon] app crashed - waiting for file changes before starting...
The issue is that your values need to be quoted for MySQL. You can do this by escaping them (https://github.com/mysqljs/mysql#escaping-query-values):
const connection = require('../lib/mysql');
let {phone, digits, username, password} = req.body;
connection.query(`insert into users (phone, username, password) values (?, ?, ?)`, [phone, username, password], function(err, results, fields) {
if(err) throw err;
console.log(results[0]);
console.log(fields);
});
Most helpful comment
The issue is that your values need to be quoted for MySQL. You can do this by escaping them (https://github.com/mysqljs/mysql#escaping-query-values):