一般错误:1364:字段“dis”没有默认值

Posted

技术标签:

【中文标题】一般错误:1364:字段“dis”没有默认值【英文标题】:General error:1364 :Field 'dis' does not have a default value 【发布时间】:2021-02-28 20:54:10 【问题描述】:

我收到错误 1364。字段“dis”没有默认值。我在表单和 CRUD 操作方面进展顺利。通过表单将数据保存在数据库中时,出现此错误。创建表单重定向以显示视图。单击表单的提交按钮时出现此错误。我检查我的数据库数据没有存储在那里。

错误:

Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'dis' doesn't have a default value (SQL: insert into 
`students` (`full_name`, `dob`, `gender`, `cnic`, `marital_Status`, `nationality`, `religion`, 
`domicile`, `province`, `father_name`, `father_cnic`, `father_profession`, `guardian_name`, 
`guardian_cnic`, `email`, `address`, `tel_landline`, `tel_office`, `cell1`, `cell2`, 
`matric_obtained_marks`, `matric_total_marks`, `matric_marksheet_image`, `fsc_obtained_marks`, 
`fsc_total_marks`, `fsc_marksheet_image`, `nmdcat_obtained_marks`, `nmdcat_total_marks`, 
 `nmdcat_result_image`, `updated_at`,

这是我的控制器

class StudentsController extends Controller 

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
 public function index()
 
     return view ('index');
 

 /**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
 public function create()
 
     return view('create');
 

 /**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)

  $student= new student;
  $student->full_name=$request->full_name;
  $student->dob=$request->dob;
  $student->gender=$request->dis;
  $student->cnic=$request->cnic;
  $student->marital_Status=$request->marital_status;
  $student->nationality=$request->nationality;
  $student->religion=$request->religion;
  $student->domicile=$request->domicile;
  $student->province=$request->province;
  $student->father_name=$request->father_name;
  $student->father_cnic=$request->father_cnic;
  $student->father_profession=$request->father_profession;
  $student->guardian_name=$request->guardian_name;
  $student->guardian_cnic=$request->guardian_cnic;
  $student->email=$request->email;
  $student->address=$request->address;
  $student->tel_landline=$request->tel_lanline;
  $student->tel_office=$request->tel_office;
  $student->cell1=$request->cell1;
  $student->cell2=$request->cell2;
  $student->matric_obtained_marks=$request->matric_obtained_marks;
  $student->matric_total_marks=$request->matric_total_marks;
  $student->matric_marksheet_image=$request->matric_marksheet_image;
  $student->fsc_obtained_marks=$request->fsc_obtained_marks;
  $student->fsc_total_marks=$request->fsc_total_marks;
  $student->fsc_marksheet_image=$request->fsc_marksheet_image;
  $student->nmdcat_obtained_marks=$request->nmdcat_obtained_marks;
  $student->nmdcat_total_marks=$request->nmdcat_total_marks;
  $student->nmdcat_result_image=$request->nmdcat_result_image;
  $student->save();

  return redirect('/show');


这是我的表格

                <form action="/students" method="post">
      csrf_field()
                <div class="form-group col-md-9">
                <label for="exampleFormControlInput1">Name of applicant:</label>
                <input type="text" class="form-control" id="exampleFormControlInput1" name="full_name" placeholder="Enter your full name" required>
                </div>

                <div class="form-row">
                    <div class="form-group col-md-6">
                      <label for="example-date-input" class="col-6 col-form-label">Date of Birth:</label>
                        <div class="col-8">
                            <input class="form-control" type="date" value="2001-05-19" id="example-date-input" name="dob" required>
                    </div>
                    </div>
                    <div class="form-group col-md-6" id="gen">
                     <label for="example-date-input" class="col-3 col-form-label" id="gender">Gender:</label>
                     <div class="col-9" id="gnd">
                      <div class="form-check form-check-inline">
                        <label class="form-check-label">
                            <input class="form-check-input" type="radio" name="gender" id="inlineRadio1" value="male"> Male
                        </label>
                        </div>
                        <div class="form-check form-check-inline" id="fma">
                            <label class="form-check-label">
                                <input class="form-check-input" type="radio" name="gender" id="inlineRadio2" value="female"> Female
                            </label>
                        </div>
                    </div>
                    </div>
                    <div class="form-group col-md-4" id="dis">
                      <label for="exampleFormControlInput1" class="col-2 col-form-label">Disability:</label>
                        <div class="col-7">
                            <input class="form-control" type="text" id="exampleFormControlInput1" name="dis" required>
                    </div>
                    </div>
                </div>
                <div class="form-group col-md-9">
                    <label for="exampleFormControlInput1">CNIC:</label>
                    <input type="text" class="form-control" id="exampleFormControlInput1" name="cnic" placeholder="Enter your CNIC" required>
                </div>
                <div class="form-row">
                  <div class="form-group col-md-6">
                  <label for="example-date-input" class="col-6 col-form-label">Marital Status:</label>
                        <div class="col-6">
                            <input class="form-control" type="text" name="marital_status">
                    </div>
                    </div>
                    <div class="form-group col-md-5" id="nat">
                  <label for="example-date-input" class="col-4 col-form-label">Nationality:</label>
                        <div class="col-7">
                            <input class="form-control" type="text" name="nationality" required>
                    </div>
                    </div>
                    <div class="form-group col-md-5" id="rel">
                  <label for="example-date-input" class="col-4 col-form-label">Religion:</label>
                        <div class="col-7">
                            <input class="form-control" type="text" name="religion" required>
                    </div>
                    </div>
                </div>
                <div class="form-row">
                    <div class="form-group col-md-6">
                    <label for="example-date-input" class="col-4 col-form-label">Domicile:</label>
                    <div class="col-7">
                        <input class="form-control" type="text" name="domicile" placeholder="Your domicile" required>
                    </div>
                    </div>
                    <div class="form-group col-md-6" id="dom">
                    <label for="example-date-input" class="col-4 col-form-label">Province:</label>
                    <div class="col-7">
                        <input class="form-control" type="text" name="province" placeholder="Your province" required>
                    </div>
                    </div>
                </div>
                <div class="form-group col-md-9">
                    <label for="exampleFormControlInput1">Father Name:</label>
                    <input type="text" class="form-control" id="exampleFormControlInput1" name="father_name" placeholder="Enter your father name" required>
                </div>
                <div class="form-group col-md-9">
                    <label for="exampleFormControlInput1">Father CNIC:</label>
                    <input type="text" class="form-control" id="exampleFormControlInput1" name="father_cnic" placeholder="Enter father CNIC" required>
                </div>
                <div class="form-group col-md-9">
                    <label for="exampleFormControlInput1">Father Profession:</label>
                    <input type="text" class="form-control" id="exampleFormControlInput1" name="father_profession" placeholder="Enter father profession" required>
                </div>
                <div class="form-group col-md-9">
                    <label for="exampleFormControlInput1">Guardian Name:</label>
                    <input type="text" class="form-control" id="exampleFormControlInput1" name="guardian_name" placeholder="Enter your guardian name" required>
                </div>
                <div class="form-group col-md-9">
                    <label for="exampleFormControlInput1">Guardian CNIC:</label>
                    <input type="text" class="form-control" id="exampleFormControlInput1" name="guardian_cnic" placeholder="Enter guardian CNIC" required>
                </div>
                <div class="form-group col-md-9">
                    <label for="exampleFormControlInput1">Email Address:</label>
                    <input type="email" class="form-control" id="exampleFormControlInput1" name="email" placeholder="Enter your email address" required>
                </div>
                <div class="form-group col-md-9">
                    <label for="exampleFormControlTextarea1">Permanent and Mailing Address:</label>
                    <textarea class="form-control" id="exampleFormControlTextarea1" rows="5" name="address" placeholder="Enter your address" required></textarea>
                </div>
                <div class="form-row">
                    <div class="form-group col-md-8">
                        <label for="example-date-input" class="col-5 col-form-label">Tel (Landline):</label>
                        <div class="col-5">
                            <input class="form-control" type="tel" name="tel_landline" placeholder="Landline">
                        </div>
                    </div>
                    <div class="form-group col-md-8" id="tel1">
                        <label for="example-date-input" class="col-5 col-form-label">Tel (Office):</label>
                        <div class="col-5">
                            <input class="form-control" type="tel" name="tel_office" placeholder="Office">
                        </div>
                    </div>
                </div>
                <div class="form-row">
                    <div class="form-group col-md-7">
                        <label for="example-date-input" class="col-4 col-form-label">Cell 1:</label>
                        <div class="col-7">
                            <input class="form-control" type="tel" name="cell1" placeholder="0***-*******" required>
                        </div>
                    </div>
                    <div class="form-group col-md-7" id="cell2">
                        <label for="example-date-input" class="col-4 col-form-label">Cell 2:</label>
                        <div class="col-7">
                            <input class="form-control" type="tel" name="cell2" placeholder="0***-*******" required>
                        </div>
                    </div>
                </div>
                <label id="mat">Matric/O Level Marks:</label>
                <div class="form-row" id="mar">
                    <br>
                    <div class="form-group col-md-8">
                        <label for="example-date-input" class="col-5 col-form-label">Obtained Marks:</label>
                        <div class="col-5">
                            <input class="form-control" type="number" name="matric_obtained_marks" required>
                        </div>
                    </div>
                    <div class="form-group col-md-8" id="tom">
                        <label for="example-date-input" class="col-5 col-form-label">Total Marks:</label>
                        <div class="col-5">
                            <input class="form-control" type="number" max="1100" name="matric_total_marks" required>
                        </div>
                    </div>
                </div>
                <label id="mat">Matric/O Level Marksheet:</label>
                <div class="input-group col-md-9">
                    <div class="custom-file">
                    <input type="file" class="custom-file-input" id="inputGroupFile04" name="matric_marksheet_image" >
                    <label class="custom-file-label" for="inputGroupFile04">Choose file</label>
                    </div>
                    <div class="input-group-append">
                    <button type="button" class="btn btn-primary" id="upl">Upload</button>
                    </div>
                </div>
                <small id="mat" class="form-text text-muted">Upload scanned picture of your Matric/O Level marksheet</small>
                <br>
                <label id="mat">FSc/A-Level Marks:</label>
                <div class="form-row" id="mar">
                    <br>
                        <div class="form-group col-md-8">
                      <label for="example-date-input" class="col-5 col-form-label">Obtained Marks:</label>
                            <div class="col-5">
                                <input class="form-control" type="number" name="fsc_obtained_marks" required>
                        </div>
                    </div>
                    <div class="form-group col-md-8" id="tom">
                      <label for="example-date-input" class="col-5 col-form-label">Total Marks:</label>
                            <div class="col-5">
                                <input class="form-control" type="number" max="1100" name="fsc_total_marks" required>
                        </div>
                    </div>
                </div>
                <label id="mat">FSc/A-Level Marksheet:</label>
                <div class="input-group col-md-9">
                  <div class="custom-file">
                    <input type="file" class="custom-file-input" id="inputGroupFile04" name="fsc_marksheet_image" >
                    <label class="custom-file-label" for="inputGroupFile04">Choose file</label>
                  </div>
                  <div class="input-group-append">
                    <button type="button" class="btn btn-primary" id="upl">Upload</button>
                  </div>
                </div>
                <small id="mat" class="form-text text-muted">Upload scanned picture of your FSc/A-Level marksheet</small>
                <br>
                <label id="mat">NMDCAT Marks:</label>
                <div class="form-row" id="mar">
                    <br>
                        <div class="form-group col-md-8">
                      <label for="example-date-input" class="col-5 col-form-label">Obtained Marks:</label>
                            <div class="col-5">
                                <input class="form-control" type="number" name="nmdcat_obtained_marks" required>
                        </div>
                    </div>
                    <div class="form-group col-md-8" id="tom">
                      <label for="example-date-input" class="col-5 col-form-label">Total Marks:</label>
                            <div class="col-5">
                                <input class="form-control" type="number" name="nmdcat_total_marks" required>
                        </div>
                    </div>
                </div>
                <label id="mat">NMDCAT Result:</label>
                <div class="input-group col-md-9">
                  <div class="custom-file">
                    <input type="file" class="custom-file-input" id="inputGroupFile04">
                    <label class="custom-file-label" for="inputGroupFile04" name="nmdcat_result_image" >Choose file</label>
                  </div>
                  <div class="input-group-append">
                    <button type="button" class="btn btn-primary" id="upl">Upload</button>
                  </div>
                </div>
                <small id="mat" class="form-text text-muted">Upload scanned picture of your NMDCAT result</small>
                <br>
                <button type="submit" class="btn btn-success col-md-2" id="sig" align="center" center>Submit</button>
            </form>
        </div>
        @endsection

这是我的迁移:

public function up()

    Schema::create('students', function (Blueprint $table) 
        $table->id();
        $table->string('full_name');
        $table->date('dob');
        $table->text('gender');
        $table->text('dis');
        $table->biginteger('cnic');
        $table->text('marital_status');
        $table->text('nationality');
        $table->text('religion');
        $table->text('domicile');
        $table->text('province');
        $table->text('father_name');
        $table->biginteger('father_cnic');
        $table->text('father_profession');
        $table->text('guardian_name');
        $table->biginteger('guardian_cnic');
        $table->text('email');
        $table->text('address');
        $table->biginteger('tel_landline');
        $table->biginteger('tel_office');
        $table->biginteger('cell1');
        $table->biginteger('cell2');
        $table->integer('matric_obtained_marks');
        $table->integer('matric_total_marks');
        $table->binary('matric_marksheet_image');
        $table->integer('fsc_obtained_marks');
        $table->integer('fsc_total_marks');
        $table->binary('fsc_marksheet_image');
        $table->integer('nmdcat_obtained_marks');
        $table->integer('nmdcat_total_marks');
        $table->binary('nmdcat_result_image');
        $table->timestamps();
    );

【问题讨论】:

存储功能没有$student->dis = $request->dis 我收到错误 1364。字段 'dis' 没有默认值。 这意味着 (1) 字段 'dis' 没有默认值,并且在同时定义为 NOT NULL (2) 您尝试使用查询插入新行,其中 dis 列根本没有被提及。最简单的解决方案是根据ALTER TABLE执行并为该列指定默认值。 你尝试过什么调试问题? 【参考方案1】:

您没有将任何数据保存到数据库中的 dis 字段,而是 dis 输入字段将数据保存到数据库中的性别字段。这样做

$student->dis=$request->dis;

或为数据库中的 dis 字段分配一些默认值。

【讨论】:

或添加迁移文件。 $table->text('dis')->nullable();//$table->text('dis')->default('value');【参考方案2】:

在您的表中,此处的 dis 列不可为空。因此,当您保存任何行时,此列需要填充。要解决此问题,您需要将此列设为空或为此设置默认值。添加一个新的迁移文件并使该列可以为空或设置默认值。

$table->text('dis')->nullable();

$table->text('dis')->default('your_disere_value');

或者将dis列值从刀片保存到控制器。

$student->dis=$request->dis;

【讨论】:

以上是关于一般错误:1364:字段“dis”没有默认值的主要内容,如果未能解决你的问题,请参考以下文章

SQLSTATE [HY000]:一般错误:1364 字段“标题”没有默认值

SQLSTATE [HY000]:一般错误:1364 字段 'uID' 没有默认值

Laravel 表更新问题一般错误:1364 字段'start_date'没有默认值

错误 SQLSTATE [HY000]:一般错误:1364 字段“名称”没有默认值(SQL:插入“收藏夹”()值())

SQLSTATE [HY000]:一般错误:1364 字段“author_id”没有默认值

SQLSTATE[HY000]:一般错误:1364 字段“parent_id”没有默认值