You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PDO操作Access多表更新报错SQLSTATE[HY000]:-1310求排查

PHP PDO 更新两个表时执行报错问题

我是PHP和PDO的初学者,为这个问题折腾了很久,调试半天也找不到报错原因。我想在同一个提交表单里更新两个表,数据已经成功从index.php传到update-inc.php,但两条更新语句无法执行,错误出现在第一个$stmt->execute();处——如果注释掉第二个表的代码,程序就能正常运行。

完整错误信息:

SQLSTATE[HY000]: General error: -1310 [Microsoft][ODBC Microsoft Access Driver] Object invalid or no longer set. (SQLExecute[-1310] at ext\pdo_odbc\odbc_stmt.c:263) Code: HY000

update-inc.php

try {
        
        require_once "dbh-inc.php"; // 获取数据库连接
        
        //echo '<script> var message = "update-inc: "+'.json_encode($postId).'; alert(message);</script>';
        
        
        // ------- 构建查询语句 --------
        $query ="";
        $oeId=0;
        
            foreach($postId as $key=>$value){
                $tempQuery ="";
                $tempQuery2 ="";
                
                ${"t".$key} = "UPDATE ". $mainTable . " SET";
                
                if($value != null){
                    ${"t".$key} .= " Title = :newTitle".$key .",";
                }
                $tempQuery .= ${"t".$key};
                
                if($value != null){
                    ${"t".$key} = $tempQuery . " CopyText = :newText".$key;
                }
                
                ${"t".$key} .= " WHERE id = :id". $key ."; ";
                $tempQuery2 .= ${"t".$key};
                
                
                $query = $tempQuery2;
                

                if($oeId < 1){
                    
                    $oeQuery = 'UPDATE '. $opEnTable .' SET
                    opening = :newOpening, ending = :newEnding 
                    WHERE id = :oeId;';
                    
                    $stmt = $dbh->prepare($oeQuery);
                    
                    $stmt->bindValue(':oeId', $opEnId[$oeId], PDO::PARAM_INT);
                    $stmt->bindValue(':newOpening', $newOpening[$opEnId], PDO::PARAM_STR);
                    $stmt->bindValue(':newEnding', $newEnding[$opEnId], PDO::PARAM_STR);
                    
                    echo "I'm getting here";
                    $stmt->execute();
                    // 无法执行到这里
                    
                    $stmt->closeCursor();
                    $oeId++;
                }
                
                
                $stmt = $dbh->prepare($query);
                
                if($newTitle != null){
                    $stmt->bindValue(':newTitle'.$key, $newTitle[$key]);
                }

                if($newText != null){
                    $stmt->bindValue(':newText'.$key, $newText[$key]);
                }
                $stmt->bindValue(':id'.$key, $key);
                
                //echo '<script> alert("query: '.$query.'\nkey: '.$key.'\n\nnewTitle: '.$newTitle[$key].'");</script>';
                
                $stmt->execute();
                

                $db = null;
                $stmt = null;               
            }

dbh-inc.php

<?php
$dbStr = 'C:\xampp\htdocs\QuickCopy\QC1.accdb';
$mainTable = "main";
$opEnTable = "openingEnding";

if( !file_exists($dbStr) ){
    die("找不到数据库文件。");
}

$dsn = 'odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};
    DBQ='.$dbStr.';';

try {
    
    $dbh = new PDO($dsn);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
}
catch (PDOException $e) {
    echo "连接失败: " . $e->getMessage();
}
?>

内容的提问来源于Stack Exchange,提问作者Karl Gadd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 01:10:19