Samuel Prevost Samuel Prevost - 3 months ago 9
MySQL Question

Update a single cell MySQL PDO

I am trying to update a single cell and I don't know why but each time I try I got this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'partcipateInLolProject' in 'field list'


Here is the PHP code:

if(!isset($args['id'], $args['status'])){
return $response->withStatus(400)->withHeader('Location', $this->router->pathFor('list-online-contacts-page'));
}

$id = $args["id"];
$status = $args['status'];

$prep = $this->db->prepare("UPDATE `onlineinscription` SET `partcipateInLolProject` = :status WHERE `id` = :id");

$prep -> bindParam(':status', $status);
$prep -> bindParam(':id', $id);
$prep -> execute();


Those args are checked earlier by regex and are both 1 to 5 number long integer. When I run the exact same query on my mysql prompt it work like a charm, here is the test query :
UPDATE onlineinscription SET participateInLolProject = 1 WHERE id = 104


I checked the two variables (status and id) before binding them and they are as expected. The MySQL credentials are fine, on the right server/DB. The db is created earlier like this:

$db = $c->get('settings')['db'];
$pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'], $db['user'], $db['pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;

Answer

Spot the difference:

$prep = $this->db->prepare("[..snip..] `partcipateInLolProject` = :status WHERE `id` = :id");
                                            ^----

UPDATE onlineinscription SET participateInLolProject = 1 WHERE id = 104
                                 ^--- 

So... read the error message more closely next time?