[Fixed] Database table does not update

Issue

it doesn’t show any error but database table doesn’t update

id and status taken from HTML form

router.get('/update_todo/:todo_id',async function(req,res){
const id = req.body.todo_id;
const status = req.body.complete;
const queryString = "UPDATE todos SET completed = ? WHERE todo_id = ?";
con.query(queryString,[id,status],function(err,rows,fields){
    if(err)
    {
        console.log(err.message)
    }
    else
    {
        console.log("Updated successfull")
         
    }
})

})

my output gives

fieldCount  0
affectedRows    0
insertId    0
serverStatus    2
warningCount    0
message "(Rows matched: 0  Changed: 0  Warnings: 0"
protocol41  true
changedRows 0

Solution

What you have to do, you have to change the sequence of array member in array to [status, id] from [id,status]

And also as mentioned by Robert Kawecki, your request has to be a POST request to fetch status value from the body.

router.post('/update_todo/:todo_id',async function(req,res){
    const id = req.params.todo_id;
    const status = req.body.complete;
    const queryString = "UPDATE todos SET completed = ? WHERE todo_id = ?";
    con.query(queryString,[status, id],function(err,rows,fields){
        if(err)
        {
            console.log(err.message)
        }
        else
        {
            console.log("Updated successfull")
         
        }
    });

});

Leave a Reply

(*) Required, Your email will not be published