我是在 MySQL 中使用 node 的新手,但是當我嘗試向我的 SQL 服務器發送查詢以插入資料時,它并沒有從提交的表單中插入值。我的路線代碼如下所示,
const { application } = require('express')
const express = require('express')
const router = express.Router()
const pool = require('../config/db');
router.post("/create", async (req, res) => {
// Get user input
const name = req.body.name
const country = req.body.country
const dob = req.body.dob
const sql = `INSERT INTO users (name, country, dob) VALUES ('" req.body.name "', '" req.body.country "','" req.body.dob "');`
pool.query(sql, function(error, results){
if (error) {
throw error
}
res.status(201).send(`User added with ID: ${results.id}`)
console.log(name);
})
});
我以前的查詢是這樣的,
const sql = `INSERT INTO users (name, country, dob) VALUES (name, country, dob);`
但就我試圖將它們插入表中而言,兩者都不起作用。我的 React Form 中的一些重要代碼可能會有所幫助,
constructor(props) {
super(props);
this.options = countryList().getData();
this.state = {
name: '',
options: this.options,
val: null,
country: '',
dob: new Date(),
}
this.handleStartDateChange = this.handleStartDateChange.bind(this);
}
handleNameChange = (e) => {
this.setState({
name: e.target.value
})
}
handleOptionsChange = (val) => {
this.setState({
val
})
this.setState({
country:val.label
})
}
handleStartDateChange(date) {
this.setState({
dob: date
})
}
我的句柄提交是這樣的,
const handleSubmit = (e) =>{
e.preventDefault()
axios.post('http://localhost:4000/app/create', this.state)
console.log(this.state);
console.log(this.state) 提交后在控制臺中給出這個,
{name: 'Prajwal V', options: Array(249), val: {…}, country: 'Afghanistan', dob: Wed Nov 09 2022 18:02:00 GMT 0530 (India Standard Time)}
country
:
"Afghanistan"
dob
:
Wed Nov 09 2022 18:02:00 GMT 0530 (India Standard Time) {}
name
:
"Prajwal V"
options
:
(249) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
val
:
{value: 'AF', label: 'Afghanistan'}
[[Prototype]]
:
Object
PHPMyAdmin 中的表格內容
,
我希望我已經清楚地解釋了我的問題。非常感謝您對此進行審查。
uj5u.com熱心網友回復:
我建議更改 API 中的查詢。此外,您需要回傳并執行 SELECT 以獲取您想要發送回前端的行的 ID。
const { application } = require("express");
const express = require("express");
const router = express.Router();
const pool = require("../config/db");
router.post("/create", async (req, res) => {
// Get user input
const { name, country, dob } = req.body;
pool.query(
"INSERT INTO users SET name = ?, country = ?, dob = ?",
[name, country, dob],
(err, results) => {
if (err) {
throw err;
} else {
pool.query(
"SELECT id FROM users WHERE name = ?, country = ?, cob = ?",
[name, country, dob],
(err, results) => {
if (err) {
throw err;
}
if (results) {
res.status(200).send(`User added with ID: ${results.id}`);
console.log(name);
}
}
);
}
}
);
});
此外,請確保您的資料庫中的資料型別正確,否則 MySQL 將拒絕您提供的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/530734.html
上一篇:編輯資料庫中沒有表格記錄的資料
下一篇:Postgres Go Docker-compose無法ping資料庫:撥號tcp127.0.0.1:5432:連接:連接被拒絕
