1

I have a update function that for now updates the required changes to MySQL database when I run index.php.

This is updating my password buy not the name field, ive been over the code and can not work out why. Any help is greatly appreciated.

Index that tells what id and fields to update with entered data

    <?php
require_once 'core/init.php';

$userInsert = DB::getInstance()->update('users', 1, array(
    'password'   => 'newpass',
    'name'       => 'Ben'
));

Function in different php that updated database

    public function update($table, $id, $fields) {
    $set = '';
    $x = 1;

    foreach($fields as $name => $value) {
        $set .= "{$name} = ?";
        if($x < count($fields)) {
            $set .= ',';
        }
        $x++;
    }

    $sql = "UPDATE {$table} SET {$set} = 'newpassword' WHERE id = {$id}";

    if(!$this->query($sql, $fields)->error()) {
        return true;
    }

    return false;
}

I believe it to be a small error or mistype but I can not see the problem.

As you can see bellow the password field has been changed but the name has not enter image description here

7
  • Echo out the query after you've filled in the variables, and you'll see what's wrong with it. Commented Jan 7, 2014 at 21:15
  • 1
    Take Jessica's approach, the problem is banging you on the nose. $sql = "UPDATE {$table} SET {$set} = 'newpassword' WHERE id = {$id}"; Commented Jan 7, 2014 at 21:17
  • You're never actually updating your username. SET username = 'blah',password = 'blahblah' Commented Jan 7, 2014 at 21:17
  • @InGodITrust Um.... apparently its banging you on the nose too Commented Jan 7, 2014 at 21:21
  • @InGodITrust Have you ever seen a query that looks like this work? UPDATE TABLE SET username = ? = 'newpassword' Commented Jan 7, 2014 at 21:22

3 Answers 3

4
public function update($table, $id, $fields) {
    $set = '';
    $x = 1;

    foreach($fields as $name => $value) {
        $set .= "{$name} = \"{$value}\"";
        if($x < count($fields)) {
            $set .= ',';
        }
        $x++;
    }

    $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";

    if(!$this->query($sql, $fields)->error()) {
        return true;
    }

    return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@InGodITrust It does fix the problem. whats your deal?
@InGodITrust this answer is pretty self-explanatory if you read the question: it's the OP's code, slightly modified to remove the illogic.
1

Simply use of prepare and execute in PDO:

$sql = 'UPDATE '. $table .' SET username = :username, password = :password WHERE id = '. $id;
$sth = $dbh->prepare($sql);
$sth->execute(array(
    ':username' => 'ben',
    ':password' => 'newpassword'
));

2 Comments

This works but it doesn't fix his problem but showing him what was wrong
This is a good practice in general, but if you look at his code, he is trying to make a "universal" update function that will allow for any number of fields and can be applied on any table.
0
    private function update($table, $primaryKey, $fields) {
        $query = 'UPDATE `' . $this->table . '` SET ';
        
        foreach ($fields as $key => $value) {
            $query .= '`' . $key . '` = :' . $key . ',';
        }

        $query = rtrim($query, ',');

        $query .= ' WHERE `' . $this->primaryKey . '` = :primaryKey';

        $fields['primaryKey'] = $fields['id'];
    
        $this->query($query, $fields);

    }

An example of an update function. Attention mine is inside a class and the query is another function and passes as an object.

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.