【问题标题】:Column not found: 1054 Unknown column '0' in field list未找到列:1054 字段列表中的未知列“0”
【发布时间】:2019-01-25 05:21:38
【问题描述】:

我在一页中有 3 个表单,它们使用 ajax。编辑表单出现如下错误。我不知道为什么我会收到这个错误。 我是初学者。 提交表单时出错:

“SQLSTATE[42S22]: 未找到列: 1054 '字段列表'中的未知列'0' (SQL: 更新user_profiles 设置0 = , updated_at = 2018-08-18 10:25: 43 其中id = 1)"

这是我的编辑按钮代码:

<a class="btn btn-primary" href="javascript:void(0);" onclick="editExchangeMarket({{$user_profiles->id}});">
    <span class="glyphicon glyphicon-edit"></span> Edit
</a>

这是我的模态和 ajax 代码:

<script>
        function updateExchangeMarket() {
            var edit_fid = $('#edit_fid').val();
            var edit_charge_per_lot = $('#edit_charge_per_lot').val();
            $('#exchangeMarketModal').modal('show');
            //   alert(values);
            $.ajax({
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                url: "{{URL::to('user/updateExchangeMarket')}}",
                data:{'id' : edit_fid, 'edit_charge_per_lot':edit_charge_per_lot},
                method:'POST',
                success: function( resp ) {
                    },
                error: function( req, status, err ) {
                console.log( 'something went wrong', status, err );
                    }
            });

        }
</script>
<div id="exchangeMarketModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
                <form id="exchangeMarketForm" name="exchangeMarketForm" >
                    <input type="hidden" class="form-control" id="edit_fid" value="1">

                    <div class="col-md-6 mb-3 form-group">
                        chargeperlot: <input type="text" name="edit_charge_per_lot" id="edit_charge_per_lot" class="form-control"><br>
                    </div>
               
                </form>
            </div>
            <div class="modal-footer">

                <a type="submit" href="javascript:void(1);" onclick="updateExchangeMarket({{$user_profiles->id}});" value="Submit" class="btn btn-primary">Save changes</a>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

现在我在这里添加我的控制器代码:

        public function updateExchangeMarket(Request $request)
    {
        $id = $request->id;    
        $charge_per_lot = $request->edit_charge_per_lot;

        $data = array('country_id'=>$country_id,
            'id'=>$id,
            'charge_per_lot'=>$charge_per_lot,

        );
        $res = UserProfile::where('id',$id)->update([$data]);
        echo json_encode($res);
    }

【问题讨论】:

    标签: html laravel laravel-5


    【解决方案1】:

    Update() 采用关联数组来更新值。您的 $data 已经是一个关联数组,因此无需将其作为数组推送。

    $data = array('country_id'=>$country_id,
                'id'=>$id,
                'charge_per_lot'=>$charge_per_lot,
    
            );
            $res = UserProfile::where('id',$id)->update($data)
    

    【讨论】:

      【解决方案2】:
      UserProfile::where('id',$id)->update(
      [
        'name' => $name,
        'number' => $number
      ]);
      

      通过这种方式,您可以查询是否有多个参数和值要查询

      【讨论】:

      • 仅在列上更新
      • 如果我添加了 5 条记录并且我尝试编辑,第 4 条但它不工作第 4 条但它从第 5 条开始执行其他操作。
      • 它实际上不是错误,而是更新不同的列,
      • 向我显示您要编辑的数据库表。代码也进行更新
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 2021-10-24
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      相关资源
      最近更新 更多