我是laravel和php的新手。我无法在laravel中显示查询结果。我在我的程序中写了这个。
routes.php文件
Route::get('books/see', function()
{
return View::make('books.absen');
});
Route::post('books/absen','BookController@absen');
BookController.php
public function absen()
{
$ruang = Input::get('ruangn');
$results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));
return Redirect::to('books/see');
}
absen.blade.php
<select name="ruangn" class="form-control" method="post" action="{{URL::to('books/absen')}}">
<?php
for( $i=1; $i<19; $i++ )
{
?>
<option>
<?php echo $i;
}
?>
</option>
</select>
<input type="submit" name="submit" value="Oke" class="btn btn-info">
<table class="table table-bordered table-striped">
<thead>
<th>No</th>
<th>Name</th>
<th>ISBN</th>
<th>TA</th>
<th>TB</th>
</thead>
<tbody>
@foreach ($results as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->isbn}}</td>
<td>{{$value->ta}}</td>
<td>{{$value->tb}}</td>
</tr>
@endforeach
</tbody>
</table>
错误是未定义变量:结果(查看:... \ absen.blade.php)我对此非常厌倦。请帮忙
答案 0 :(得分:3)
您应该在absen()
操作中呈现视图,而不是重定向。在重定向时,您刚刚从数据库中选择的数据全部消失。
试试这个:
public function absen()
{
$ruang = Input::get('ruangn');
$results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));
return View::make('books/see')->with('results', $results);
}
此外,您需要检查视图中是否存在$results
,因为您还希望在没有结果时显示它
@if(isset($results))
<table class="table table-bordered table-striped">
<thead>
<th>No</th>
<th>Name</th>
<th>ISBN</th>
<th>TA</th>
<th>TB</th>
</thead>
<tbody>
@foreach ($results as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->isbn}}</td>
<td>{{$value->ta}}</td>
<td>{{$value->tb}}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
AndréDaniel对他的评论非常正确。您的代码很容易注入SQL。你应该看看Laravels ORM Eloquent或query builder。至少,使用参数绑定:
DB::select(DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = ?"), array($ruang));
以下是查询构建器的示例(Thanks @AndréDaniel)
DB::table("book")->where("ta", $ruang)->get()
答案 1 :(得分:0)
您必须使用变量制作视图。
这是你应该做的:
routes.php文件
Route::get('books/see', function()
{
return View::make('books.absen');
});
Route::post('books/absen','BookController@absen');
BookController.php
public function absen()
{
$ruang = Input::get('ruangn');
$results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));
return View::make('books.absen')->with(array('results' => $results));
}
absen.blade.php
<select name="ruangn" class="form-control" method="post" action="{{URL::to('books/absen')}}">
<?php
for( $i=1; $i<19; $i++ )
{
?>
<option>
<?php echo $i;
}
?>
</option>
</select>
<input type="submit" name="submit" value="Oke" class="btn btn-info">
@if(isset($results))
<table class="table table-bordered table-striped">
<thead>
<th>No</th>
<th>Name</th>
<th>ISBN</th>
<th>TA</th>
<th>TB</th>
</thead>
<tbody>
@foreach ($results as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->isbn}}</td>
<td>{{$value->ta}}</td>
<td>{{$value->tb}}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
顺便说一下,你应该使用Laravel表单函数Forms & HTML