Why does bindValue set fields to the last value?

87 views Asked by At

I am trying to run an UPDATE from a $_POST array using a foreach statement with bindValue. But the fields in the table are always set to the last values. I thought that this problem would arise only if I used bindParam? I call the bind function as follows:

public function bind($param, $value, $type = null){
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                    default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

And then update the table with the following foreach statement

        $database->query('UPDATE documents SET
    document=:document,
    dateissued=:dateissued,
    expirydate=:expirydate
    WHERE peopleid=:peopleid');

foreach ($_POST['document'] as $i => $item) {
        $trim_value = trim($item);
                if (!empty($trim_value)){
                                        $database->bind(':document',$_POST['document'][$i]);
                                        $database->bind(':dateissued',$_POST['dateissued'][$i]);
                                        $database->bind(':expirydate',$_POST['expirydate'][$i]);
                                        $database->bind(':peopleid',$_POST['peopleid'][$i]);
                }
                $database->execute();

    }

Any idea why this would be happening? I use the same function with INSERT and it works fine. Any help would be great!

0

There are 0 answers