Guest User

Untitled

a guest
Aug 17th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. const INSERT_PRODUCTS_QUERY = 'INSERT INTO products(name, price) VALUES('${name}',${price})';
  2.  
  3. {
  4. "code": "ER_PARSE_ERROR",
  5. "errno": 1064,
  6. "sqlMessage": "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 '{name}, ${price})' at line 1",
  7. "sqlState": "42000",
  8. "index": 0,
  9. "sql": "INSERT INTO products(name, price) VALUES(${name}, ${price})"
  10. }
  11.  
  12. const express = require('express');
  13. const cors = require('cors');
  14. const mysql = require('mysql');
  15.  
  16. const app = express();
  17.  
  18. const SELECT_ALL_PRODUCTS_QUERY = 'SELECT * FROM products';
  19.  
  20. const connection = mysql.createConnection({
  21. host: 'localhost',
  22. user: 'root',
  23. password: 'password',
  24. database: 'react_sql'
  25. });
  26.  
  27. connection.connect(err => {
  28. if(err) {
  29. return err;
  30. }
  31. });
  32.  
  33. app.use(cors());
  34.  
  35. app.get('/', (req, res) => {
  36. res.send('go to /products to see products')
  37. });
  38.  
  39. app.get('/products/add', (req, res) => {
  40. const { name, price } = req.query;
  41. const INSERT_PRODUCTS_QUERY = 'INSERT INTO products(name, price) VALUES('${name}',${price})';
  42.  
  43. connection.query(INSERT_PRODUCTS_QUERY, (err, results) => {
  44. if(err) {
  45. return res.send(err);
  46. } else {
  47. return res.send('successfully added products');
  48. }
  49. });
  50.  
  51. })
  52.  
  53. app.get('/products', (req, res) => {
  54. connection.query(SELECT_ALL_PRODUCTS_QUERY, (err, results) => {
  55. if(err) {
  56. return res.send(err)
  57. } else {
  58. return res.json({
  59. data: results
  60. })
  61. }
  62. });
  63. });
  64.  
  65. app.listen(4000, () => {
  66. console.log("listening port 4000");
  67. });
  68.  
  69. const INSERT_PRODUCTS_QUERY = `INSERT INTO products(name, price) VALUES('${name}',${price})`
  70.  
  71. INSERT INTO products(name, price) VALUES('".${name}."','".${price}."');
Add Comment
Please, Sign In to add comment