php - 表单未将数据传递到数据库但没有错误 Laravel 5.8.22

标签 php html laravel laravel-5

我是 Laravel 新手,我正在尝试根据此 tutorial 将多数据插入到多个表中。我复制了该项目,但当我使用表单插入时,数据无法到达数据库。通过PhPMyAdmin手动插入可以看到数据,怀疑是数据库连接问题。

表单 View :

<form>
    <section>
        <div class="panel panel-header">

            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <input type="text" name="customer_name" class="form-control" placeholder="Please enter your name">
                    </div></div>
                <div class="col-md-6">
                    <div class="form-group">
                        <input type="text" name="customer_address" class="form-control" placeholder="Please enter your Address">
                    </div></div>
            </div></div>
        <div class="panel panel-footer" >
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>Product Name</th>
                    <th>Brand</th>
                    <th>Quantity</th>
                    <th>Budget</th>
                    <th>Amount</th>
                    <th><a href="#" class="addRow"><i class="glyphicon glyphicon-plus"></i></a></th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td><input type="text" name="product_name[]" class="form-control" required=""></td>
                    <td><input type="text" name="brand[]" class="form-control"></td>
                    <td><input type="text" name="quantity[]" class="form-control quantity" required=""></td>
                    <td><input type="text" name="budget[]" class="form-control budget"></td>
                    <td><input type="text" name="amount[]" class="form-control amount"></td>
                    <td><a href="#" class="btn btn-danger remove"><i class="glyphicon glyphicon-remove"></i></a></td>
                </tr>
                </tbody>
                <tfoot>
                <tr>
                    <td><input type="submit" name="" value="Submit" class="btn btn-success"></td>
                </tr>
                </tfoot>
            </table>
        </div>
    </section>
</form>
<script type="text/javascript">
    $('.addRow').on('click',function(){
        addRow();
    });
    function addRow()
    {
        var tr='<tr>'+
            '<td><input type="text" name="product_name[]" class="form-control" required=""></td>'+
            '<td><input type="text" name="brand[]" class="form-control"></td>'+
            '<td><input type="text" name="quantity[]" class="form-control quantity" required=""></td>'+
            '<td><input type="text" name="budget[]" class="form-control budget"></td>'+
            ' <td><input type="text" name="amount[]" class="form-control amount"></td>'+
            '<td><a href="#" class="btn btn-danger remove"><i class="glyphicon glyphicon-remove"></i></a></td>'+
            '</tr>';
        $('tbody').append(tr);
    };
    $('.remove').live('click',function(){
        var last=$('tbody tr').length;
        if(last==1){
            alert("you can not remove last row");
        }
        else{
            $(this).parent().parent().remove();
        }

    });
</script>

OrderController 存储功能:

public function store(Request $request)
{
    $data=$request->all();
    $lastid=Orders::create($data)->id;
    if(count($request->product_name) > 0) {
        foreach($request->product_name as $item=>$v) {
            $data2=array(
                         'orders_id'=>$lastid, 
                         'product_name'=>$request->product_name[$item],
                         'brand'=>$request->brand[$item], 
                         'quantity'=>$request->quantity[$item], 
                         'budget'=>$request->budget[$item], 
                         'amount'=>$request->amount[$item]            
                         );
            Items::insert($data2);
        }
    }
    return redirect()->back()->with('success','data insert successfully');
}

路线是:

Route::post('/orders','OrderController@store');
Route::get('/orders','OrderController@index');
Route::get('/items/{id}','OrderController@items');

我做错了什么?或者只是兼容性问题? 谢谢

最佳答案

您忘记添加action (和 method )属性到您的 <form>标签。这就是表单不发送数据的原因。 此外,您还需要在 POST 中发送 csrf token 。请求。

<form method="POST" action="/orders">
    @csrf
    // ...
</form>

关于php - 表单未将数据传递到数据库但没有错误 Laravel 5.8.22,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56695359/

相关文章:

javascript根据2个不同的选择下拉值显示结果

laravel - 方法 Yajra\DataTables\CollectionDataTable::where 不存在

php - BadMethodCallException:方法 Illuminate\Database\Eloquent\Collection::orderBy 不存在

php - MySql 搜索两个组合字段的查询

php - 在该文件夹中创建文件夹和子文件夹

javascript - 如何根据绝对容器中响应图像的高度设置父(相对)的动态高度

事件处理

php - HTML 标签中的三元运算符 - Laravel 5.6 Blade 模板

html - 在 Blade 模板中的表单标签内使用 <span>

php - 自定义错误页面不适用于 Apache 和 nginx 设置的 .htaccess