【问题标题】:laravel/php (Crud) Connect a delete button to a function (routes)laravel/php (Crud) 将删除按钮连接到函数(路由)
【发布时间】:2015-07-06 07:56:37
【问题描述】:

您好,我有一个像这样迭代的表

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

连接到这条路线。

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

我不知道为什么它不起作用。它按预期刷新页面并更新表格。但它不会删除任何行!

【问题讨论】:

  • 但是我将如何引用 id?我试过 $id = $post->id;但没有骰子。
  • 据我所知,您没有正确引用 id。
  • 对不起我的无知,但我不确定你的意思?我该怎么做?它会在函数中去哪里?我在这里遇到了一些严重的斗争。

标签: php arrays database sqlite laravel-4


【解决方案1】:
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')
          }

【讨论】:

  • 让我添加一个返回函数。理想情况下应该有 return @函数的结尾
猜你喜欢
  • 1970-01-01
  • 2016-10-30
  • 2020-02-16
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
相关资源
最近更新 更多