mardi 28 juin 2016

Why this code isn't update on db?


When I try update with a form, this code insert a new row in database instead updating it. I can't see where is the error.

Root code:

$app->post('/slides/update/{id}', function (Request $request, Response $response, $args) {

$slide_data['id_slide'] = (int)$args['id'];
$slide_data['title'] = filter_var($data['title'], FILTER_SANITIZE_STRING);
...
$slide_data['text_3'] = filter_var($data['text3'], FILTER_SANITIZE_STRING);

$slide = new SlideEntity($slide_data);
$slide_mapper = new SlideMapper($this->db);
$slide_mapper->update($slide);

$response = $response->withRedirect('/slides');
return $response;
});

SlideMapper code:

public function update(SlideEntity $slide) {
$sql = "UPDATE slide
        SET id_client=:id_client, title=:title ... text_3=:text_3
        WHERE id_slide=:id_slide";

$stmt = $this->db->prepare($sql);
$result = $stmt->execute([
  "id_slide" => $slide->getId(),
  "url" => $slide->getUrl(),
  ...
  "text_3" => $slide->getText_3(),
]);
}

SlideEntity code:

class SlideEntity
{

protected $id;
protected $url;
...
protected $text_3;


public function __construct(array $data) {
    if(isset($data['id_slide'])) {
        $this->id = $data['id_slide'];
    }

    $this->title = $data['title'];
    $this->description = $data['description'];
    ...
    $this->text_3 = $data['text_3'];
}

I will appreciate any help :)


Aucun commentaire:

Enregistrer un commentaire