SQLSTATE[HY000]: 常规错误: -1310 [Microsoft][ODBC Microsoft 访问驱动程序] 对象无效或不再设置

SQLSTATE[HY000]: General error: -1310 [Microsoft][ODBC Microsoft Access Driver] Object invalid or no longer set

提问人:Karl Gadd 提问时间:11/16/2023 最后编辑:Karl Gadd 更新时间:11/16/2023 访问量:33

问:

总的来说,我是使用 PDO 和 php 的初学者,我很快就没有头发可以为此拔出汗毛了。

一直在调试,似乎找不到此错误背后的原因。谁能发现我的错误?

基本上,我正在尝试在同一提交表单中更新两个表。我似乎成功地将索引 .php 中的所有数据传递到我的更新.php,但我无法在同一提交中执行两个语句。我已经指出了错误发生在我的第一个.如果我注释掉我的第二个表的内容,它就可以工作,但我无法弄清楚为什么。$stmt->execute();

这也是我第一次像这样向 StackOverflow 提交编码问题,所以如果我遗漏了任何内容,请告诉我!:)

完整错误代码: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"; //Get db connection
        
        //echo '<script> var message = "update-inc: "+'.json_encode($postId).'; alert(message);</script>';
        
        
        // ------- QUERY HERE --------
        $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();
                    // but not here
                    
                    $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("Could not find database file.");
}

$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 "Connection failed: " . $e->getMessage();
}
?>
PHP HTML MS-ACCESS PDO

评论

0赞 ADyson 11/16/2023
撇开实际问题不谈,您到底为什么要使用 Access 作为 Web 应用程序的后端?它根本不是为它设计的,也不是为它设计的。这将是你第一次头疼的问题。我建议使用像MySQL这样的东西,甚至SQLite会更好。
0赞 Karl Gadd 11/16/2023
真诚地?我需要一个数据库,并注意到 Access 默认安装在我的工作笔记本电脑上,我想我会尝试一下并使用它。哈哈。。。我的公司有很多限制。我想MySQL也应该得到批准,因为XAMPP是。我应该重写吗?
0赞 ADyson 11/16/2023
如果这个应用程序将被超过 2 人使用,那么我绝对会说是的。而且,通过PDO与其他数据库平台合作通常会更容易。如果您安装了 XAMPP,那么您可能已经安装了 MySQL 或 MariaDB 并可用。
0赞 Karl Gadd 11/16/2023
谢谢,我来看看这个。出于好奇,你知道我为什么会收到错误吗?
0赞 ADyson 11/16/2023
老实说,不,我没有。但是访问本身就是一条法律,所以我不评价你轻易修复它的机会

答: 暂无答案