0

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";
        }
    }
4
  • How can I solve this? Commented Sep 6, 2022 at 21:46
  • your update query makes no sense $villageID = ? would result in something like 1=1 which is always true, so why do you try to substitute the columnnames? Commented Sep 6, 2022 at 22:13
  • Is this what oyu want ? stackoverflow.com/questions/14098222/… Commented Sep 8, 2022 at 5:02
  • Does this answer your question? Pass parameters to MySQL script Commented Sep 8, 2022 at 5:02

1 Answer 1

0

You shouldn't use the variables in the UPDATE query, use the column names.

$sql = "UPDATE village SET `res` = ? WHERE `villageID` = ? AND `fabrik` = ?";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.