0

I am trying to use Pear MDB2 object in order to execute a query against my database, but it isn't working. Instead I am receiving an error Unable to bind to missing placeholder on the execute(). I have a sql builder function, which returns a sql statement and parameters to a store data function which also accepts an array of values to bind to the statement. NOTE: I substituted $params for $_POST they are both an array of form inputs. Except I validate all input store it in an object for later use by the application and then store it in the database. The database connection is MDB2. The function userinfo() returns the user object variables.

public function build_sql_Insert($arry, $table)
    {
        if(!is_array($arry)){
            return false;
        }

        $types = str_repeat("s", count($arry));
        $keys = array_keys($arry);
        $values = array_values($arry);

        if ($table==="user") {
            $sql = "INSERT INTO $table (" . implode(',', $keys) . ") VALUES (".implode(',', array_fill(0, count($values), '?')).")ON DUPLICATE KEY UPDATE ";
            $updates = implode(',', array_map(function($col) {
                return "$col = VALUES($col)";
            }, $keys));

            $sql .= $updates;
            return $sql;
        }
        $sql = "INSERT INTO $table (" . implode(',', $keys) . ") VALUES (" .
            implode(',', array_fill(0, count($values), '?')) . ")";

        return $sql;
    }

Store Data

public function storeData($sql, $cxn, $params){

        if($stmt = $cxn->prepare($sql, MDB2_PREPARE_MANIP)){
            $afft = $stmt->execute($params);
            $stmt->free();

            return 1;
        }
        return 0;

    }

Passed user object which is returned as an array calling userInfo():

WI_User Object
(
    [UserId:WI_User:private] => 1
    [FirstName:WI_User:private] => Natasha
    [LastName:WI_User:private] => Harris
    [Email:WI_User:private] => kevlwig@gmail.com
    [Password:WI_User:private] => $2y$10$YrYZs24ihAcRYqBROemTUuTmUQCI4l0zO52pjPGIC2PfKn/tOHC1G
    [UserPix:WI_User:private] => 
    [Status:WI_User:private] => 1
)

SQL statement:

INSERT INTO user (UserId,FirstName,LastName,Email,Password,UserPix,Status) VALUES (?,?,?,?,?,?,?)ON DUPLICATE KEY UPDATE UserId = VALUES(UserId),FirstName = VALUES(FirstName),LastName = VALUES(LastName),Email = VALUES(Email),Password = VALUES(Password),UserPix = VALUES(UserPix),Status = VALUES(Status)

Function calling:

$userdata = $this->user->userInfo();
$sql = $this->build_sql_Insert($userdata, $this->moduleName);
$this->storeData($sql, $this->db, $userdata)
Lamar
  • 7
  • 6

0 Answers0