【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be nullSQLSTATE [23000]:违反完整性约束:1048 列“名称”不能为空
【发布时间】:2018-05-08 16:29:00
【问题描述】:

SQLSTATE[23000]:完整性约束违规:1048 列“名称”不能为空(SQL:插入productsnametypeimg_urlupdated_atcreated_at)值(, , img/products/IMG-20170921-WA0003.jpg, 2017-11-24 15:31:41, 2017-11-24 15:31:41))

PHP 代码

 public function Products(Request $request){

        if($request->isMethod('post'))
        {
              if(Input::hasFile('file')){

                  $myproduct = new Product();
                  $myproduct->name=$request->input('name');
                  $myproduct->type=$request->input('type');

                  $file = Input::file('file');
                  $url='img/products/'.$file->getClientOriginalName();
                  $file->move('img/products',$file>getClientOriginalName());
                  $myproduct->img_url = $url;

                  $myproduct->save();
                  return view("controlpanel.products");
            }
      }}

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <table>
      <tr>
        <form action="{{ URL('products') }}" method="post" enctype="multipart/form-data"  class="container">
      <td>
         <label for="name">Product Name</label>
         <input type="text" name="name" class="form-control" id="name" required>
        </td>
        <td>
           <label for="type">Product Type</label>
           <select class="form-control" name="type" id="type">
              <option value="type1">type 1</option>
            <option value="type2">type 2</option>
           </select>
        </td>
        <td>
          <label for="file">Select Product image :</label>
          <input type="file" name="file" id="file">
        </td>
        <td>
          <input type="submit" value="Upload" name="submit" id="sub1">
          <input type="hidden" value="{{ csrf_token() }}" name="_token">
        </td>
    </form>
      </tr>
    </table>
  </body>
</html>

所以除了 img_url 之外,列名和类型都为空,我不知道为什么?请帮忙

【问题讨论】:

  • 您的错误说“列'名称'不能为空”,您在插入时将其留空。
  • 不,我完全插入所有输入
  • 试试 var_dump($my_product)
  • “没有我完全插入所有输入” - 不是根据错误消息...values (, , img ...
  • Products 方法下dd($request-&gt;name,$request-&gt;type); 得到了什么。

标签: php mysql laravel


【解决方案1】:

我假设您正在使用 laravel。要获取输入值,您可以尝试使用此代码作为您的名称和类型字段。

$myproduct->name=$request->name;
$myproduct->type=$request->type;

【讨论】:

  • 是的,我正在使用 laravel,我尝试过,但没有成功。谢谢
【解决方案2】:

我有同样的问题,但问题是表格的名称与表格上的名称不同......

$todo->todo = $request->todo;

【讨论】:

    【解决方案3】:

    是的,我在 laravel 上遇到了同样的问题,我不得不将我的数据库更改为可为空,然后请求通过....回到我的代码并添加 dd($article),它表明表单字段为空.我检查了表单字段,发现在迁移过程中名称与表的名称不匹配。

    【讨论】:

      【解决方案4】:

      如果你用过 laravel 然后用 PHP artisan optimize 清除 catch: clear 然后重新运行项目,因为有些 catch 会产生这样的问题

      【讨论】:

        【解决方案5】:

        其中一个原因是,当您以反逗号声明变量时,请检查您是否在声明中留出了空格,如示例所示 'name' 那么它会显示错误:

        $profileName = $request-&gt;input(' name'); → 你已经看到我在变量声明中给了空间,所以,每当你声明变量时我都会遇到这样的问题,请确保你不应该错误地给空间

        【讨论】:

          猜你喜欢
          • 2021-02-01
          • 1970-01-01
          • 2019-11-20
          • 2021-08-16
          • 2019-10-06
          • 2020-10-25
          • 2017-02-28
          • 2017-04-15
          • 2017-12-26
          相关资源
          最近更新 更多