Mysql: How to resolve the problem?

Created on 25 Apr 2019  路  1Comment  路  Source: mysqljs/mysql

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...
mysql server behavior question

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):

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);
      });

>All comments

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);
      });
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hohozhao picture hohozhao  路  4Comments

winzig picture winzig  路  4Comments

johnrc picture johnrc  路  3Comments

acefxlabs picture acefxlabs  路  4Comments

bologer picture bologer  路  3Comments