我的数据库中有此表。
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)"
任何帮助将不胜感激。
答案 0 :(得分: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
]);
}
我认为这必须有效
答案 1 :(得分:0)
在HTML中使用<input type="checkbox" name="check[]" value="cake">
在控制器中(使用php)
$checkbox1=$_POST['check'];
现在
foreach($checkbox1 as $chk1)
{
$chk .= $chk1.",";
}