将js表单数据插入mysql数据库

时间:2017-03-24 19:43:30

标签: mysql node.js reactjs express

我必须使用express js,fetch api将名称字段数据插入到mysql数据库中。任何人都可以用简单的方式帮助流动和项目结构。提前谢谢。

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

ReactDOM.render(
  <NameForm />,
  document.getElementById('root')
);

1 个答案:

答案 0 :(得分:-1)

有一个很好的例子,说明如何使用Express指南中的Node连接到MySQL数据库。 https://expressjs.com/en/guide/database-integration.html#mysql

var mysql = require('mysql')
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'dbuser',
  password : 's3kreee7',
  database : 'my_db'
});

connection.connect()

connection.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {
  if (err) throw err

  console.log('The solution is: ', rows[0].solution)
})

connection.end()

希望能帮助你开始跑步。