I
I
Ivan Ivanovich2020-07-15 07:28:38
MySQL
Ivan Ivanovich, 2020-07-15 07:28:38

Prepared query not working?

Hello, please tell me why the prepared request is not working

const writeData = await getDataDB('INSERT INTO test (ownId, email, ip, category, productId) VALUES (?, ?, ?, ?, ?)', ["1212", "[email protected]", "12.12.12", "test", "1"]);


Error: UnhandledPromiseRejectionWarning: 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 '?, ?, ?, ?)' at line 1

But if you write the values ​​in advance

const writeData = await getDataDB('INSERT INTO test (ownId, email, ip, category, productId) VALUES ("12324", "[email protected]", "12.12", "test", "11")');


Everything works fine.

PS Function

const getDataDB = (q, p = '')=>{
  return new Promise((resolve,reject)=>{
    pool.query(q, [p], (err,result)=>{
      if(err) reject(err);

      resolve(result);
    });
  });
};


Also if I do something like this:

const writeData = await getDataDB(`INSERT INTO test (ownId, email, ip, category, productId) VALUES ("123245", ${email}, "12.12", "test", "11")`);


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 '@ss.ss, "12.12", "test", "11")' at line 1
mail was in this case [email protected]

All equals error

Please tell me why the option with placeholders does not work? I seem to be doing everything right, passing the values ​​\u200b\u200bin the array, that's it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-07-15
@IwanQ

Apparently, the one who wrote the getDataDB function did not provide an option with placeholders.
See the documentation for this function or its code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question