简体   繁体   中英

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). I use 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 .
Here php code:

<?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.

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). I used the same sql queries as here, but I used oleDB. In this case I think that something wrong with odbc with pdo in php. Couldn't someone tell me how can I connect to DB via oleDB in php because fast search didn't get any result?

EDIT: I tried to use code with 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 . 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.

According to this list , var is a reserved word in MS Access.

One of your variables is called var , so you are getting a syntax error. 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.

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

To perform the INSERT via OLEDB you would use something like this

<?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. PHP and Access ODBC do not play well together when Unicode characters are involved.

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.

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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