I have a function where I set a few parameters, these parameters I want to pass on later in my SQL query as a variable.
My goal is not to write an extra function for every color I have in my game.
I want a function and whenever I enter the parameters it should be processed in my SQL query.
public function setVillageRes(int $villageID, int $ownerID, $res, $fabrik){
$stmt = $this->pdo->prepare("SELECT * FROM village WHERE villageID = ? AND ownerID = ?");
$stmt->execute([$villageID, $ownerID]);
$result = $stmt->fetch();
if(is_array($result)){
$sql = "UPDATE village SET `$res` = ? WHERE `$villageID` = ? AND `$fabrik` = ?";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([$res, $villageID, $fabrik]);
} else {
return "RE-3001";
}
}
$villageID= ? would result in something like 1=1 which is always true, so why do you try to substitute the columnnames?