모든 테이블의 항목을 하나 또는 여러 개의 문자열 값으로 업데이트 할 수있는 PHP 함수를 작성했습니다. 스크립트가 작동하지 않는 것처럼 보이지만 PDO는 오류를 발생시키지 않습니다! 데이터베이스, 테이블 및 필드의 이름을 여러 번 확인했습니다. 그것은 모두 정확합니다. 이 함수는 작동하지 않는 유일한 함수입니다. 나는 그것이 SQL 문과 PDO -> bindParam() 함수에 전달되는 배열 im과 관련이 있다고 생각한다.복잡한 PDO MYSQL 쿼리가 작동하지 않습니다.
코드 :
public function updateTableDetail($table, $id, $params) {
include($this->doc_root . 'config/config.php');
if (is_array($params)) {
foreach ($params as $param) {
$param = Utilities::escapeString($param);
}
} else {
throw new InvalidInputException(InputErrors::NOTANARRAY);
}
if (is_nan($id)) throw new InvalidInputException(InputErrors::NOTANUMBER);
$table = Utilities::escapeString($table);
$sql = "UPDATE " . $table . "
SET " . $config['table_field_updated'] . " = :updated";
while (current($params)) {
$sql .= "," . key($params) . " = :" . key($params);
next($params);
}
reset($params);
$sql .= " WHERE id = :id
AND " . $config['userId'] . " = :userId";
if ($this->serverConnector == null) {
$this->serverConnector = new ServerConnector();
}
if ($this->db == null) {
$this->db = $this->serverConnector->openConnectionOnUserDb($this->dbname);
}
$stmt = $this->db->prepare($sql);
$updated = date("Y-m-d H:i:s");
$stmt->bindParam(':updated',$updated);
$stmt->bindParam(':id',$id);
$stmt->bindParam(':userId',$this->userId);
while ($param = current($params)) {
$stmt->bindParam(":".key($params),$param);
next($params);
}
reset($params);
$stmt->execute();
}
편집 : - 배열과 클래스 변수 (가) 문은 $ 설정 []을 포함 대해 걱정하지 마십시오. 모두 잘 작동합니다. 이미 그들의 가치를 시험했다.
그럼 작동하지 않는 것은 무엇입니까? – Halcyon
** 동적 매개 변수 중 어떤 것도'updated','id' 또는'userID'를 복제하지 않는다는 사실을 확신합니까? 매개 변수 이름은 쿼리 내에서 고유해야합니다. 바인드 호출의 리턴 값을 확인하는 것을 귀찮게하지 않고 성공했다고 가정합니다. –
모든 바인드 - 콜을 검사하는 것은 좋은 습관입니까? 도움을 주셔서 감사합니다. Akam이 이미 문제를 해결했지만 – JustBasti