0

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!

Giuseppe
  • 1
  • 1
  • Rewrite your code to vanilla PDO and everything would work. – Your Common Sense Jun 12 '20 at 15:11
  • It works for insert but not for update, this is what is confusing me. The only difference I can think of is that with update the html form already has a value which is then modified. But bindValue should work... – Giuseppe Jun 17 '20 at 12:42
  • Thanks, I tried vanilla PDO but it still saves only the last values. It probably has something to do with the fact that I am already using a foreach statement to loop through the fields in the table to build the form. Back to the drawing board... – Giuseppe Jun 20 '20 at 05:17
  • Did you do output your input array for the debugging purpose? – Your Common Sense Jun 20 '20 at 07:26
  • Yes, debugDumpParams() shows the array being updated in each loop correctly, but only the last loop is updated for all records. Confirmed by Var_dump as well. – Giuseppe Jun 21 '20 at 10:26
  • print_r displays Array ([0] => doc1, [1] => doc2, [2] => doc3) but var_dump shows only string(4) "doc3". So the foreach statement in the UPDATE works correctly but it for some reason only the last value is update in the table. – Giuseppe Jun 23 '20 at 08:15
  • Solved it! Really silly mistake, but all I had to do is to pass an additional field to the where clause to identify the row as well. – Giuseppe Jun 23 '20 at 10:26

0 Answers0