如何解决这个未定义的偏移量:0 错误?
Posted
技术标签:
【中文标题】如何解决这个未定义的偏移量:0 错误?【英文标题】:How to solve this Undefined offset: 0 error? 【发布时间】:2018-10-14 21:44:00 【问题描述】:在大会详细信息页面中,用户为每种门票类型选择他想要的数量,然后单击“下一步”并进入注册页面。在注册页面有注册表格。
congresses 表有一列“all_participants”:
如果 all_participants 为“0”,则表示只需要收集有关正在注册的用户的信息,即经过身份验证的用户。注册时使用 auth 用户的姓名、姓氏和电子邮件也是如此。并且在注册表单中只需要显示与所选票证类型相关的自定义问题一次(用于身份验证用户答案),并且应该使用身份验证用户的 ID 存储 因此,如果 all_participants 为“0”并且用户在上一页中选择了 在注册中关联了 1 个或多个自定义问题 似乎是自定义问题。如果没有与用户选择的任何票证类型相关联的自定义问题,则用户不需要插入任何信息,因为注册时使用了他的身份验证信息(姓名、姓氏和电子邮件)。问题是当用户填写该字段并单击“转到第 2 步”时,它会显示“Undefined offset: 0
”。
你知道如何正确解决这个问题吗?
用图表解释错误:(在这种情况下,有一个自定义问题“你的手机是什么?”与用户选择的至少一种票证类型相关联)
// 报名表
<form method="post" id="step1form" action="">
csrf_field()
@if (!empty($allParticipants))
@if($allParticipants == 1)
<p>Please fill in all fields. Your tickets will be sent to
p (\Auth::check()) ? Auth::user()->email : old('email').</p>
@foreach($selectedTypes as $selectedType)
@foreach(range(1,$selectedType['quantity']) as $test)
<h6>Participant - 1 - $test</h6>
<div class="form-check">
<input class="form-check-input" type="radio" name="" value="">
<label class="form-check-label d-flex align-items-center" for="exampleRadios1">
<span class="mr-auto">Fill the following fields with the authenticated user information.</span>
</label>
</div>
<div class="form-group font-size-sm">
<label for="participant_name" class="text-gray">Name</label>
<input type="text" name="participant_name[]" required class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="participant_surname" class="text-gray">Surname</label>
<input type="text" required class="form-control" name="participant_surname[]" value="">
</div>
<input type="hidden" name="ttypes[]" value=" $selectedType['id'] "/>
@foreach($selectedType['questions'] as $customQuestion)
<div class="form-group">
<label for="participant_question">$customQuestion->question</label>
<input type="text"
@if($customQuestion->pivot->required == "1") required @endif
class="form-control" name="participant_question[]">
<input type="hidden" name="participant_question_required[]"
value=" $customQuestion->pivot->required ">
<input type="hidden" value=" $customQuestion->id " name="participant_question_id[]"/>
</div>
@endforeach
@endforeach
@endforeach
@else
<p>Its not necessary aditional info. Your tickets will be sent to (\Auth::check()) ? Auth::user()->email : old('email').</p>
@if($selectedRtype['questions'] )
<p>You only need to answer the cutom questions below.</p>
@foreach($selectedRtype['questions'] as $customQuestion)
<div class="form-group">
<label for="participant_question">$customQuestion->question</label>
<input type="text"
@if($customQuestion->pivot->required == "1") required @endif
class="form-control" name="participant_question[]">
<input type="hidden" name="participant_question_required[]"
value=" $customQuestion->pivot->required ">
<input type="hidden" value=" $customQuestion->id " name="participant_question_id[]"/>
</div>
@endforeach
@endif
@endif
@endif
<input type="submit" href="#step2"
id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
</form>
//当点击“go to step2”按钮时调用的RegistrationController的storeUSerInfo方法:
public function StoreUserInfo(Request $request, $id, $slug = null, Validator $validator)
$allParticipants = Congress::where('id', $id)->first()->all_participants;
$user = Auth::user();
if($allParticipants)
$rules = [
'participant_name.*' => 'required|max:255|string',
'participant_surname.*' => 'required|max:255|string',
];
$messages = [
'participant_question.*.required' => 'The participant is required'
];
foreach ($request->participant_question_required as $key => $value)
$rule = 'string|max:255'; // I think string should come before max
//dd($value);
// if this was required, ie 1, prepend "required|" to the rule
if ($value)
$rule = 'required|' . $rule;
// add the individual rule for this array key to the $rules array
$rules["participant_question.$key"] = $rule;
$validator = Validator::make($request->all(), $rules, $messages);
if($validator->passes())
$registration = Registration::create([
'congress_id' => $id,
'main_participant_id' => $user->id,
'status' => 'C',
]);
$participants = [];
for ($i = 0; $i < count($request->participant_name); $i++)
$participants[] = Participant::create([
'name' => $request->participant_name[$i],
'surname' => $request->participant_surname[$i],
'registration_id' => $registration->id,
'ticket_type_id' => $request->rtypes[$i]
]);
for ($i = 0; $i < count($request->participant_question); $i++)
$answer = Answer::create([
'question_id' => $request->participant_question_id[$i],
'participant_id' => $participants[$i]->id,
'answer' => $request->participant_question[$i],
]);
return response()->json([
'success' => true,
'message' => 'success'
], 200);
else
$messages = [
'participant_question.*.required' => 'The participant is required'
];
foreach ($request->participant_question_required as $key => $value)
$rule = 'string|max:255'; // I think string should come before max
//dd($value);
// if this was required, ie 1, prepend "required|" to the rule
if ($value)
$rule = 'required|' . $rule;
// add the individual rule for this array key to the $rules array
$rules["participant_question.$key"] = $rule;
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->passes())
$registration = Registration::create([
'congress_id' => $id,
'main_participant_id' => $user->id,
'status' => 'C',
]);
$participants = [];
for ($i = 0; $i < count($request->participant_name); $i++)
$participants[] = Participant::create([
'name' => '',
'surname' => '',
'registration_id' => $registration->id,
'ticket_type_id' => $request->rtypes[$i]
]);
for ($i = 0; $i < count($request->participant_question); $i++)
$answer = Answer::create([
'question_id' => $request->participant_question_id[$i],
// the error undefined offset is here
'participant_id' => $participants[$i]->id,
'answer' => $request->participant_question[$i],
]);
return response()->json([
'success' => true,
'message' => 'success'
], 200);
与问题相关的数据库结构:
congresses: id, name, all_participants,...
ticekt type table: id, name, etc
registrations: id, congress_id (fk), main_participant_id (main_participant_id is the id of the auth user the user that does the registration)
participants: id registration_id (fk), ticket_type_id (fk), name, surname
questions: id, question, congress_id (fk)
ticket_type_questions pivot table: id, ticket_type_id, question_id, required (required is 1 or 0, 1 means required)
【问题讨论】:
异常旁边显示什么行号? 错误消息是说$participants
数组没有值为 0 的键。鉴于您创建该数组的方式,这表明 count($request->participant_name)
为 0。您是否尝试过 @ 987654329@看看里面有什么?
使用 var 转储,例如 " for ($i = 0; $i participant_name); $i++) var_dump($request->participant_name);"它出现“SQLSTATE [23000]:完整性约束违规:1048列'ticket_type_id'不能为空(SQL:插入participants
(name
,surname
,registration_id
,ticket_ype_id
)值(,,6 , ,))" "
这绝对看起来像您的 $request
对象没有您期望的值。你需要研究它是如何产生的......
【参考方案1】:
只搜索0
:访问数组的键时,您必须确保该键存在。
我看到 4 个带有 $i = 0;
声明的 for 循环,它们习惯于访问数组但从未检查过。此外,您可能只想使用 foreach 循环,但这取决于您。
检查密钥是否存在
if (isset($request->participant_question_id[$i]))
$answer = Answer::create([
'question_id' => $request->participant_question_id[$i],
// the error undefined offset is here
'participant_id' => $participants[$i]->id,
'answer' => $request->participant_question[$i],
]);
foreach 方法
foreach ($request->participant_question as $key => $question)
$answer = Answer::create([
'question_id' => $question,
// the error undefined offset is here
'participant_id' => $participants[$i]->id,
'answer' => $request->participant_question[$i],
]);
【讨论】:
谢谢,但是 foreach 就像 "foreach ($request->participant_question as $key => $question) $answer = Answer::create([ 'question_id' => $question, 'participant_id' => $participants[$i]->id, 'answer' => $request->participant_question[$i], ]); " 仍然出现 "message : "Undefined offset: 0"。 是的,我刚刚为questions
做了示例。但是我需要知道:1)participants
来自哪里,2)它们真的与questions
共享相同的密钥吗?如果 2 的答案是 yes,您可以使用$participants[$key]
。 participant_question
也一样
参与者来自注册表,如果“all_participants”为1,则需要收集每个参与者的姓名和姓氏。所以该部分在注册表单代码中的“@if($allParticipants == 1)...”中。
问题是另一回事,如果有自定义问题与用户在上一页(大会详细信息页面)中选择的票证类型相关联,那么问题也会出现在注册表中。以上是关于如何解决这个未定义的偏移量:0 错误?的主要内容,如果未能解决你的问题,请参考以下文章