Orange Orange - 1 month ago 7
PHP Question

PDOStatement::execute(): SQLSTATE[HY093]

Here code. Please help me!!! Thanks very much

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined



public function update($table, $data, $cond){
$updateKeys = NULL;
foreach ($data as $key => $value) {
$updateKeys .= "$key=:$key";
}
$updateKeys = rtrim($updateKeys, ",");

$sql = "UPDATE $table SET $updateKeys WHERE $cond";
$stmt = $this->prepare($sql);
foreach ($data as $key => $value) {
$stmt->bindParam(":$key", $value);
}
return $stmt->execute();

}




Answer

You need to put "," in $updateKeys .= "$key=:$key,";.Please use

function update($table, $data, $cond){
        $updateKeys = NULL;
        foreach ($data as $key => $value) {
            $updateKeys .= "$key=:$key,";
        }

        $updateKeys = rtrim($updateKeys, ",");

        echo $sql = "UPDATE $table SET $updateKeys WHERE $cond";
        $stmt = $this->prepare($sql);
        foreach ($data as $key => $value) {
            $stmt->bindParam(":$key", $value);
        }
        return $stmt->execute();

    }