Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

omarf7122's avatar

update the data in database

I am trying to update my data in the database. but " edit " function just added new data, not updated. how can do that ?. thanks

public function index_update(){ $books = DB::select('select * from users'); return view('index2',['books'=>$books]); }

public function show($id) { $books = DB::select('select * from users where id = ?',[$id]); return view('edit2',['books'=>$books]); }

public function edit(Request $req,$id) { $name = $req->input('name'); $email = $req->input('email') ; $data=array('name'=>$name,'email'=>$email); DB::table('users')->insert($data); echo "data recorded successfully."; }

------------------- Route::get('/update','TestController@index_update')->name('display'); Route::get('edit/{id}','TestController@show')->name('updateD'); Route::post('/edit/{id}','TestController@edit')->name('updateEdit');; -------------------

{!! csrf_field() !!} Name email -----------**--------
0 likes
14 replies
Cronix's avatar

Yes, your edit method is creating a new record, not editing an existing one. You'd use update() instead of insert(). Perhaps it should be something like

public function edit(Request $req,$id) {
    $name = $req->input('name');
    $email = $req->input('email') ;
    $data=array('name'=>$name,'email'=>$email);

  
    DB::table('users')
        ->where('id', $id)
        ->update($data);
    echo "data recorded successfully.";
}

https://laravel.com/docs/5.8/queries#updates

1 like
jlrdw's avatar

DB::table('users')->insert($data); echo "data recorded successfully."; }

Prior to attempting figuring out insert vs edit, etc, perhaps look here, they have great tutorials.

http://www.mysqltutorial.org/

1 like
omarf7122's avatar

hello sir, thank you for your reply, f I use " DB::table('users')->update($data);" it is updated all the data , but " DB::table('users')->where('id',$id)->update($data); " it does not updated. i am not getting where am i doing worng .

public function edit(Request $req,$id) {
  $name = $req->input('name');
      $email = $req->input('email') ;
      $data=array('name'=>$name,'email'=>$email) ;
   
      //DB::table('users')->update($data);

      DB::table('users')->where('id',$id)->update($data);
    
      
      echo "data   recorded successfully.<br/>";
  } 
    Route::get('/update','TestController@index_update')->name('display');
Route::get('edit/{id}','TestController@show')->name('updateD');
Route::post('/editt/{id}','TestController@edit')->name('updateEdit');; 
   <body>
      @if($errors->any())
         @foreach ($errors->all() as $error) 
            <li>{{ $error }}</li>
         @endforeach
      @endif
      <form  method="post" action = "{{route('updateEdit')}}" > 
          {!! csrf_field() !!}
        <table>
            <tr>
              <td>Name</td>
               <td>
    <input type = 'text' name = 'name' value = "{{ $books[0]->name }}"/>
               </td>
               <td>email</td>
               <td>
                  <input type = 'text' name = 'email' 
                     value = "{{ $books[0]->email }}"/>
               </td>
            </tr>
            <tr>
               <td colspan = '2'>
                  <input type = 'submit' value = "Update student" />
               </td>
               </tr>
         </table>
      </form>
Snapey's avatar

You would get more help if you formatted your code blocks so we can see what you are on about.

Please format your code by putting 3 backticks ``` on a line before and after each code block

You can go back and edit your previous response

1 like
jlrdw's avatar

Passing id as parameter = like above.

Passing id in query string = :

    $id = $req->input('id') ;
    $name = $req->input('name');
    $email = $req->input('email') ;
    $data=array('name'=>$name,'email'=>$email);

  
    DB::table('users')
        ->where('id', $id)
        ->update($data);

You should consider taking some free video lessons, just a suggestion. Also please format your code.

Snapey's avatar

Are you sure this view is even being used, because this

action = "{{route('updateEdit')}}" >

should be throwing an error. Your route requires an id value but you are not providing one.

1 like
jlrdw's avatar

Please format the code in your original question. Extremely hard to read.

Snapey only needed if ID is being passed via parameter but not needed if it's in the query string.

Scratch that I found it above it's just so hard to read. I had to do a find on this page.

omarf7122's avatar

this code work for me. thank you everyone. my code is working perfectly .

public function edit(Request $req,$id) {
  
     
      $name = $req->input('name');
      $email = $req->input('email') ;
     $data=array('name'=>$name,'email'=>$email) ;
      DB::table('users')->where('id',$id)->update($data);
       
      echo "data   is updated";
  } 
    Route::get('/update','TestController@index_update');
Route::get('edit/{id}','TestController@show')->name('updateD');
Route::post('/updateEdit/{id}','TestController@edit'); 
<form  method="post" action = "{{  url('updateEdit/'.$books[0]->id)}}"> 
          {!! csrf_field() !!}

        <table>
            <tr>
              <td>Name</td>
               <td>
    <input type = 'text' name = 'name' value = "{{ $books[0]->name }}"/>
               </td>
               <td>email</td>
               <td>
                  <input type = 'text' name = 'email' 
                     value = "{{ $books[0]->email }}"/>
               </td>
            </tr>
            <tr>
               <td colspan = '2'>

                  <input type = 'submit' value = "Update student" />

               </td>
               </tr>
         </table>
      </form>
jlrdw's avatar

Here

Route::post('/updateEdit/{id}','TestController@edit'); 

You can pass the ID in a hidden field for a post.

I guess I've never seen a mix of a get request Style and a post request combined like this.

Snapey's avatar

@jlrdw

Snapey only needed if ID is being passed via parameter but not needed if it's in the query string.

No, this is incorrect. If the route says /updateEdit/{id} then you MUST pass the id

Its working now because the OP changed from the Route helper to using URL and concatenating the ID onto the route

"{{  url('updateEdit/'.$books[0]->id)}}">
jlrdw's avatar

No, this is incorrect. If the route says /updateEdit/{id} then you MUST pass the id

That's why I said:

Scratch that I found it above it's just so hard to read. I had to do a find on this page.

I missed it at first.

But why isn't OP using a hidden field the usual way to pass ID. He is using POST yet passing a parameter also like (similar to) a get request.

I have never see a mix like that. But I guess it works.

jlrdw's avatar

@snapey thank's for explaining, and I will look over link. I guess I am just old school still.

Please or to participate in this conversation.