'Inserting data into SQL Server Db An Invalid direction was specified

I am using SQL Server DB (mssql), And trying to execute Insert Query through PHP.

public function registerCustomer($custId, $custData)
{

    $sqlString = "INSERT INTO CUSTMASTER ( CustId, CustData ) values ( ?, CONVERT(varbinary(max),? )) ; SELECT SCOPE_IDENTITY()";
    $params = array($custId, $custData);
    $stmt = sqlsrv_query($this->conn, $sqlString, $params);
    if ($stmt === false) {
        die(print_r(sqlsrv_errors(), true));
    }

    $rows = sqlsrv_has_rows($stmt);
    sqlsrv_next_result($stmt);
    sqlsrv_fetch($stmt);
    $lastId = sqlsrv_get_field($stmt, 0);

    if ($rows === true) {
       echo "Data Inserted."
    } else {
        return "Data Insert Failed.";
    }

}

I am getting the following error ,

Array
(
    [0] => Array
        (
            [0] => IMSSP
            [SQLSTATE] => IMSSP
            [1] => -15
            [code] => -15
            [2] => An invalid direction for parameter 2 was specified. SQLSRV_PARAM_IN, SQLSRV_PARAM_OUT, and SQLSRV_PARAM_INOUT are valid values.
            [message] => An invalid direction for parameter 2 was specified. SQLSRV_PARAM_IN, SQLSRV_PARAM_OUT, and SQLSRV_PARAM_INOUT are valid values.
        )
)

Note : CustData column in db is varbinary(max) type.

I am sending Base64 String from Android App and then converting the string into byte[] using following code.

 $a = base64_decode($FingerData);
 $custData = array();
 foreach (str_split($a) as $c) {
    $custData[] = sprintf("%08b", ord($c));
 }

How to resolve this issue?



Solution 1:[1]

I ran into the error An invalid direction for parameter # was specified and found this question. My issue, and assuming the OP issue, was that the variable at position # was an array. sqlsrv_query is trying to pars the parameter as a directional parameter.

Solution 2:[2]

If I understand you correctly, you have a base64 encrypted image. So try this:

<?php 

# Only decode $FingerData.
...
$a = base64_decode($FingerData);
...

# Insert data
public function registerCustomer($custId, $custData)
{
    ...
    $sqlString = "INSERT INTO CUSTMASTER (CustId, CustData) VALUES (?, CONVERT(varbinary(max), ?)); SELECT SCOPE_IDENTITY();";
    $params = array(
        array($custId, SQLSRV_PARAM_IN),
        array($custData, SQLSRV_PARAM_IN)
    );
    $stmt = sqlsrv_query($this->conn, $sqlString, $params);
    ...
}
?>

I've made a small script, which is based on your example:

<?php
#------------------------------
# Connection info
#------------------------------
$server = 'server\instance,port';
$database = 'database';
$uid = 'user';
$pwd = 'password';

#------------------------------
# Connection
#------------------------------
$cinfo = array(
    "Database" => $database,
    "UID" => $uid,
    "PWD" => $pwd
);
$conn = sqlsrv_connect($server, $cinfo);
if( $conn === false )
{
    echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
    exit;
}

#------------------------------
# Function
#------------------------------
function registerCustomer($connection, $custId, $custData) {

    $sqlString = "INSERT INTO CUSTMASTER (CustId, CustData) VALUES (?, CONVERT(varbinary(max), ?)); SELECT SCOPE_IDENTITY();";
    $params = array(
        array($custId, SQLSRV_PARAM_IN),
        array($custData, SQLSRV_PARAM_IN)
    );
    $stmt = sqlsrv_query($connection, $sqlString, $params);
    if ($stmt === false) {
        die(print_r(sqlsrv_errors(), true));
    }

    $rows = sqlsrv_has_rows($stmt);
    while (sqlsrv_fetch($stmt)) {
        $lastId = sqlsrv_get_field($stmt, 0);
    }

    if ($rows === true) {
       echo "Data Inserted.";
    } else {
        return "Data Insert Failed.";
    }

    echo $lastId;
}

#------------------------------
# Load image file into database
#------------------------------
# With next two lines I load and encode an image. 
# If I understand you correctly, this is already done and you have a base64 encrypted image.
$image = file_get_contents('image.jpg');
$encoded_image = base64_encode($image);

# Insert image
$decoded_image = base64_decode($encoded_image);
registerCustomer($conn, 1, $decoded_image);
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2