0

I wrote an SQL query in nodejs like this

router.get('/bookingAppointment', function (req, res, next) {
var specialty = req.query.specialty;
var doctor = req.query.doctor;
var date = req.query.date;
var newdate = date.split('/').reverse().join('-');
var stm = "SELECT numericalOrder, date"
    + "FROM appointment "
    + "WHERE specialty = '" + specialty + "' AND doctor = '" + doctor 
    + "' AND date ='" + newdate + "' AND status = 0 "
    + "ORDER BY numericalOrder asc "
    + "LIMIT 1";
con.query(stm, function (err, results) {
    if (err) throw err;
    res.send(JSON.stringify({ "status": 200, "error": null, "response": results }));
});

});

But I get the following SQL syntax error

ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE specialty = 'KNTK' AND doctor = 'Nguyễn Văn A' AND date ='2018-7-15' AN' at line 1

Anyone know why my query break?

4
  • You should look into using statements. Commented Jul 15, 2018 at 4:32
  • I dont understand Commented Jul 15, 2018 at 4:33
  • You should avoid constructing SQL queries using string concatenation because it makes your code vulnerable to SQL injection attacks. Commented Jul 15, 2018 at 4:48
  • I understand. I will find information about statements Commented Jul 15, 2018 at 8:15

1 Answer 1

2

Check your sql,you will find that you have no space between date and FROM

var stm = "SELECT numericalOrder, date " //need to add a space here
    + "FROM appointment "
    + "WHERE specialty = '" + specialty + "' AND doctor = '" + doctor 
    + "' AND date ='" + newdate + "' AND status = 0 "
    + "ORDER BY numericalOrder asc "
    + "LIMIT 1";
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.