简体   繁体   English

PDO无法将新记录插入Access数据库,但可以在同一连接中读取和删除

[英]PDO can't insert new record into Access database, but can read and delete in same connection

I have a problem. 我有个问题。 I have a ms Access DB files, which I have to read and modify (delete and insert records). 我有一个ms Access数据库文件,我必须阅读和修改(删除和插入记录)。 I use PDO odbc. 我使用PDO odbc。 And I have strange situation where I can read and delete but I cann't insert new records in DB with same connection. 我有奇怪的情况,我可以读取和删除但我不能在具有相同连接的数据库中插入新记录。 I have already checked insert string in ms Access and it works perfectly . 我已经在ms Access中检查了插入字符串,它运行正常
Here php code: 这里的PHP代码:

<?php

$path = getcwd() . "\\tempFolder\\MSDB.mdb";
$con = "odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$path; Uid=; Pwd=;";
try
{
    $db = new PDO($con);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //READ
    $cmd = "SELECT * FROM temp_HEADER WHERE [LINE-ID]='101'";
    $result = $db->prepare($cmd);
    $result->execute();
    $result = $result->fetch(PDO::FETCH_ASSOC);
    print_r($result); echo "<br/>";

    //DELETE
    $cmd = "DELETE FROM temp_HEADER WHERE [LINE-ID]='101'";
    $result = $db->prepare($cmd);
    $result->execute();
    echo "DELETE OK<br/>";

    //INSERT
    $cmd = "INSERT INTO temp_HEADER ([LINE-ID], PLANE, DISPLAY, X, Y, Z, LENGTH, SURF, ROCK, VARIA, LTYPE, NAME) "
    . "VALUES('101', '100', '', 100, 100, 100, 0, 0, '', 1, 'LINE', 'TEMP')";
    $result = $db->prepare($cmd);
    $result->execute();    
    echo "INSERT OK";

} catch (Exception $ex) {
    echo $ex->getMessage();
}



This code get me following result: 这段代码让我得到以下结果:

Array ( [LINE-ID] => 101 [PLANE] => 100 [DISPLAY] => [X] => 100.0 [Y] => 100.0 [Z] => 100.0 [LENGTH] => 0.0 [SURF] => 0.0 [ROCK] => [VAR] => 1 [LTYPE] => LINE [NAME] => TEMP [Ind] => 1041 ) 
DELETE OK
SQLSTATE[22018]: Invalid character value for cast specification: 39 [Microsoft][Driver ODBC Microsoft Access]Invalid character value for cast specification (null) (SQLExecute[39] at ext\pdo_odbc\odbc_stmt.c:254)



What is wrong with this? 这有什么问题?

ps: I tested this one script in several computers and some of them show all results well, but another - got error in insert command. ps:我在几台计算机上测试了这个脚本,其中一些很好地显示了所有结果,但是另一个 - 在insert命令中出错了。

EDIT: Add type of fields: 编辑:添加字段类型:

LINE-ID - Text<br/>
PLANE - Text<br/>
DISPLAY - Text<br/>
X - Number(Double)<br/>
Y - Number(Double)<br/>
Z - Number(Double)<br/>
LENGTH - Number(Double)<br/>
SURF - Number(Double)<br/>
ROCK - Text<br/>
VARIA - Number(Long Integer)<br/>
LTYPE - Text<br/>
NAME - Text<br/>
Ind - AutoNumber(Long Integer)<br/>



EDIT: I have just written the same code in C# and everything is OK (I could read, delete and insert). 编辑:我刚刚在C#中编写了相同的代码,一切正常(我可以阅读,删除和插入)。 I used the same sql queries as here, but I used oleDB. 我使用了与此处相同的SQL查询,但我使用了oleDB。 In this case I think that something wrong with odbc with pdo in php. 在这种情况下,我认为odbc与php中的pdo有问题。 Couldn't someone tell me how can I connect to DB via oleDB in php because fast search didn't get any result? 难道有人告诉我如何通过php中的oleDB连接数据库,因为快速搜索没有得到任何结果?

EDIT: I tried to use code with COM (OleDB). 编辑:我试图使用COM(OleDB)代码。 I repeated the same task with read, delete and insert. 我通过读取,删除和插入重复了相同的任务。 What I got: 我得到了什么:

variant Object 
DELETE OK
Source: Microsoft JET Database Engine
Description: type Mismatch



EDIT: I created mdb file and simple script with read, delete and insert function and put it here . 编辑:我创建了mdb文件和简单的脚本与读取,删除和插入功能,并把它放在这里 This code doesn't work for me. 此代码对我不起作用。 If you can run it without problem, please, write down the version of your driver. 如果您可以毫无问题地运行它,请记下您的驱动程序版本。 I think the problem in driver. 我认为司机的问题。 In my case, I've already tried to change ODBC shortcut from system32 into sysWOW64, but nothing changes. 就我而言,我已经尝试将ODBC快捷方式从system32更改为sysWOW64,但没有任何改变。

According to this list , var is a reserved word in MS Access. 根据此列表var是MS Access中的保留字。

One of your variables is called var , so you are getting a syntax error. 您的一个变量称为var ,因此您会收到语法错误。 Wrap the name in square braces. 用方括号包裹名称。

EDIT: 编辑:

The error suggests that there is a type conflict between one of the columns and the value you are providing. 该错误表明其中一列与您提供的值之间存在类型冲突。 In any case, the over-riding idea is the same. 无论如何,最重要的想法是一样的。 The connection is fine. 连接很好。 The SQL has syntax errors. SQL有语法错误。

What type is the "Display" field. 什么类型的“显示”字段。 You seems to be inserting it into the database, yet the print out says its a two dimensional array 您似乎将其插入到数据库中,但打印输出表明它是一个二维数组

how can I connect to DB via oleDB in php 如何通过php中的oleDB连接到DB

To perform the INSERT via OLEDB you would use something like this 要通过OLEDB执行INSERT,您可以使用类似的东西

<?php
// this code requires the following php.ini directive:
//
// extension=php_com_dotnet.dll

$path = getcwd() . "\\tempFolder\\MSDB.mdb";
$con = new COM("ADODB.Connection"); 
$con->Open(
        "Provider=Microsoft.Jet.OLEDB.4.0;" .
        "Data Source=$path");

$cmd = "INSERT INTO temp_HEADER ([LINE-ID], PLANE, DISPLAY, X, Y, Z, LENGTH, SURF, ROCK, VARIA, LTYPE, NAME) "
    . "VALUES('101', '100', '', 100, 100, 100, 0, 0, '', 1, 'LINE', 'TEMP')";       

$con->Execute($cmd);

$con->Close();

If you are dealing with Unicode text (as you mention in the comments to the question) then I strongly suggest that you abandon the PDO_ODBC approach because it is doomed. 如果您正在处理Unicode文本(正如您在问题的评论中提到的那样),那么我强烈建议您放弃PDO_ODBC方法,因为它注定要失败。 PHP and Access ODBC do not play well together when Unicode characters are involved. 当涉及Unicode字符时,PHP和Access ODBC 无法很好地协同工作。

There is no point in preparing if you are going to harcode the value. 准备你是否要对这个值进行编码是没有意义的。

On the other hand it is a risk you are taking as you can make a mistake and data type won't match. 另一方面,由于您可能会犯错并且数据类型不匹配,因此您承担风险。

It looks like you are inserting coordinates, for instance a blank string ('') will not convert to float. 它看起来像是在插入坐标,例如空字符串('')不会转换为float。

Try to prepare this query properly: 尝试正确准备此查询:

$cmd = "INSERT INTO 
        temp_HEADER (`[LINE-ID]`, `PLANE`, `DISPLAY`, 
                     `X`, `Y`, `Z`, `LENGTH`, `SURF`, 
                     `ROCK`, `VARIA`, `LTYPE`, `NAME`) "
    . "VALUES(  ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$result = $db->prepare($cmd);
$success = $result->execute(array('101', '100', '', 
                                  100, 100, 100, 0, 0, 
                                  '', 1, 'LINE', 'TEMP'));    
if($success){
    echo "INSERT OK";
}else{
    echo "INSERT FAIL";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM