0

I am trying to insert a record into my MySQL table. When I submit my form, it puts a post request to '/admin', which inserts the posted data into the events table.

However, instead of inserting the record, the query keeps on giving me an error, because of Error: ER_PARSE_ERROR. To fix this, I looked at many sample insert queries for mysql, but I still do not know what I am doing wrong. Can someone give me a hand with this? Thank you!

Image of Error:

image of error

Image of mySQL table structure: image of mysql table

app.post('/admin', upload.none(), function(req, res) {
        //store the event
    var event_date = convertDateFormat(req.body.date);
    var start_time = convertTimeStringTo24Hours(req.body.starttime);
    var end_time = convertTimeStringTo24Hours(req.body.endtime);
    var sql = 
        "INSERT INTO events"
        + " (name, description, start_time, end_time, location, max_capacity, hidden_sign_up, other)"
        + " VALUES ?";
    var values = [
        req.body.eventname, 
        req.body.description, 
        event_date + " " + start_time,
        event_date + " " + end_time, 
        req.body.location, 
        req.body.attendee, 
        0, 
        ""
    ];
    con.query(sql, values, function(err, result) {
        if (err) throw err
        res.redirect('/admin')
    });
});

1 Answer 1

4

You need as many parameters as there are values to insert in the table, enclosed with parenthesis.

So you would need to replace this:

var sql = 
    "INSERT INTO events"
    + " (name, description, start_time, end_time, location, max_capacity, hidden_sign_up, other)"
    + " VALUES ?";

With:

var sql = 
    "INSERT INTO events"
    + " (name, description, start_time, end_time, location, max_capacity, hidden_sign_up, other)"
    + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"

As far as concerned you don't need to change the rest of your code (bind parameters should be passed as an array, which you are already doing).

Sign up to request clarification or add additional context in comments.

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.