首页 > 解决方案 > 我无法将数据插入我的数据库(mysql)

问题描述

我现在正在学习 MySQL,但我遇到了一些问题。我试图向我的数据库发出发布请求::

在此处输入图像描述

我来自客户端的代码:

async function sendValues() {
    const settings = {
        method: 'post',
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(product)
    }
    try {
        const fetchResponse = await fetch('http://localhost:9001/products', settings);
        const data = await fetchResponse.json();
        console.log(data) // what the user send
    } catch (err) {
        return err
    }
}

我的服务器端代码:

router.post('/', (req, res) => {
    const { c_id, p_name, p_price } = req.body
    const q =
        `
        INSERT INTO products (c_id,p_name,p_price)
        VALUES("${c_id}", "${p_name}" , "${p_price}")
        `;
    con.query(q, (err, result, fields) => {
        if (err) throw err;
        res.json(result)
    });
});

标签: javascriptmysqlnode.jsreactjsexpress

解决方案


有两个可能的错误:

1.检查客户端和服务器之间的连接。正如刚才提到的。

2.检查表名,属性的数据类型,您提供的属性名称。


推荐阅读