mysql - 当我尝试将数据插入表中时,出现错误

标签 mysql laravel-5

我使用 Laravel 创建了页面,用于从数据库中获取数据到表中。在同一页面中,我可以选择将数据插入数据库。当我向数据库插入数据时,显示错误“SQLSTATE[23000]:违反完整性约束:1062 键“PRIMARY”的重复条目“”(SQL:插入设备(vehicleID) 值 (avs))"。以前它工作正常,我能够插入数据。 我的 View 页面如下

   @extends('app')

@section('content')



    <div class="templatemo-content-wrapper">
        <div class="templatemo-content">
            <ol class="breadcrumb">
                <li><a href="{{ url("/") }}"><font color="green">Home</font></a></li>
                <li class="active">Vehicle information</li>
            </ol>
            <h1>View/Edit Vehicle information</h1>

            <p></p>

            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive" style="overflow-x:auto;">

                        <table id="example" class="table table-striped table-hover table-bordered" bgcolor="#fff8dc">
                            <h3>Select a Vehicle :</h3>
                            <thead>
                            <tr>
                                <th>Vehicle ID</th>
                                <th>Unique ID</th>
                                <th>Description</th>
                                <th>Equipment Type</th>
                                <th>SIM Phone</th>
                                <th>Server ID</th>
                                <th>Ignition State</th>
                                <th>Expecting ACK</th>
                                <th>Active</th>
                                <th>Actions</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($devices as $device)
                            <tr>

                            <td>{{ $device->vehicleID }}</td>
                            <td>{{ $device->uniqueID }}</td>
                            <td>{{ $device->description }}</td>
                            <td>{{ $device->equipmentType }}</td>
                            <td>{{ $device->simPhoneNumber }}</td>
                            <td></td>
                            <td>
                                @if(@$device->ignitionIndex == '0')
                                    OFF
                                    @else
                                ON
                                    @endif
                            </td>
                            <td>{{ $device->expectAck }}</td>
                            <td>
                                @if($device->isActive == '1')
                                    Yes
                                @else
                                    No
                                @endif
                            </td>
                                <td>
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-info">Action</button>
                                        <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
                                            <span class="caret"></span>
                                            <span class="sr-only">Toggle Dropdown</span>
                                        </button>
                                        <ul class="dropdown-menu" role="menu">

                                            <li data-toggle="modal" data-target="#acceptModal" data-bookingid="{{ $device->vehicleID }}"><a href="#">View/ Edit</a>
                                            </li>

                                            <li><a href="{{ url('/vehicle/delete/'.$device->vehicleID)}}">Delete</a></li>
                                        </ul>
                                    </div>
                                </td>
                            </tr>

                            @endforeach
                            </tbody>
                        </table>
                        {{--{!! $results->appends(['sort' => $sort])->render() !!}--}}

                        {{$devices->links()}}
                    </div>
                </div>
            </div>
        </div>
    </div>
    {{--{!! $device->links()!!}--}}


    </br>

    <h4>Create a new Vehicle</h4>
    <form role="form" method="POST" action="{{ url('vehicleAdmin') }}">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">


        <div class="row">
            <div class="col-md-6 margin-bottom-15">

                <input type="text" class="form-control" name="vehicleID" value="{{ old('vehicleID') }}" placeholder="Enter vehicle ID">
            </div>
            <div class="row templatemo-form-buttons">
                <div class="submit-button">
                    <button type="submit" class="btn btn-primary">New</button>

                </div>
            </div>
        </div>
    </form>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#example').dataTable();
        } );
    </script>
@endsection

Controller 页面是

 <?php

namespace App\Http\Controllers;

use Mail;
use Illuminate\Support\Facades\DB;
use App\Device;
use App\Http\Requests\createUserRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Pagination\Paginator;

class VehicleController extends Controller
{
    public $type = 'Device';





    public function getIndex()
    {


        $devices = DB::table('device')->simplePaginate(15);
        return view('vehicle.vehicleAdmin')->with('devices', $devices);
    }


    public function vehicleInsert()
    {
        $postUser = Input::all();
        //insert data into mysql table
        $data =      array('vehicleID'=> $postUser['vehicleID']
        );
        //  echo print_r($data);
        $ck = 0;
        $ck = DB::table('device')->Insert($data);
        //echo "Record Added Successfully!";
        $devices = DB::table('device')->simplePaginate(50);
        return view('vehicle.vehicleAdmin')->with('devices', $devices);


    }


    public function delete($id)
    {
        DB::table('device')->where('vehicleID', '=', $id)->delete();
        return redirect('vehicleAdmin');
    }

}

路线页面是

 Route::any('vehicleAdmin', 'VehicleController@getIndex');
Route::post('vehicleAdmin', 'VehicleController@vehicleInsert');
Route::delete('vehicle/delete/{id}', 'VehicleController@delete');

谁能告诉我为什么会出现这个错误,以及我需要做什么? 提前致谢

最佳答案

我认为vehicleID是自增的,不需要插入

public function vehicleInsert()
{
    $postUser = Input::all();
    //insert data into mysql table 
    $data =      array('other-columns'=> $postUser['other-columns']
    );
    //  echo print_r($data);
    $ck = 0;
    $ck = DB::table('device')->Insert($data);
    //echo "Record Added Successfully!";
    $devices = DB::table('device')->simplePaginate(50);
    return view('vehicle.vehicleAdmin')->with('devices', $devices);


}

关于mysql - 当我尝试将数据插入表中时,出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094659/

相关文章:

php - 如何在 MySQL 表的列中找到最常见的结果

mysql - 提高mysql导入速度

mysql - 房协管理系统数据库结构

php - mysql按记录集数量最少的组选择语句

laravel - 使用 git-codecommit 拒绝 aws 权限(公钥)

php - 如何在 Laravel 护照中实现多重身份验证

mysql - 如何将不同分类的多个文本框值存储到一张表中?

javascript - 在 Laravel 上提交表单后如何关闭 Bootstrap Modal?

php - 需要从多个具有动态用户 ID 的表插入产品表

php - 触发更新每个 CRUD 操作的字段 laravel 5 Query Builder