Perform an update of an array using pdo

0

I want to update this table with this array :

$datos[] = array('id_comprobantes' => $value['id_comprobantes'],
                    'estado' => "GENERADO");

With this he sent it to the model:

$actu = $this->model->actualizar_est($datos);

And when I get to try this but nothing does not work for me.

public function actualizar_est($data)
    {

                foreach ($data as $clave => $valor) {


                    $fieldNames = implode('', '', array_keys($valor));
                    $fieldValues = ':' . implode(', :', array_keys($valor));
                    $sth = $this->db->update('comprobantes',$fieldNames, "'id_comprobantes' = $fieldValues");

                    foreach ($valor as $key => $value) {
                        $sth->bindValue(":$key", $value);
                    }
                    $sth->execute(); //Ejutamos el segunto Insert
                    if ($sth == true) {
                    $this->sms = "Actualizado";
                    }else{
                    $this->sms = "No se pudo actualizar el registro";
                    }
                }
        return $this->sms;
    }

How you can get it to update correctly.

    
asked by Fernando Abel Gonzales Ch 17.07.2018 в 16:51
source

1 answer

1

Because of the little that can be found in the documentation, the problem is in the structure of the array, it is not necessary to make implodes and separate the names of the fields from their corresponding value and it would not be necessary to execute (), since the update () is responsible for executing the query. I recommend that when you are going to use a framework, you should use one that has enough documentation so that you can guide yourself, in that order of ideas, I propose the following to solve it:

The documentation indicates the following example:

$postdata = array(
    'firstName' => $firstName,
    'lastName'  => $lastName,                                 
    'email'     => $email                            
);

$where = array('contactID' => $id);

$this->model->updateContact($postdata, $where); 

public function updateContact($data, $where)
{
    $this->db->update(PREFIX.'contacts',$data, $where);
}

In that case, and considering that each data array is a record to update (I do not know why you make an insert) the code would look like this:

public function actualizar_est($data)
{

    foreach ($data as $valor) {

        $where = array('id_comprobantes' => $valor['id_comprobantes']);
        $info = array('estado' => $valor['estado']);

        $sth = $this->db->update('comprobantes',$info, $where);

        if($sth == true){
            #Codigo...
        }else{
            #Codigo...
        }
    }
}

Try it and let me know if it works for you - in any case I do not have experience with the framework in question

    
answered by 17.07.2018 / 21:14
source