2016-06-29 12 views
0

Ich erstellte Seite mit Laravel zum Abrufen von Daten aus der Datenbank in die Tabelle. Auf der gleichen Seite habe ich die Möglichkeit, Daten in die Datenbank einzufügen. Beim Einfügen von Daten in die Datenbank wird der Fehler "SQLSTATE [23000]: Verletzung Integritätseinschränkung: 1062 Doppelter Eintrag '' für den Schlüssel 'PRIMARY' (SQL: Einfügen in device (vehicleID) Werte (avs))" angezeigt. Zuvor funktionierte es einwandfrei und ich konnte die Daten einfügen. Meine Ansicht Seite wird unterWenn ich versuche, Daten in die Tabelle einzufügen, erhalten Fehler

@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 Seite gibt, ist

<?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'); 
    } 

} 

und die Route Seite ist

Route::any('vehicleAdmin', '[email protected]'); 
Route::post('vehicleAdmin', '[email protected]'); 
Route::delete('vehicle/delete/{id}', '[email protected]'); 

Kann jemand mir sagen, warum dieser Fehler kommt, und was ich tun müssen? Vielen Dank im Voraus

Antwort

0

ich denke VehicleID Autoinkrement ist und keine Notwendigkeit

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); 


} 
+0

Fahrzeug-ID einfügen wird nicht automatisch erhöht ein –

+0

ist diese einzigartige dann anders einfügen jedes Mal –

+0

ja, anderen Wert am Einfügen nur , aber es zeigt immer noch denselben Fehler –

Verwandte Themen