【问题标题】:INSERT INTO is inserting empty values into mySQL tableINSERT INTO 正在将空值插入 mySQL 表
【发布时间】:2018-10-30 04:05:51
【问题描述】:

我正在尝试使用 ReactNative、Express.js 和 mySQL 制作用户注册应用程序。我有以下登录功能的代码:

login = () => {

      fetch('http://[myipaddress]/users', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          username: this.state.username,
          password: this.state.password,
          insert: false
        })
      })

      .then((response) => response.json())
      .then ((res) => {

        if(res.success === true){
          AsyncStorage.setItem('user', res.user);
          this.props.navigation.navigate('Profile');
        }
        else{
          alert(res.message);
        }

      })
      .done();
  }

注册码如下所示。

userRegister = () =>{

      fetch('http://[myipaddress]/users', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          username: this.state.username,
          email: this.state.email,
          password: this.state.password,
          insert: true
        })
      })

      .then((response) => response.json())
      .then((responseJson) => {
        alert(responseJson);
      })
      .catch((error)=>{
        console.error(error);
      });
    }

最后,这是 users.js 中的插入代码:

var express = require('express');
var router = express.Router();
var mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'users'
});

router.post('/', function(req, res, next) {

  var username = req.body.username;
  var email = req.body.email;
  var password = req.body.password;
  var insert = req.body.insert;

  console.log(username);
  console.log(email);
  console.log(password);

  if (insert === false) {
      connection.query(
            "SELECT * FROM user WHERE username = ? AND password = ?",
            [username, password], function (err, row, field) {

            if (err) {
                console.log(err);
                res.send({ 'success': false, 'message': 'Could not connect' });
            }

            if (row.length > 0) {
                res.send({ 'success': true, 'user': row[0].username });
            } else {
                res.send({ 'success': false, 'message': 'User not found' });
            }

        });
    } else {
        connection.query(
            "INSERT INTO user(username, email, password) VALUES (username,email,password)", 
            function (err, result) {

            if (err) {
                res.send({ 'success': false, 'message': 'Could not connect' });
            }
            else{
                res.send({ 'success': true, 'message': 'User created' });
            }
        });
    }
});

module.exports = router;

所以基本上,如果 insert 为 false,则表示应用正在尝试登录,只需要检查用户是否存在。这部分工作正常。但是,如果insert 为真,它应该将新的用户信息插入到表中。但由于某种原因,它会插入空值,如下所示: Screenshot

使用用户名 John 的第一个插入应该是这样的,我在 phpMyAdmin 中手动插入。其他是通过应用程序进行的插入,您可以看到所有值都是空的。控制台日志可以很好地打印出这些值,所以我知道它们正按应有的方式存储在变量中,只是由于某种原因没有被插入。

【问题讨论】:

    标签: mysql node.js express react-native


    【解决方案1】:

    您需要在 VALUES 列表中使用占位符,就像您在 SELECT 查询中所做的那样。

        connection.query(
            "INSERT INTO user(username, email, password) VALUES (?, ?, ?)", 
            [username, email, password],
            function (err, result) {
    

    【讨论】:

      猜你喜欢
      • 2014-12-07
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2017-04-21
      • 2018-11-13
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多