skip to Main Content
public function updateMovie(Request $request, $id)
{
    $request->title;
    $movie = DB::connection('mysql2')->select('UPDATE cb_video SET title='.$request->title.' WHERE videoid=' . $id);
    return response()->json(['status' => 'success', 'data' => $movie]);
}

I am trying to update title in db but its throwing me error of

SQLSTATE[42000]: Syntax error or access violation: 1064 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
videoid=1449’ at line 1 (SQL: UPDATE cb_video SET title= WHERE
videoid=1449)

what can I do?

3

Answers


  1. I hope you find this helpful

    $title = 'New Title'; 
    
    DB::table('table_name')->where('id', $id)->update(['title' => $title]);
    

    Thanks

    Login or Signup to reply.
  2. there are many better ways in laravel than what you are doing you could use query builder or eloquent to do your task and in a secure way

    for example

        public function updateMovie(Request $request, $id)
        {
            $movie = Your_Movie_Modal::findbyId($id);
            $movie->title = $request->title;
            $movie->update();
            return response()->json(['status' => 'success', 'data' => $movie]);
        }
    

    and as I can see in your error it mean that you are not sending the title in the request

    Login or Signup to reply.
  3. DB::connection('mysql2')->table('cb_video')
    ->where('videoid', $id)
    ->update(['title' => $request->title]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search