1

I’m setting up a subdomain where users can upload updates to the database.

The correct data gets sent from the front end, and the API connects to the database successfully but stops there. I can’t figure out why.

app.post('/newsupdate', (req, res) => {
    let headline = req.body.headline;
    let content = req.body.content;

    let con = mysql.createConnection({
        host: HOST,
        user: USERNAME,
        password: PASSWORD,
        database: DATABASE
    });

    con.connect(function(err){
        if(err) return res.json({
            message: err
        });
  
        let sql = `INSERT INTO news (Headline, Content) VALUES (${headline}, &{content})`;
  
        con.query(sql, function(err, result){
            if(err) return res.json({message: err});

            if(result){
                res.json({
                    message: 'news update saved'
                });
            } else {
                res.json({
                    message: 'problems happened'
                });
            }
        })
    })
})
3
  • 2
    For future reference, you probably shouldn't put your real username and password into an online forum. Commented Oct 13, 2022 at 21:28
  • 2
    And you should immediately change your username and password because people can see the edit history on your question. Commented Oct 13, 2022 at 21:31
  • 1
    Also, looking at your code, I'd suggest looking into prepared statements. Commented Oct 13, 2022 at 21:35

2 Answers 2

1

You have a typo in your query (&{content}), try to use a prepared statement as suggested:

let sql = `INSERT INTO news (Headline, Content) VALUES (?, ?)`;

con.query(sql, [headline, content], function (err, result) {
  if (err) return res.json({ message: err });

  if (result) {
    res.json({
      message: 'news update saved',
    });
  } else {
    res.json({
      message: 'problems happened',
    });
  }
});
Sign up to request clarification or add additional context in comments.

5 Comments

I refactored to this and it still isn’t actually saving anything to the db, and my res.json isn’t working either. All my other routes in this api work fine though?
Addendum: the con.query function is getting called, cuz it’s console.logging “saved” and shit from within the callback, it just isn’t actually functioning
@MixedByInstinct Can you post the result of console.log(req.body) and the structure of the news table?
@Ipizzinidev on further inspection, it’s saving to the db just fine. res.json isn’t working but my original question has been answered lol. Thanks everyone 😅
Final update: I was missing an e.preventDefault() call in my frontend so the page was refreshing instead of displaying the response where I wanted. It’s been a long week lol
0

your syntax typo, just change to

let sql = `INSERT INTO news (Headline, Content) VALUES (${headline}, ${content})`;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.