gpt4 book ai didi

php - Laravel SQLSTATE[23000] : Integrity constraint violation: 1452 Cannot add or update a child row

转载 作者:行者123 更新时间:2023-11-29 02:51:12 24 4
gpt4 key购买 nike

我正在尝试使用 laravel 框架制作一个基本的简单 CRUD,我设法制作了一个没有选择表单的简单 CRUD,但是当我使用选择表单时,它收到错误消息

完全错误

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (activity_report_2015-04-22.tn_project, CONSTRAINT fk_client_id FOREIGN KEY (cv_client_id) REFERENCES tn_client (cn_id)) (SQL: insert into tn_project (cv_created_by, cn_created_at, cv_id, cv_name, cv_client_id, cn_invoice_method, cn_project_rate, cn_note) values (ada, 2016-02-18 23:52:45, TestProject, TestName, $client->cn_id, $invoice->cn_id, 1.2, JustTest))

cv_client_idcv_invoice_method 的值来自不同的表,即 tn_clienttn_invoice_method,我注意到为什么 cv_client_idcv_invoice_method 结果查询的值比值大,知道吗?

如果你们需要更多信息,请告诉我

View

<div class="form-group">
<label class="col-md-3 control-label">Client</label>
<div class="col-md-9">
<select name="clientId" id="clientId" class="form-control" placeholder="Select Client">
@foreach ($clients as $client)
<option value='$client->cn_id'>{{$client->cv_name}}</option>;
@endforeach
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Invoice Method</label>
<div class="col-md-9">
<select type="hidden" name="invoiceId" id="invoiceId" class="form-control">
@foreach ($invoices as $invoice)
<option value='$invoice->cn_id'>{{$invoice->cv_method}}</option>;
@endforeach
</select>
</div>
</div>

Controller

public function createOrEdit(){
$currentUsername = Auth::user()->name;

$isUpdate = false;
$projectId = Input::get('prevId');

//populate data
$project = new Project;
if($projectId != ""){
$project = Project::where('cv_id','=',$projectId)->firstOrFail();
$project->cv_updated_by = $currentUsername;
$project->cn_updated_at = Carbon::now();
$isUpdate = true;
} else{
$project->cv_created_by = $currentUsername;
$project->cn_created_at = Carbon::now();
}

$project->cv_id = Input::get('projectId');
$project->cv_name = Input::get('projectName');
$project->cv_client_id = Input::get('clientId');
$project->cn_invoice_method = Input::get('invoiceId');
$project->cn_project_rate = Input::get('projectRate');
$project->cn_note = Input::get('note');

//execute
if($isUpdate){
Log::info("entering update mode");
Project::where('cv_id','=',$projectId)->update(['cv_id'=>$project->cv_id,
'cv_name'=>$project->cv_name,
'cv_client_id'=>$project->cv_client_id,
'cn_invoice_method'=>$project->cn_invoice_method,
'cn_project_rate'=>$project->cn_project_rate,
'cn_note'=>$project->cn_note,
'cn_updated_at'=>$project->cn_updated_at,
'cv_updated_by'=>$project->cv_updated_by]);
}else{
$project->save();
}

return Response::json(
array(
'content' => Project::with('client','invoice')->get(),
'status' => 'success',
)
);
}

注意:如果我在我的 Controller 中手动输入 cv_client_idcv_invoice_method 的值,它工作正常

最佳答案

你忘记回显 $client->cn_id <option value='{{$client->cn_id}}'>{{$client->cv_name}}</option>;

同样在这里: <option value='{{$invoice->cn_id}}'>{{$invoice->cv_method}}</option>;

关于php - Laravel SQLSTATE[23000] : Integrity constraint violation: 1452 Cannot add or update a child row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35494982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com