I have codes see below which is works.
$city = City::find(1);
$city->location = DB::raw('geomfromtext('point(' . $location . ')')');
$city->save();
Question 1:
Is there SQL injection issue in the codes above because I using DB::raw() which sticking the string(variable) in without processing ?
Question 2:
If yes, there is SQL injection issue in the codes, how to bind the variables into DB::raw()?
Thanks
I tried below.
DB::connection('datadb')->table('city')->where('id', '=', $profile->id)->update(['location' => DB::raw("(GEOMFROMTEXT('POINT(54.8765696 -2.9261824)'))")]);
It works, but if I try to do like below. It failed.
$location = ‘54.8765696 -2.9261824’;
DB::connection(‘datadb’)->table(‘city’)->setBindings([$location, $profile->id])->whereRaw(‘id = ?’)->update([‘location’ => DB::raw("(GEOMFROMTEXT(‘POINT(?)’))")]);
I got the error "SQLSTATE[HY093]: Invalid parameter number (Connection: datadb, SQL: update city
set location
= (GEOMFROMTEXT(‘POINT(-33.742612777347 151.27899169922)’)) where id = 1126)""
But the query "update city set location = (GEOMFROMTEXT(‘POINT(-33.742612777347 151.27899169922)’)) where id = 1126;" definitely works.
2
Answers
What docs say about raw expressions/methods:
and
A tip is that you can pass an array of bindings to most raw query methods to avoid SQL injection.: