Home > Net >  update title in laravel
update title in laravel

Time:01-25

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?

CodePudding user response:

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

CodePudding user response:

I hope you find this helpful

$title = 'New Title'; 

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

Thanks

CodePudding user response:

DB::connection('mysql2')->table('cb_video')
->where('videoid', $id)
->update(['title' => $request->title]);
  • Related