1

我的数据库中有这张表。

id   |  scenario_id |  questions
1         null           test 1
2         null           test 2
3         null           test 3

我想scenario_id使用我将在我的复选框上获得的值来更新该列。

这是我的控制器

  public function store(Request $request)
    {

      $scenarios = Scenario::create([
           'topic_id' => 1,
           'title' => "Title New Scenario",
           'resources_path' => "Sample",
           'component_type' => $request['scenarios_scenario_component_component'],
           'component' => $request['scenarios_scenario_component_text'],
           'created_by' => Auth::user()->id
      ]);

     $questions= $request->get('scenario_questions'); //my checkbox values

     for ($i=0; $i < count($questions) ; $i++) {
        $questions_update = Question::where('id', $questions[$i])->update([
          'scenario_id', $scenarios->id
        ]);
     }


      return response()->json([
        'success'=>"Success"
      ]);
    }

这是我的html代码:

   <input type="checkbox" name="scenario_questions[]" value="92"> Test123
   <input type="checkbox" name="scenario_questions[]" value="91"> What would you do if, let's say for example, a customer starts screaming at you?
   <input type="checkbox" name="scenario_questions[]" value="35"> Today, you will learn how to take in phone calls.
   <input type="checkbox" name="scenario_questions[]" value="34"> I drink coffee before doing anything else. It has become routinary for me.
   <input type="checkbox" name="scenario_questions[]" value="33"> Wait for me at the coffee shop at the back of the office building.
   <input type="checkbox" name="scenario_questions[]" value="32"> In the office, we can wear polos on Mondays. 
   <input type="checkbox" name="scenario_questions[]" value="31"> I had to fall in line for three hours to get these concert tickets.

如何根据用户在我的复选框上选中的内容更新我的所有表格列?复选框的值是我表中的 ID。

我一直有一个错误

Column not found: 1054 Unknown column '0' in 'field list' (SQL: update `questions` set `0` = scenario_id, `1` = 12, `questions`.`updated_at` = 2020-11-03 10:17:00 where `id` = 92)"

任何帮助将非常感激。

4

2 回答 2

0

你的代码在这里是错误的

for ($i=0; $i < count($questions) ; $i++) {
    $questions_update = Question::where('id', $questions[$i])->update([
      'scenario_id', $scenarios->id
    ]);
 }

一定是

for ($i=0; $i < count($questions) ; $i++) {
    $questions_update = Question::where('id', $questions[$i])->update([
      'scenario_id'=> $scenarios->id
    ]);
 }

我认为这必须有效

于 2020-11-03T10:33:48.867 回答
0

在控制器中使用 HTML <input type="checkbox" name="check[]" value="cake"> (使用 php)执行此操作

$checkbox1=$_POST['check']; 

现在

foreach($checkbox1 as $chk1)  
   {  
      $chk .= $chk1.",";  
   } 
于 2020-11-03T11:09:21.527 回答