简体   繁体   English

laravel / php(Crud)将删除按钮连接到功能(路线)

[英]laravel/php (Crud) Connect a delete button to a function (routes)

Hi There I have a table that iterates like so 嗨,我有一个像这样迭代的表

  <form method="get" action="search"> <table class="bordered"> <thead> <tr><th>No.</th><th>Post Username</th><th>Post Title</th><th>Post</th><th>Delete This post</th><td>Update This Post</td></tr> </thead> <tbody> @foreach ($posts as $post) <tr> <td>{{{ $post->id }}}</td> <td>{{{ $post->post_username }}}</td> <td>{{{ $post->post_title }}}</td> <td>{{{ $post->post_message }}}</td> <td> <form method="post" action="{{{ url('delete_post_action') }}}"> <input type="submit" value="{{ $post->id }}">Delete</td> </form> </td> <td><button name="update" value="{{ $post->id }}">Update</button></td> </tr> @endforeach </tbody> </table> </form> 

Which connects to this route. 哪个连接到此路由。

Route::post('delete_post_action', function()
{
    $sql = "select * from posts";
    $current_posts = DB::select($sql);

    $results=$current_posts;

    return View::make('pages.home')->withposts($results);
});

  function delete_item($id)
  {
    $id = $post->id;
    $sql = "delete from posts where id = ?";
    DB::delete($sql, array($id));
  }

I have no idea why it isn't working. 我不知道为什么它不起作用。 It refreshes the page as intended and updates the table. 它会按预期刷新页面并更新表。 But it is not deleting any rows! 但这不是删除任何行!

Concatenate id you are injecting with your query would result in below 

        function delete_item($id)
          {
            $id = $post->id;
            $sql = "delete from posts where id='".$id."'";
            DB::delete($sql);
            return View::make('pages.home')
          }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM