-1

I have done with getting date and deleting data, but when it comes to posting data, I'm stuck with this error. I have tried it a lot time, but couldn't find it. I am using Postman for posting data. I am facing this issue,:

code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "You have an error in your SQL syntax; check the manual that 
corresponds to your MariaDB server version for the right syntax to use near '= 
`name` = 'african', `tagline` = 'black', `description` = 'asthetic', `image...' 
at line 1",
sqlState: '42000',
 index: 0,"

This is my code

 const express= require('express')
 const bodyParser=require('body-parser')
 const mysql=require('mysql')

 const app= express()
 const port=process.env.PORT || 5000


 app.use(bodyParser.urlencoded({extended:false}))

app.use(bodyParser.json())

//mysql
const pool=mysql.createPool({

 connectionLimit:10,
 host           :'localhost',
 user           :'root',
 password       :'',
 database       :'nodejs_beers'

})

  //Get All beers
  app.get('',(req,res)=>{

    pool.getConnection((err,connection)=>{

        if(err) throw err
        console.log(`connected as id ${connection.threadID}`)

        connection.query('SELECT * from beers',(err,rows)=>{

            connection.release()

            if(!err){
                res.send(rows)
            }
            else{
                console.log(err)

            }
        })

       })


   })

    //Get All beers
   app.get('/:id',(req,res)=>{

    pool.getConnection((err,connection)=>{

        if(err) throw err
        console.log(`connected as id ${connection.threadID}`)

        connection.query('SELECT * from beers WHERE id=?',[req.params.id],(err,rows)=>{

            connection.release()

            if(!err){
                res.send(rows)
            }
            else{
                console.log(err)

            }
        })

      })


   })

   //delete A beer by id
    app.delete('/:id',(req,res)=>{

      pool.getConnection((err,connection)=>{

        if(err) throw err
        console.log(`connected as id ${connection.threadID}`)

        connection.query('DELETE from beers WHERE id = ?',[req.params.id],(err,rows)=>{

            connection.release()

            if(!err){
                res.send(`Beer with the id ${[req.params.id]} has been deleted`)
            }
            else{
                console.log(err)

            }
        })

      })


   })

   //Inser A beer 
   app.post('',(req,res)=>{

    pool.getConnection((err,connection)=>{

        if(err) throw err
        console.log(`connected as id ${connection.threadID}`)
        const params=req.body

        connection.query('INSERT INTO beers SET = ?',params,(err,rows)=>{

            connection.release()

            if(!err){
                res.send(`Beer with the id: ${params.id} has been added`)
            }
            else{
                console.log(err)

            }
        })

        console.log(req.body)

       })


    })







    //listen on environment port
 app.listen(port,()=>console.log('listen on port  $(port)'))
               
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

Your INSERT INTO... SET... syntax is a bit off. You've included an = where it shouldn't be (unless you can link to an authoritative source that states otherwise).

Instead, your INSERT query should look something more like:

connection.query('INSERT INTO beers SET ?',params,(err,rows)=>{ // ...
esqew
  • 42,425
  • 27
  • 92
  • 132