From 80bb52da21b72046aaebc037cb38907192003242 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Wed, 20 Sep 2017 13:08:24 -0400 Subject: [PATCH] update works with postgres --- controllers/people.php | 2 +- database/sql/update.sql | 1 + models/person.php | 12 ++++-------- 3 files changed, 6 insertions(+), 9 deletions(-) create mode 100644 database/sql/update.sql diff --git a/controllers/people.php b/controllers/people.php index e643ac5..67d4a94 100644 --- a/controllers/people.php +++ b/controllers/people.php @@ -18,7 +18,7 @@ if($_REQUEST['action'] === 'index'){ } else if ($_REQUEST['action'] === 'update'){ $requestBody = file_get_contents('php://input'); $body = json_decode($requestBody); - $updatedPerson = new Person($body->name, $body->age); + $updatedPerson = new Person(null, $body->name, $body->age); $allPeople = People::update($_REQUEST['id'], $updatedPerson); echo json_encode($allPeople); diff --git a/database/sql/update.sql b/database/sql/update.sql new file mode 100644 index 0000000..e1ec26a --- /dev/null +++ b/database/sql/update.sql @@ -0,0 +1 @@ +UPDATE people SET name = $1, age = $2 WHERE id = $3 diff --git a/models/person.php b/models/person.php index 38606c6..3a81f7b 100644 --- a/models/person.php +++ b/models/person.php @@ -35,15 +35,11 @@ class People { return self::find(); } - static function update($index, $updatedPerson){ - $people = array(); - $people[] = new Person('joni', 52); - $people[] = new Person('bob', 34); - $people[] = new Person('sally', 21); - $people[] = new Person('matt', 37); + static function update($id, $updatedPerson){ + $query = file_get_contents(__DIR__ . '/../database/sql/update.sql'); + $result = pg_query_params($query, array($updatedPerson->name, $updatedPerson->age, $id)); - $people[$index] = $updatedPerson; - return $people; + return self::find(); } } ?>