laravel mysql数据库同一页多个表单提交表未更新

时间:2018-11-29 19:09:44

标签: php mysql forms laravel-5 checkbox

我正在尝试使用复选框更新数据库表,代码未给出任何错误,但数据库表未更新。我的刀片式服务器代码的形式是:

<body>
        <h2>Reported Posts </h2>
        <a href="{{route('moderator.index')}}">Back</a> 

        <table>
        @foreach($post_reports as $post_report)
            <tr>
                <td>Report NO:</td>
                <td>{{$post_report->report_id }}</td>
                <td>{{$post_report->status}}</td>
    <form method="post">
        @csrf
                <td><input type="checkbox" name="statusyes[]" value={{$post_report->report_id }} >report to admin  <input type="checkbox" name="statusno[]" value={{$post_report->report_id}} >wrong report </td>
            </tr>
        @endforeach
        <td><input type="submit" name="submit" value="Submit" /></td>
    </form>
        </table>

我的控制器代码是:

我正在使用status_update_reported_post函数

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post_report;
use Illuminate\Support\Facades\DB;


class moderatorController extends Controller
{
    public function index(Request $request){

    return view('moderator.index');
   }

   public function reported_post(Request $request){

    $post_reports=DB::table('Post_reports')
                ->where('status','moderator')->get();

    return view('moderator.reported_post')-> with('post_reports',$post_reports);
   }

   public function status_update_reported_post(Request $request){
   $statusadmin[]=$request['statusyes'];
   $statuswrong[]=$request['statusno'];
   $r='admin';
   $wr='wrong';

   foreach($statusadmin as $report_id){
      DB::table('post_reports')
               ->where('report_id','$report_id')
               ->update(['status' => 'admin']);

   }

   foreach($statuswrong as $report_id){

        DB::table('post_reports')
               ->where('report_id','$report_id')
               ->update(['status' => 'wrong']);

   }
}


}

代码工作正常,但是表中没有更新,我不知道问题出在哪里,对我来说一切都很好。

1 个答案:

答案 0 :(得分:0)

此行

->where('report_id','$report_id')

应该是

->where('report_id',$report_id)

使用单引号表示字符串$report_id,而不是变量$report_id的值