2017-08-04 144 views
0

我想創建一個註冊表單。所以,我創建了一個RegShortController.php文件,並創建了名爲RegShort.blade.php的視圖文件。在RegShortController.php文件中,我使用了一個名爲insert的函數。但是,當我運行的網站,我得到這個錯誤 -如何解決名爲SQLSTATE的Laravel和MySql錯誤[23000]

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into學術(,用戶名, PW ) values (, ,))

那麼,如何解決這一問題?

enter image description here

這裏是RegShort.blade.php文件

不幸的是,我已刪除名稱中的用戶名和密碼屬性,當我修改此主題。現在,我修好了。

<form action="{{ route('RegShort') }}" method="post" enctype="multipart/form-data"> 

{{ csrf_field() }} 

<div class="form-group"> 
    <label>Name : *</label> 
    <input type="text" class="form-control" name="name"> 
    </div> 

    <div class="form-group"> 
    <label>Username : *</label> 
    <input type="text" class="form-control" name="username" required> 
    </div> 

    <div class="form-group"> 
    <label>Password : *</label> 
    <input type="password" class="form-control" name="password"> 
    </div> 

    <button type="submit" class="btn btn-primary" name="submit">Submit</button> 

</form> 

這裏是RegShortController.php文件

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use DB; 

class RegShortController extends Controller 
{ 

    public function index() 
    { 
     return view('RegShort'); 
    } 

    function insert(Request $req) 
    { 

     $name = $req->input('name'); 
     $username = $req->input('username'); 
     $password = $req->input('password'); 

     $data = array("name"=>$name,'username'=>$username,"pw"=>$password); 

     DB::table('academic')->insert($data); 

    } 

} 

這裏是我所創建的路線。

Route::any('/RegShort', '[email protected]')->name('RegShort'); 
+1

值賦給name列或從表中 – scaisEdge

+0

刪除約束你爲什麼不使用'Model' ..? –

+0

您的'name'屬性沒有'用戶名'輸入或'密碼'輸入,這意味着這些輸入的數據無論如何都不會出現在請求中。 –

回答

4

name領域必須是非空的name屬性,你還是你應該將它(服務器端驗證)之前驗證數據:

function insert(Request $req) 
{ 

    $this->validate($req, [ 
     'name' => 'required' 
    ]); 

    $name = $req->input('name'); 
    $username = $req->input('username'); 
    $password = $req->input('password'); 

    $data = array("name"=>$name,'username'=>$username,"pw"=>$password); 

    DB::table('academic')->insert($data); 

} 

BTW會在將數據發送到服務器(客戶端驗證)之前,要善於通過javascript驗證數據。

編輯烏爾HTML,UVE錯過了名烏拉圭回合輸入屬性:

<form action="{{ route('RegShort') }}" method="post" enctype="multipart/form-data"> 

{{ csrf_field() }} 

<div class="form-group"> 
    <label>Name : *</label> 
    <input type="text" class="form-control" name="name"> 
    </div> 

    <div class="form-group"> 
    <label>Username : *</label> 
    <input type="text" class="form-control" name="username" required> 
    </div> 

    <div class="form-group"> 
    <label>Password : *</label> 
    <input type="password" name="password" class="form-control"> 
    </div> 

    <button type="submit" class="btn btn-primary" name="submit">Submit</button> 

</form> 

而且改變烏爾路線:

Route::get('/RegShort', '[email protected]')->name('RegShort'); 
Route::post('/RegShort', '[email protected]'); 
+0

如果真的需要他的'name'字段,他還可以爲其添加'required'屬性。 – iArcadia

+0

@ЛевМакаренко - 現在它說「本地主機頁面不能正常工作localhost重定向你太多次了。」所以,如何解決這個問題? –

+0

@iArcadia - 我做到了..但是,它仍然不起作用。 –

2

看來你是無法獲得從$ REQ對象數據。插入表格之前一定要檢查您的數據。

+0

我該怎麼做? –

2

你沒有用戶名和密碼

<div class="form-group"> 
    <label>Name : *</label> 
    <input type="text" class="form-control" name="name"> 
    </div> 

    <div class="form-group"> 
    <label>Username : *</label> 
    <input type="text" name="username" class="form-control" required> 
    </div> 

    <div class="form-group"> 
    <label>Password : *</label> 
    <input type="password" name="password" class="form-control"> 
    </div> 
+0

不幸的是,當我編輯此主題時,我已經在用戶名和密碼中刪除了名稱屬性。現在,我修好了。 –

2

嘗試更新HTML:

你搞亂:name attribute here

<input type="text" name="username" class="form-control" required> 
<input type="password" name="password" class="form-control"> 

代碼:

<form action="{{ route('RegShort') }}" method="post" enctype="multipart/form-data"> 

{{ csrf_field() }} 

<div class="form-group"> 
    <label>Name : *</label> 
    <input type="text" class="form-control" name="name"> 
    </div> 

    <div class="form-group"> 
    <label>Username : *</label> 
    <input type="text" name="username" class="form-control" required> 
    </div> 

    <div class="form-group"> 
    <label>Password : *</label> 
    <input type="password" name="password" class="form-control"> 
    </div> 

    <button type="submit" class="btn btn-primary" name="submit">Submit</button> 

</form> 
+0

不幸的是,我已經刪除了用戶名中的名稱屬性和密碼,當我編輯這個線程。現在,我修好了。 任何,幫助該名稱空錯誤? –

相關問題