0

I'm trying to implement a method to update users from the admin page through a form. I have a generic method update because I'm updating my own MVC framework.

public function update()
    {
        $tableName = $this->tableName();
        $attributes = $this->attributes();
        $params = array_map(fn($attr) => ":$attr", $attributes);
        $paramsArray = array_combine($attributes, $params);
        $list = [];
        foreach ($paramsArray as $key => $value) {
            $list[] = "$key=$value";
        }
        $primaryKey = $this->primaryKey();
        $primaryValue = $this->{$primaryKey};
        try {
            $statement = self::prepare("UPDATE $tableName SET " . implode(', ', $list) . " WHERE $primaryKey = $primaryValue");
            foreach ($attributes as $attribute) {
                $statement->bindValue(":$attribute", $this->{$attribute});
            }
            $statement->execute();
            return true;
        } catch (\PDOException $e) {
            echo "Check Input Error: " . $e->getMessage() . "</br>";
        }
    }

So, this method (with my user) give me this statement :
'UPDATE users SET firstname=:firstname, lastname=:lastname, email=:email, password=:password, status=:status, type=:type WHERE id = 10'.
I don't catch any PDOException, but nothing change in my database.
I setted PDO attributes like that :
pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
Things I checked :

  • I dumped this part $this->{$attribute} in my foreach and the values I'm getting are fine.
  • I've tried to use the statement above directly in PHPMyAdmin and replacing the bind values by the real values and everything works fine.
  • I also tried to change the statement to create a mysql query error and I had one.

Maybe I'm missing something from the whole update process but I have a save method for database INSERT that use kind of the same logic and it works fine.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Should `$statement->bindValue(":$attribute", $this->{$attribute});` not be `$statement->bindValue(":$attribute", $attribute);`? – Professor Abronsius Dec 27 '20 at 10:00
  • This method is clearly part of a larger whole. That makes it difficult for us to solve your problem, unless it is something obvious. I don't see anything. I mean Prof. Abronsius could be right, but who knows? I did notice you didn't say that you actually checked what the query and values have become, and whether it actually executes. I would expect so, and they are simple checks, so why not do them? – KIKO Software Dec 27 '20 at 10:02
  • Make sure the code actually runs and you provided correct values. There is no magic. ether query executed successfully or there is an error. – Your Common Sense Dec 27 '20 at 10:02
  • @KIKOSoftware he is obviously not right because $attribute contains the name not value – Your Common Sense Dec 27 '20 at 10:08
  • @YourCommonSense Well, the object might have properties with `$attribute` names. They are used as property names: `$this->{$attribute}`. I do agree that looks very weird, but who are we to judge? – KIKO Software Dec 27 '20 at 10:12
  • @KIKOSoftware thanks for your answer. I'm afraid I don't really know how to check what the query and values have become.. – Nicolas Halberstadt Dec 27 '20 at 10:14
  • the code should be like this tho https://3v4l.org/3uUP4 not to fix some imaginary error but just for sake of simplicity – Your Common Sense Dec 27 '20 at 10:15
  • @YourCommonSense thanks as well for your answer. To be honest, I followed 'TheCodeHolic' course to create a PHP MVC framework and I'm trying to implement other methods. So I'm using the logic he used for the other methods – Nicolas Halberstadt Dec 27 '20 at 10:16
  • @NicolasHalberstadt There are many ways to check what PHP actually does. You can simply echo what is happening. You could make your own little log system. The possibilities are endless, just do something that will tell you the query has actually executed. I like Your Common Sense code better: more to the point. – KIKO Software Dec 27 '20 at 10:19

0 Answers0