php - 从 View 成功插入后MySQL出现一般错误

标签 php mysql laravel

我在 xampp 中使用 Laravel 5.4 和 PHP 7.2。我正在尝试截断临时表,然后从 MySQL View 插入新数据。代码执行并完全按照我想要的方式执行,但它说存在错误 -

SQLSTATE[HY000]: General error (SQL: Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins)

最初,在将两列插入到我的 View 中后,我遇到了以下错误一般错误:1615 准备好的语句需要重新准备:time_starttime_end 列。

我尝试删除这两列以查看这是否是问题所在,但现在我得到的只是可怕的一般错误。

我尝试过这个答案 Laravel: General error: 1615 Prepared statement needs to be re-prepared 。它不起作用,根据评论,它会导致 ak 验证出现问题。

我找到了这个答案PDO error: “ SQLSTATE[HY000]: General error ” When updating database这与我的问题完全相同,但它没有回答我的问题,因为我没有使用 fetchAll() ,也没有使用任何变量的任何重复变量。

我尝试了以下两种方法来实现我的目标。

截断表 prod_joins_temp

ProdJoinsTemp::truncate();

然后从 View prod_joins 填充表 prod_joins_temp

DB::select('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins');

给我完全相同错误的另一种方法如下

删除表 prod_joins_temp(如果存在)

\Schema::dropIfExists('prod_joins_temp');

然后从 View prod_joins 创建表 prod_joins_temp

DB::select('CREATE TABLE prod_joins_temp AS SELECT * FROM prod_joins;');

这是制作 View 的代码

SELECT
    `scorecard54`.`production`.`date` AS `date`,
    `scorecard54`.`products`.`code` AS `code`,
    `scorecard54`.`production`.`sid_production` AS `sid`,
    `scorecard54`.`production`.`time_start` AS `time_start`,
    `scorecard54`.`production`.`time_end` AS `time_end`,
    `scorecard54`.`production`.`made` AS `made`,
    `scorecard54`.`qcontrol`.`firsts` AS `firsts`,
    `scorecard54`.`qcontrol`.`seconds` AS `seconds`,
    `scorecard54`.`production`.`broken_production` AS `p_broken`,
    `scorecard54`.`qcontrol`.`broken_qcontrol` AS `q_broken`,
    `scorecard54`.`grinding`.`discarded` AS `g_broken`,
    `scorecard54`.`products`.`prod_id` AS `prod_id`
FROM
    (
        (
            (
                `scorecard54`.`production`
            LEFT JOIN `scorecard54`.`products` ON
                (
                    (
                        `scorecard54`.`production`.`prod_code` = `scorecard54`.`products`.`prod_id`
                    )
                )
            )
        LEFT JOIN `scorecard54`.`grinding` ON
            (
                (
                    `scorecard54`.`production`.`sid_production` = `scorecard54`.`grinding`.`sid_grinding`
                )
            )
        )
    LEFT JOIN `scorecard54`.`qcontrol` ON
        (
            (
                `scorecard54`.`grinding`.`tray_id_grinding` = `scorecard54`.`qcontrol`.`tray_id_qcontrol`
            )
        )
    )
WHERE
    (
        `scorecard54`.`grinding`.`have_qcontrol` = 1
    )

这是从 View 中删除并创建表格的函数

public function generateTable(Request $request) {

    // Drop temp table
    \Schema::dropIfExists('prod_joins_temp');

    // re create table with the new data
    DB::select('CREATE TABLE prod_joins_temp AS SELECT * FROM prod_joins;');


    $request->session()->flash('alert-success', 'Success: Reports table regenerated.');
    return Redirect::to('/statistics/');

}

我也尝试过截断然后用这个函数重新填充

public function generateTable(Request $request) {

    // Drop temp table
    ProdJoinsTemp::truncate();

    // re generate table with the new data
    DB::select('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins');   

    $request->session()->flash('alert-success', 'Success: Reports table regenerated.');
    return Redirect::to('/statistics/');

}

我在这里做错了什么?是否有某种 View 缓存?

最佳答案

我已经通过这个答案解决了这个问题 SQLSTATE[HY000]: General error: 2053 error occurs at Laravel

之前我使用以下内容更新表格

DB::select('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins'); 

我更改了要更新的查询的选择部分,一切正常。请看下面

DB::update('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins'); 

我不知道为什么在将字段添加到 View 之前以前的方法有效,但现在它要求我使用更新而不是选择

关于php - 从 View 成功插入后MySQL出现一般错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53778291/

相关文章:

javascript - 无限滚动: Load all data from database and scroll to show more

php - PHP RESTful 应用程序的正确 header ?

PHP 类抽象/继承

mysql - 将c#代码转为mysql函数

php - 即使在 php 中设置时区后,服务器中的时间戳也会给出错误的时间

javascript - 我应该提交由 laravel mix 生成的 css 和 js 吗?

javascript - 删除输入字段更改时的验证错误

php - 如何简单地更新 mysql 中的特定时间戳?

mysql - 如何一次更新多个ID

php - Laravel自定义异常处理程序未运行