我试图将数据插入数据库,但是,我未能做到这一点。我试过大量的视频和上网,但都不成功。我的代码如下,我已经创建了控制器,并从控制器传递请求,它不接受。
<form action="/StudentInsert" method="post">
<div class="form-group">
<div class="col-sm-10">
<p>
<input type="text" name="txtFname" class="form-control" placeholder="First name">
<input type="text" name="txtLname" class="form-control" placeholder="Last name">
</p>
</div>
<div class="col-sm-10">
<p>
<input type="text" name="txtContact" class="form-control" placeholder="Contact">
<input type="text" name="txtEmail" class="form-control" placeholder="Email">
</p>
</div>
<div class="col-sm-10">
<p>
<button type="submit" name="btnSubmit" class="btn btn-outline-primary">Submit</button>
</p>
</div>
</div>
</form>
这是我的控制器文件
<?php
namespace AppHttpControllers;
use AppHttpControllersController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
class StudInsertController extends Controller
{
//Insert Form
public function StudentInsertForm()
{
return view('StudInsert');
}
public function StudentInsert(Request $request)
{
$first_name = $request->input('txtFname');
$last_name = $request->input('txtLname');
$contact = $request->input('txtContact');
$email = $request->input('txtEmail');
$data = array('first_name' => $first_name, 'last_name' => $last_name, 'contact' => $contact, 'email' => $email);
DB::table('student')->insert($data);
echo "Record inserted Successfully.<br/>";
echo '<a href="/StudView">Click Here</a>';
}
}
web.php
<?php
use IlluminateSupportFacadesRoute;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('StudInsert');
});
Route::get('StudentInsertForm', 'AppHttpControllersStudentInsertController@StudentInsertForm');
Route::get('StudentInsert', 'AppHttpControllersStudentInsertController@StudentInsert');
你必须使用post路由而不是get在web.php文件中检索post数据,像这样
Route::post('StudentInsert', 'AppHttpControllersStudentInsertController@StudentInsert');
替换web.php
Route::get('StudentInsert', 'AppHttpControllersStudentInsertController@StudentInsert');
和这个新的
Route::post('StudentInsert', 'AppHttpControllersStudentInsertController@StudentInsert');
如果您能提供您得到的确切错误,那就太好了。但我已经看到几个bug了。你正在尝试从表单向你的Laravel应用程序发送一个post请求,但是在你的路由列表中,你没有等待任何post请求。
你可以这样做:
- 确保您在
web.php
中接受post请求:
Route::post('StudentInsert', 'AppHttpControllersStudentInsertController@StudentInsert');
- Laravel默认要求在任何形式发布CSRF令牌。所以你的表单应该是这样的:
<form action="/StudentInsert" method="post">
@csrf
<!-- Equivalent to... -->
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<div class="form-group">
<div class="col-sm-10">
<p>
<input type="text" name="txtFname" class="form-control" placeholder="First name">
<input type="text" name="txtLname" class="form-control" placeholder="Last name">
</p>
</div>
<div class="col-sm-10">
<p>
<input type="text" name="txtContact" class="form-control" placeholder="Contact">
<input type="text" name="txtEmail" class="form-control" placeholder="Email">
</p>
</div>
<div class="col-sm-10">
<p>
<button type="submit" name="btnSubmit" class="btn btn-outline-primary">Submit</button>
</p>
</div>
</div>
</form>
更多细节见Laravel docs
我还建议使用Laravel表单帮助器