【发布时间】:2017-09-03 04:53:29
【问题描述】:
我想通过执行具有 id = $id 的 SQL 查询来填充表。
代码 ajax:
$('#ingredientTable').DataTable({
"ajax": {
"url": "api/ingredients",
"type": "post",
"data" : {
'_token': token,
"id" : '1' ,
}
},
"columns":[
{data:'name', name: 'ingredients.name'},
],
});
在路线中:
route::post('api/ingredients', function() {
return Datatables::of(
DB::select('SELECT * FROM ingredients
INNER JOIN ingredient_product ON ingredient_product.ingredient_id = ingredients.id
WHERE ingredients.id=' . $id . ' ;'))
->make(TRUE);
});
我收到此错误:
POST http://localhost:8080/pizzeria/public/api/ingredients 500(内部服务器错误)和 DataTables 警告:表 id=ingredientTable - Ajax 错误。有关此错误的更多信息,请参阅http://datatables.net/tn/7
【问题讨论】:
-
你永远不会从帖子中获得 id。此外,您应该使用参数而不是尝试引用您自己的变量,这会让您为 SQL 注入打开大门。
-
@aynber
select()是参数化的,只有DB::raw()暴露你。 -
@Ohgodwhy 以该格式发送 $id 未参数化。
-
@aynber 你是对的,它应该是
DB::raw()无论如何,这很可能是 500 ISE 的原因。select()使用不正确。我会更新我的答案来解决这个问题。 -
您实际上可以像这样使用 DB::select。其他东西会引发错误。
标签: php jquery mysql ajax laravel