php - Complicated PDO MYSQL query not working -
i've written php function allows update entry in table string values (one or multiple). pdo not throw errors, though script not seem work! i've checked name of database, tables , fields multiple times. it's correct. query in functions not work. believe has array im passing in sql statement , pdo->bindparam() function.
code:
public function updatetabledetail($table, $id, $params) { include($this->doc_root . 'config/config.php'); if (is_array($params)) { foreach ($params $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 .= " id = :id , " . $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(); }
edit: don't worry include statement, $config[]-array , class-variables. it's working aswell. tested values already.
change part:
while ($param = current($params)) { $stmt->bindparam(":".key($params),$param); next($params); }
to:
foreach($params $key => &value){ $stmt->bindparam(":$key",$value); }
because according php manual: pdostatement::bindparam
binds php variable corresponding named or question mark placeholder in sql statement use prepare statement. unlike pdostatement::bindvalue(), variable bound reference , evaluated @ time pdostatement::execute() called.
Comments
Post a Comment