SQLSTATE [22007]将数据插入2个表时错误

发布于 2025-01-21 22:57:19 字数 1008 浏览 0 评论 0原文

我正在尝试将数据插入2个单独的表中; 首先,它将其发送到“ empdetails ”,然后“屏幕截图。

我花了几个小时寻找解决方案,没有找到解决方案,或者我正在寻找错误的来源。

错误时,将数据插入 员工>

我已经禁用了模型中的默认时间戳,但仍然相同。有人可以在这里帮助我吗?我准时不足,需要一些快速的帮助,谢谢。

$empDetail = new EmpDetail();


    
    $empDetail -> cnic = $registerRequest->EmployeeCnic;
    $empDetail -> city = $registerRequest->EmployeeNumber;
    $empDetail -> address = $registerRequest->EmployeeCity;
    $empDetail -> phone = $registerRequest->EmployeeAddress;
    $empDetail -> designation = $registerRequest->EmployeeDesignation;

    $emp = $empDetail -> save();

    $emp = new Employee();
    



    $emp -> name = $registerRequest->EmployeeName;
    $emp -> details = EmpDetail::where('cnic','=', $registerRequest->EmployeeCnic)->first();

    $res = $emp -> save();

I'm trying to insert data into 2 separate tables here;
first, it sends it to "EmpDetails" then "Employees" but gives an error which I've included a screenshot of.

I've spent hours searching for a solution and haven't found a solution or I'm looking at the wrong sources.

Error when inserting data into Employee table

I've disabled the default timestamp in models and it's still the same. Could someone please help me here, I'm short on time and need some quick help please and thank you.

$empDetail = new EmpDetail();


    
    $empDetail -> cnic = $registerRequest->EmployeeCnic;
    $empDetail -> city = $registerRequest->EmployeeNumber;
    $empDetail -> address = $registerRequest->EmployeeCity;
    $empDetail -> phone = $registerRequest->EmployeeAddress;
    $empDetail -> designation = $registerRequest->EmployeeDesignation;

    $emp = $empDetail -> save();

    $emp = new Employee();
    



    $emp -> name = $registerRequest->EmployeeName;
    $emp -> details = EmpDetail::where('cnic','=', $registerRequest->EmployeeCnic)->first();

    $res = $emp -> save();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2025-01-28 22:57:19

我不确定当您创建问题时您是否犯了这个错误,但是我认为您将请求名称放错了位置的数据库字段(城市,地址和电话):

错误:

$empDetail->city = $registerRequest->EmployeeNumber;
$empDetail->address = $registerRequest->EmployeeCity;
$empDetail->phone = $registerRequest->EmployeeAddress;

正确:

$empDetail->city = $registerRequest->EmployeeCity;
$empDetail->address = $registerRequest->EmployeeAddress;
$empDetail->phone = $registerRequest->EmployeeNumber;

*如果这是'情况下,请提供您使用的代码中的更多信息,例如您使用的模型,迁移和控制器,以便我可以尝试在机器上重新创建它并为您提供有用的答案。

I'm not sure if you made this mistake when you were creating your question but I think that you've misplaced the request names with the wrong database fields (city, address, and phone):

Wrong:

$empDetail->city = $registerRequest->EmployeeNumber;
$empDetail->address = $registerRequest->EmployeeCity;
$empDetail->phone = $registerRequest->EmployeeAddress;

Correct:

$empDetail->city = $registerRequest->EmployeeCity;
$empDetail->address = $registerRequest->EmployeeAddress;
$empDetail->phone = $registerRequest->EmployeeNumber;

*If this isn't the case, then please provide more information from your code for example models, migrations, and controllers that you've used so I can try to recreate it on my machine and provide a useful answer for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文