I want to create a Registration form. So, I have created a RegShortController.php
file and after I have created view file called RegShort.blade.php
. In RegShortController.php
file , I have used a function called insert. But , When I run the Web Site , I got this error -
"
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into
academic(
name,
username,
pw) values (, , ))
"
So , How to Fix this ??
Here is the RegShort.blade.php file
Unfortunately , I have removed name attributes in username and password when I edit this Thread. Now , I fixed it.
<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>
Here is the RegShortController.php file
<?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);
}
}
Here is the Route that I have created.
Route::any('/RegShort', 'RegShortController@insert')->name('RegShort');
name
field must be non empty, so u should validate data before inserting it (server side validation):
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 would be nice to validate data by javascript as well before sending data to server (client side validation).
Edit ur HTML, uve missed name attributes for ur inputs:
<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>
Also change ur routes:
Route::get('/RegShort', 'RegShortController@index')->name('RegShort');
Route::post('/RegShort', 'RegShortController@insert');
It seems that you are unable to get data from the $req object. Be sure to check your data before inserting into the table.
you dont have name attribute for username and password
<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>
Try to update html:
You are messing: name attribute here
<input type="text" name="username" class="form-control" required>
<input type="password" name="password" class="form-control">
Code:
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.