php - laravel mysql数据库同一页面多个表单提交表不更新

标签 php mysql forms laravel-5 checkbox

我正在尝试使用复选框更新数据库表,代码没有给出任何错误,但数据库表没有更新。我的 Blade 代码的形式是:

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

我的 Controller 代码是:

我正在使用 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']);

   }
}


}

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

最佳答案

这一行

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

应该是

->where('report_id',$report_id)

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

关于php - laravel mysql数据库同一页面多个表单提交表不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53545971/

相关文章:

php - 如何在 zend 框架中显式关闭 mysql 连接?

javascript - 通过ajax从输入获取变量到外部php

php - mysql从2个表中选择数据,但第2个表具有动态单元格名称

html - CSS:文件输入 - 定位显示 "no file chosen"的文本

动态选项卡中的 Javascript 计算总计

PHP 获取两行但仅从最后一行上传文件

php - Laravel 5.1 date_format 验证允许两种格式

mysql - If else on WHERE 子句

java - 如何从不同的计算机连接到 MySQL 数据库?

c# - 离开文本框后,透明文本框不显示文本