【问题标题】:Performing a mysql query from node/express server using ? placeholder getting syntax error | npm mysql package使用 ? 从 node/express 服务器执行 mysql 查询占位符出现语法错误 | npm mysql 包
【发布时间】:2025-11-23 09:45:01
【问题描述】:

我似乎无法理解这一点。我有一个正确设置的连接,并且能够从前端从我的数据库中获取数据,但是我相信某些语法有问题。我将用户输入存储在“searchTerm”中,我正在尝试使用占位符运行查询?。

我遇到问题的代码是

app.get('/api/get', (req, res)=> {
    const searchTerm = req.body.searchTerm;
    const sqlSelect = "SELECT * FROM questions WHERE MATCH(questionTitle) AGAINST(?)";
    db.query(sqlSelect, searchTerm, (err, result) => {
        res.send(result);
    });
});

问题不在于查询,因为它在工作台中得到了肯定的结果,并且当我对用户输入的值进行硬编码时也可以正常工作。我收到一个错误:ER_PARSE_ERROR:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 '?)' 附近使用的正确语法

我很难过,因为这是我的帖子,它按预期工作:

app.post('/answer/insert', (req, res, next)=> {
    const question = req.body.question;
    const answer = req.body.answer;
    const category = req.body.category;
    const today = req.body.today;

    const sqlInsert = "INSERT INTO questions (questionTitle, questionBody, category, questionDate) VALUES (?,?,?,?)";
    db.query(sqlInsert, [question, answer, category, today], (err, result) => {
        console.log(err);
    });
});

更新:前端看起来像这样

function startSearch(e) {
        e.preventDefault();
        console.log(searchTerm);
        Axios.get('http://localhost:3001/api/get', {
            searchTerm: searchTerm,
        })
       .then(function(response) {
          console.log("It worked, response is: ", response)
         
       }).catch(function() {
          console.log("error");
       });
        };

我的帖子请求有效,它看起来像这样:

const submitReview = () => {
        Axios.post('http://localhost:3001/answer/insert', {
            question: question, answer: answer, category: category, today: today,
        }).then(()=> {
            console.log(category + question + answer + today);
            alert('successful insert');
        })
        .catch(()=> {
            console.log(category + question + answer + today);

            alert('it didnt work');
        })
    }

【问题讨论】:

    标签: mysql sql node.js reactjs express


    【解决方案1】:

    原答案

    https://www.npmjs.com/package/mysql#performing-queries这里所述,查询参数需要作为数组传入。

    改变

    db.query(sqlSelect, searchTerm, (err, result) => {
        res.send(result);
    });
    

    db.query(sqlSelect, [searchTerm], (err, result) => {
        res.send(result);
    });
    

    更新

    您的名为 startSearch 的前端使用 GET 请求。这不会在体内发送任何内容。我会使用查询字符串(从参数更改,查询参数是 URL 末尾的 ? 之后的内容)。假设您使用的是 Express.JS,请在此处查看 https://stackabuse.com/get-query-strings-and-parameters-in-express-js/。它应该为您指明正确的方向。

    如果您需要特定的代码,这将满足您的需求。上面的链接将帮助您了解这是如何工作的。

    更改后端
    app.get('/api/get', (req, res)=> {
        const searchTerm = req.body.searchTerm;
        const sqlSelect = "SELECT * FROM questions WHERE MATCH(questionTitle) AGAINST(?)";
        db.query(sqlSelect, searchTerm, (err, result) => {
            res.send(result);
        });
    });
    

    app.get('/api/get/:searchTerm', (req, res)=> {
        const searchTerm = req.params.searchTerm;
        const sqlSelect = "SELECT * FROM questions WHERE MATCH(questionTitle) AGAINST(?)";
        db.query(sqlSelect, [searchTerm], (err, result) => {
            res.send(result);
        });
    });
    

    前端来自

    function startSearch(e) {
            e.preventDefault();
            console.log(searchTerm);
            Axios.get('http://localhost:3001/api/get', {
                searchTerm: searchTerm,
            })
           .then(function(response) {
              console.log("It worked, response is: ", response)
             
           }).catch(function() {
              console.log("error");
           });
    };
    

    function startSearch(e) {
            e.preventDefault();
            console.log(searchTerm);
            Axios.get('http://localhost:3001/api/get/' + searchTerm)
           .then(function(response) {
              console.log("It worked, response is: ", response)
             
           }).catch(function() {
              console.log("error");
           });
    };
    

    【讨论】:

    • 我希望是这样:/,我已经尝试过了,得到了一个 0 的数组谢谢你的回答
    • const searchTerm = req.body.searchTerm上方添加console.log(req.body),并确保请求正文中有searchTerm值。似乎searchTerm 没有在req.body 中传递。
    • 你说得对,这很有帮助!我前端的代码是function startSearch(e) { e.preventDefault(); console.log(searchTerm); Axios.get('http://localhost:3001/api/get', { searchTerm: searchTerm, }) .then(function(response) { console.log("It worked, response is: ", response) }).catch(function() { console.log("error"); }); };
    • 不是很友好的格式对不起,但 searchTerm 确实有正确的值,但它没有被传递到我的服务器。我的 post req 遵循相同的格式,但不确定问题是什么? const submitReview = () => { Axios.post('http://localhost:3001/answer/insert', { question: question, answer: answer, category: category, today: today, }).then(()=> { console.log(category + question + answer + today); alert('successful insert');
    • 更新了我最初的问题以包含前端代码!
    最近更新 更多