1

I'm receiving SyntaxError: Invalid or unexpected token for the line:

var databaseQuery = "INSERT INTO userinfo

I was wondering if someone could help me format my code because this is my first time using MYSQL and javascript so I'm not sure why this isn't working or if I'm using the right format because I've seen different examples on google and wasn't sure which is the right way to write this.

app.post('/registration', urlencodedParser, function(req, res) {
console.log(req.body);
var databaseQuery = "INSERT INTO userinfo
                    (firstName,lastName,email,birthmonth,birthday,birthyear,university,country,state,zip,password)
                    VALUES ('"
                        + req.body.firstName +"','"
                        + req.body.lastName +"','"
                        + req.body.email +"','"
                        + req.body.birthmonth +"','"
                        + req.body.birthday +"','"
                        + req.body.birthyear +"','"
                        + req.body.university +"','"
                        + req.body.country +"','"
                        + req.body.state +"','"
                        + req.body.zip +"','"
                        + req.body.password +"')";

connection.query(databaseQuery, function(err, result) {
    if (err) {
        throw err;
    }else{
        console.log(result.affectedRows + "records updated.");
    }
}
res.render('registration', {qs: req.query});
});
E tom
  • 117
  • 2
  • 9

1 Answers1

1

The syntax errors occur because you're trying to code one statement in multiple lines and it's not allowed. If you want to do that, then you'll need to use the back tick (`) character in place of the outer double quotes. You can also use the ${} delimiter inside your string to include any JavaScript code/variables.

I think there's also another syntax error in your connection.query() -- you need a closing ")" to close the callback/anonymous function.

How about this modified version?

app.post('/registration', urlencodedParser, function(req, res) {
console.log(req.body);
var databaseQuery = `INSERT INTO userinfo
(firstName,lastName,email,birthmonth,birthday,birthyear,university,country,state,zip,password)
VALUES ('${req.body.firstName}',
        '${req.body.lastName}',
        '${req.body.email}',
        '${req.body.birthmonth}',
        '${req.body.birthday}',
        '${req.body.birthyear}',
        '${req.body.university}',
        '${req.body.country}',
        '${req.body.state}',
        '${req.body.zip}',
        '${req.body.password}')`;
connection.query(databaseQuery, function(err, result) {
    if (err) {
        throw err;
    }else{
        console.log(result.affectedRows + "records updated.");
    }
});
res.render('registration', {qs: req.query});
});
Christian Hur
  • 429
  • 3
  • 10