如何使用PHP将附件插入Access数据库?

时间:2022-09-26 08:51:31

I need to add some files uploaded from an HTML form to an MS Access DDBB. Using PHP, SQL and ODBC I haven't had any problem queryng to the Access file, except for attachment fields.

我需要将从HTML表单上传的一些文件添加到MS Access DDBB。使用PHP,SQL和ODBC我没有任何问题查询Access文件,附件字段除外。

INSERT INTO TEMAS (DATOSADJUNTOS.FILENAME, DATOSADJUNTOS.FILEDATA) VALUES ("ExampleName.txt", "Wathever") WHERE ID = 4;

This query returns the following error:

此查询返回以下错误:

SQLSTATE[07009]: Invalid descriptor index: -1003 [Microsoft][Controlador ODBC Microsoft Access] Argumento no válido. (SQLExecute[-1003] at ext\pdo_odbc\odbc_stmt.c:260)

No matter what I put instead of "Wathever", the error is always the same. Except if it is an empty string, then the query runs with no problem, and actually works, as you can see in the next image.

无论我放什么而不是“Wathever”,错误总是一样的。除非它是一个空字符串,否则查询运行没有问题,并且实际上正常工作,如下图所示。

Any idea of what should I put in the query to make it work, or any other way of inserting an attachment into a MS Access DDBB?

有什么想法我应该在查询中使它工作,或任何其他方式将附件插入MS Access DDBB?

Thanks

谢谢

2 个解决方案

#1


2  

Erik is right: You can't insert an Attachment using ODBC. You need to use Access DAO, like so:

Erik是对的:您无法使用ODBC插入附件。您需要使用Access DAO,如下所示:

$dbe = new COM("DAO.DBEngine.120") or die("Cannot create DBEngine object.");
$db = $dbe->OpenDatabase("C:\\Users\\Public\\Database1.accdb");
$rsMain = $db->OpenRecordset("SELECT DATOSADJUNTOS FROM TEMAS WHERE ID=4", 2);  // dbOpenDynaset
$rsMain->Edit;
$rsAttach = $rsMain->Fields("DATOSADJUNTOS")->Value;
$rsAttach->AddNew;
$fldAttach = $rsAttach->Fields("FileData");
$fldAttach->LoadFromFile("C:\\Users\\Gord\\Desktop\\sample.pdf");
$rsAttach->Update;
$rsAttach->Close;
$rsMain->Update;
$rsMain->Close;
$db->Close;

Note that Access DAO is a component of the Access Database Engine, so this approach does not require a full install of the Microsoft Access application.

请注意Access DAO是Access数据库引擎的一个组件,因此这种方法不需要完全安装Microsoft Access应用程序。

#2


1  

As far as I know, this is not possible when only using PHP and ODBC. You will need something that can interface directly with Access through COM or the command-line.

据我所知,仅使用PHP和ODBC时,这是不可能的。您将需要能够通过COM或命令行直接与Access连接的东西。

You could create a macro to either import all files from a certain folder in Access, or to listen to the command-line and import a single file. Then you could trigger that using shell_exec in PHP. That means you will need to have Access on the server running the commands.

您可以创建一个宏来从Access中的某个文件夹导入所有文件,或者监听命令行并导入单个文件。然后你可以在PHP中使用shell_exec触发它。这意味着您需要在运行命令的服务器上安装Access。

It isn't easy. I can give you pointers on steps if you really want to do this.

这并不容易。如果你真的想这样做,我可以给你指出步骤。

See this google discussion where a Microsoft employee stated ACE ODBC has no support for attachment fields.

请参阅此谷歌讨论,其中Microsoft员工声明ACE ODBC不支持附件字段。

(If someone does know a way, I'd be happy to be corrected)

(如果有人知道某种方式,我很乐意得到纠正)

#1


2  

Erik is right: You can't insert an Attachment using ODBC. You need to use Access DAO, like so:

Erik是对的:您无法使用ODBC插入附件。您需要使用Access DAO,如下所示:

$dbe = new COM("DAO.DBEngine.120") or die("Cannot create DBEngine object.");
$db = $dbe->OpenDatabase("C:\\Users\\Public\\Database1.accdb");
$rsMain = $db->OpenRecordset("SELECT DATOSADJUNTOS FROM TEMAS WHERE ID=4", 2);  // dbOpenDynaset
$rsMain->Edit;
$rsAttach = $rsMain->Fields("DATOSADJUNTOS")->Value;
$rsAttach->AddNew;
$fldAttach = $rsAttach->Fields("FileData");
$fldAttach->LoadFromFile("C:\\Users\\Gord\\Desktop\\sample.pdf");
$rsAttach->Update;
$rsAttach->Close;
$rsMain->Update;
$rsMain->Close;
$db->Close;

Note that Access DAO is a component of the Access Database Engine, so this approach does not require a full install of the Microsoft Access application.

请注意Access DAO是Access数据库引擎的一个组件,因此这种方法不需要完全安装Microsoft Access应用程序。

#2


1  

As far as I know, this is not possible when only using PHP and ODBC. You will need something that can interface directly with Access through COM or the command-line.

据我所知,仅使用PHP和ODBC时,这是不可能的。您将需要能够通过COM或命令行直接与Access连接的东西。

You could create a macro to either import all files from a certain folder in Access, or to listen to the command-line and import a single file. Then you could trigger that using shell_exec in PHP. That means you will need to have Access on the server running the commands.

您可以创建一个宏来从Access中的某个文件夹导入所有文件,或者监听命令行并导入单个文件。然后你可以在PHP中使用shell_exec触发它。这意味着您需要在运行命令的服务器上安装Access。

It isn't easy. I can give you pointers on steps if you really want to do this.

这并不容易。如果你真的想这样做,我可以给你指出步骤。

See this google discussion where a Microsoft employee stated ACE ODBC has no support for attachment fields.

请参阅此谷歌讨论,其中Microsoft员工声明ACE ODBC不支持附件字段。

(If someone does know a way, I'd be happy to be corrected)

(如果有人知道某种方式,我很乐意得到纠正)