views:

41

answers:

1

I've tried odbc_prepare() + odbc_execute() to update a record in an Access file but I always get an SQL state 07001 error message about incorrect column count (actually, the message is in Spanglish and doesn't make much sense):

<?php
$items = array();
$items[100] = 'Foo';
$items[200] = 'Bar';
$sql = 'UPDATE street
    SET name=?
    WHERE street_id=?';
$stmt = odbc_prepare($conection, $sql);
if( $stmt===FALSE ){
    die(odbc_errormsg());
}
foreach($items as $cod => $name){
    if( !odbc_execute($stmt, array($name, $cod)) ){
        die(odbc_errormsg());
    }
}

User comments at http://es2.php.net/manual/en/function.odbc-execute.php suggest that Microsoft Access ODBC drivers do not support parameterized queries. However, I haven't found an odbc_* function to escape data.

So... How can I escape input data?

+1  A: 

Typically in MS Access, you identify the parameters by placing them in brackets

$sql = 'UPDATE street
        SET name=[myname]
        WHERE street_id=[mystreet]';

How that will mesh with php, I don't know.

CodeSlave
How do you escape brackets? `UPDATE street SET name=[myname [1]] WHERE street_id=[123]`
Álvaro G. Vicario
Try $sql = `UPDATE street SET name=['.myname[1].'] WHERE street_id=' etc... pardon my rusty PHP concat
MikeAinOz
Don't worry about PHP (I'm assuming that if there was a function to escape someone would have pointed out so far). What would be the plain SQL query?
Álvaro G. Vicario
@Mike - Wouldn't putting single quotes in allow for SQL injection?
CodeSlave
Part of the problem with MS Access is that passing parameters through code wasn't documented very well. So everyone who has written up instructions has only written up instructions for the style where MS Access asks the user for input on the fly, rather than passing them in through code. For many years I though the only way to do it was to build the SQL on the fly and submit that (of course leaving your self open to enjection), but then I found this trick - http://stackoverflow.com/questions/95277/how-do-you-create-a-parameterized-query-in-ms-access-2003-and-use-other-queries-f/96134#96134
CodeSlave
@Alvaro You don't need to escape the square brackets, they are not a special character
MikeAinOz
@CodeSlave I agree with you that a VBA solution would be better, however this is currently PHP and any SQL injection issues will have to be handled in PHP
MikeAinOz
Yah, VBA was just intended an example of how it's handled in the VBA side for inspiration. The brackets just indicate where the parameters are. You probably have to dereference the array and pass the values in as you did in the for loop.
CodeSlave