Laravel向MySQL插入数据时CNIC字段为null的问题求助
问题:Laravel插入患者数据时CNIC字段填写后仍为NULL,触发数据库约束错误
插入MySQL数据库时触发Illuminate\Database\QueryException异常,错误信息:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cnic' cannot be null
表单中已填写CNIC字段,但插入数据时该字段为NULL(插入数据示例中cnic位置为?)。
控制器代码
<?php namespace App\Http\Controllers; use Haruncpi\LaravelIdGenerator\IdGenerator; use Illuminate\Http\Request; use App\Helpers\Helper; use Illuminate\Support\Facades\DB; use App\User; use App\Models\patient; class Helpercontroller extends Controller { function save(Request $request){ $fname= $request->fname; $lname= $request->lname; $cnic= $request->cnic; $contactno= $request->contactno; $gender= $request->gender; $age= $request->age; $dob= $request->dob; $city= $request->city; $address= $request->address; $husbandname= $request->husbandname; $fathername= $request->fathername; $bloodgroup= $request->bloodgroup; $primaryphy= $request->primaryphy; $clientname= $request->clientname; $maritalstatus= $request->maritalstatus; $emergencyno= $request->emergencyno; $category= $request->category; $data = DB::table('patients')->where('cnic',$cnic)->count(); if($data == 0) { $patientid = IdGenerator::generate(['table' => 'patients', 'length' => 6, 'prefix' =>'1']); $query= new patient; $query->patientid=$patientid; $query->fname=$fname; $query->lname=$lname; $query->cnic=$cnic; $query->contactno=$contactno; $query->gender=$gender; $query->age=$age; $query->dob=$dob; $query->city=$city; $query->address=$address; $query->husbandname=$husbandname; $query->fathername=$fathername; $query->bloodgroup=$bloodgroup; $query->clientname=$clientname; $query->maritalstatus=$maritalstatus; $query->emergencyno=$emergencyno; $query->category=$category; $query->save(); } else{ return back()->with ('fail','Patient already exists with the same CNIC Number'); } if ($query){ return back()->with ('success',$patientid ); } else{ return back()->with ('fail','something went wrong'); } } public function search() { return view ('registration'); } function searchpatient(Request $request){ $patientid = $request->input('patientid'); $data = DB::table('patients')->where(['patientid'=>$patientid])->first(); return $data; } function searchpatientcnic(Request $request){ $cnic = $request->input('cnic'); $data = DB::table('patients')->where(['cnic'=>$cnic])->first(); return $data; } }
Web路由代码
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\login; use App\Http\Controllers\MainController; use App\Http\Controllers\add1; use Illuminate\Http\Request; use App\Http\Controllers\Helpercontroller; /* |-------------------------------------------------------------------------- | 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('/registration',[Helpercontroller::class,'search']); Route::get('/searchpatient' , [Helpercontroller::class,'searchpatient']); Route::get('/searchpatientcnic' , [Helpercontroller::class,'searchpatientcnic']);
Blade视图代码
<div class="container"> <div class="row"> <div class="col-lg-12"> <div class="col-md-3 col-md-offset-3 d4 " style="margin-top: 50px"> <h4>Patient Registration</h4> </div><br> @if(Session::get('success')) <div class="alert alert-success"> {{Session::get('success')}} </div> @endif @if(Session::get('fail')) <div class="alert alert-danger"> {{Session::get('fail')}} </div> @endif <div class="block border mb-2"> <div class="card-body pt-2 pb-2"> <div class="row"> <div class="col-lg-2"> <form action="save" method="post" autocomplete="off"> @csrf <label>First Name:<span class="d2">*</span></label> <input placeholder="" autofocus type="text" name="fname" class="form-control form-control-sm" tabIndex="2" id="fname" value="{{old('fname')}}" required > </div> <div class="col-lg-2"> <label>Last Name:</label> <input placeholder="" type="text" name="lname" class="form-control form-control-sm" tabIndex="3" id="lname" value="{{old('lname')}}" > </div> <div class="col-lg-2"> <label>Age:</label> <input placeholder="" type="text" name="age" class="form-control form-control-sm" tabIndex="4" id="age" value="{{old('age')}}" > </div> <div class="col-lg-2"> <label>DOB:</label> <input placeholder="" type="date" name="dob" class="form-control form-control-sm" tabIndex="5" id="dob" value="{{old('dob')}}" > </div> <div class="col-lg-2"> <label>Gender:<span class="d2">*</span></label> <select class="form-control form-control-sm sel" name="gender" tabIndex="6" id="gender" value="{{old('gender')}}" required > <option value="">SELECT</option> <option value="MALE">MALE</option> <option value="FEMALE">FEMALE</option> <option value="NEUTER">NEUTER</option> </select> </div> <div class="col-lg-2"> <label>Contact No:<span class="d2">*</span></label> <input placeholder="" type="number" name="contactno" class="form-control form-control-sm" tabIndex="7" id="contactno" data-validate="Valid Contact# is required" pattern="^\d{11}$" tabIndex="6" value="{{old('contact')}}" required > </div> <div class="col-lg-2"> <label>Father Name:</label> <input placeholder="" type="text" name="fathername" class="form-control form-control-sm" tabIndex="8" id="fathername" tabIndex="7" value="{{old('fathername')}}" > </div> <div class="col-lg-2"> <label>Husband Name:</label> <input placeholder="" type="text" name="husbandname" class="form-control form-control-sm" id="husbandname" tabIndex="9" value="{{old('husbandname')}}" > </div> <div class="col-lg-2"> <label>CNIC: <span class="d2">*</span></label> <input placeholder="" type="number" name="cnic" class="form-control form-control-sm" id="cnic" tabIndex="10" pattern="^\d{13}$" required > </div> <div class="col-lg-2"> <label>City: <span class="d2">*</span></label> <input placeholder="" type="text" name="city" class="form-control form-control-sm" id="city" tabIndex="11" value="{{old('city')}}" required > </div> <div class="col-lg-2"> <label>Category:</label> <input placeholder="" type="text" name="category" class="form-control form-control-sm" id="category" tabIndex="12" value="{{old('category')}}" > </div> <div class="col-lg-2"> <label>Emergency No:</label> <input placeholder="" type="number" name="emergencyno" class="form-control form-control-sm" id="emergencyno" tabIndex="13" value="{{old('emergencyno')}}" > </div> <div class="col-lg-2"> <label>Marital Status:</label> <select class="form-control form-control-sm sel" name="maritalstatus" tabIndex="14" id="maritalstatus" value="{{old('maritalstatus')}}" > <option value="">SELECT</option> <option value="NOT SPECIFIED">NOT SPECIFIED</option> <option value="SINGLE">SINGLE</option> <option value="MARRIED">MARRIED</option> <option value="WIDOW">WIDOW</option> <option value="WIDOWER">WIDOWER</option> <option value="DIVORCED">DIVORCED</option> <option value="SEPARATED">SEPARATED</option> <option value="UNKNOWN">UNKNOWN</option> </select> </div> <div class="col-lg-2"> <label>Blood Group:</label> <select class="form-control form-control-sm sel" name="bloodgroup" tabIndex="15" id="bloodgroup" value="{{old('bloodgroup')}}" > <option value="">SELECT</option> <option value="NOT SPECIFIED">NOT SPECIFIED</option> <option value="A POSITIVE">A POSITIVE</option> <option value="A NEGATIVE">A NEGATIVE</option> <option value="B POSITIVE">B POSITIVE</option> <option value="B NEGATIVE">B NEGATIVE</option> <option value="O POSITIVE">O POSITIVE</option> <option value="O NEGATIVE">O NEGATIVE</option> <option value="AB POSITIVE">AB POSITIVE</option> <option value="AB NEGATIVE">AB NEGATIVE</option> </select> </div> <div class="col-lg-2"> <label>Primary Phy:</label> <input placeholder="" type="text" name="primaryphy" class="form-control form-control-sm" id="primaryphy" tabIndex="16" value="{{old('primaryphy')}}" > </div> <div class="col-lg-2"> <label>Address:</label> <input placeholder="" type="text" name="address" class="form-control form-control-sm" id="address" tabIndex="17" value="{{old('address')}}" > </div> <div class="col-lg-2"> <label>Client Name:</label> <select class="form-control form-control-sm sel" name="clientname" tabIndex="18" id="clientname" value="{{old('clientname')}}" > <option value="">SELECT</option> <option value="SPECIAL FINANCIAL APPROVAL">SPECIAL FINANCIAL APPROVAL</option> <option value="SEHAT CARD">SEHAT CARD</option> <option value="CCB">CCB</option> <option value="BVH HOSPITAL">BVH HOSPITAL</option> <option value="CCB HOSPITAL/INDOOR">CCB HOSPITAL/INDOOR</option> <option value="BH-MLC">BH-MLC</option> </select> </div> <div class="col-lg-2"> <label>Patient ID:</label> <input placeholder="" type="number" name="patientid" id="patientid" class="form-control PAT form-control-sm d2" disabled > </div> <div class="col-lg-2"> <button type="submit" class="btn-block col-lg-12" value="" tabIndex="19">Register</button> </div> <div class="col-lg-2"> <button type="submit" class="btn-block col-lg-12 disabled" value="" >Update</button> </div> <div class="col-lg-2"> <button type="submit" class="btn-block col-lg-12" value="" >Refresh</button> </div> <div class="col-lg-2"> <button type="submit" class="btn-block col-lg-12" value="" >Clear</button> </div> <div class="col-lg-2"> <button type="submit" class="btn-block col-lg-12 disabled">Update Contact</button> </div> <div class="col-lg-2"> <button type="submit" class="btn-block col-lg-12 disabled">Update Info</button> </div> <div class="col-lg-2"> <label>Search Patient :</label> <input placeholder="Enter Patient ID" type="number" name="patientid" id="selectpatientid" class="form-control form-control-sm d2" > <input placeholder="Enter Patient CNIC" type="number" name="cnic" id="selectpatientcnic" class="form-control form-control-sm d2" > </div> </form> </div> </div> </div> </div> </div> </div> </div>
问题排查与解决方案
核心问题1:表单内存在同名cnic字段
视图代码中,注册区域的cnic输入框与搜索区域的cnic输入框name属性完全相同:
- 注册用:
<input ... name="cnic" ...> - 搜索用:
<input ... name="cnic" id="selectpatientcnic" ...>
表单提交时,同名参数会被后出现的字段值覆盖,而搜索框的cnic通常为空,导致控制器中$request->cnic拿到的是空值,最终插入数据库时为NULL。
核心问题2:缺少POST路由
表单的提交方法为post,但路由文件中仅定义了GET路由,没有为save方法配置POST路由,导致请求无法正确映射到控制器的save方法,进一步引发参数异常。
额外优化点
表单内多个按钮使用type="submit",除了Register按钮,其他按钮(如Refresh、Clear)应改为type="button",避免误触表单提交。
具体修复步骤
修改搜索区域的cnic字段name属性
将搜索部分的cnic输入框name改为search_cnic:<input placeholder="Enter Patient CNIC" type="number" name="search_cnic" id="selectpatientcnic" class="form-control form-control-sm d2" >添加POST路由
在web.php中添加如下路由:Route::post('/save', [Helpercontroller::class, 'save']);调整非提交按钮的类型
例如:<button type="button" class="btn-block col-lg-12" value="" >Refresh</button> <button type="button" class="btn-block col-lg-12" value="" >Clear</button>
内容的提问来源于stack exchange,提问作者user19704437
相关产品推荐
相关产品推荐

