From 0132259479a53d2de0c43a8b069d2f803a6ad9ea Mon Sep 17 00:00:00 2001 From: jingaworks Date: Wed, 22 Feb 2017 20:01:35 +0200 Subject: [PATCH] Update customers.php --- src/routes/customers.php | 65 +++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/src/routes/customers.php b/src/routes/customers.php index 30a4aa0..a76d050 100644 --- a/src/routes/customers.php +++ b/src/routes/customers.php @@ -31,7 +31,7 @@ $db = null; echo json_encode($customers); } catch(PDOException $e){ - echo '{"error": {"text": '.$e->getMessage().'}'; + echo '{"error": {"text": '.$e->getMessage().'} }'; } }); @@ -52,7 +52,7 @@ $db = null; echo json_encode($customer); } catch(PDOException $e){ - echo '{"error": {"text": '.$e->getMessage().'}'; + echo '{"error": {"text": '.$e->getMessage().'} }'; } }); @@ -87,56 +87,47 @@ $stmt->execute(); - echo '{"notice": {"text": "Customer Added"}'; + echo '{"notice": {"text": "Customer Added"} }'; } catch(PDOException $e){ - echo '{"error": {"text": '.$e->getMessage().'}'; + echo '{"error": {"text": '.$e->getMessage().'} }'; } }); // Update Customer $app->put('/api/customer/update/{id}', function(Request $request, Response $response){ $id = $request->getAttribute('id'); - $first_name = $request->getParam('first_name'); - $last_name = $request->getParam('last_name'); - $phone = $request->getParam('phone'); - $email = $request->getParam('email'); - $address = $request->getParam('address'); - $city = $request->getParam('city'); - $state = $request->getParam('state'); - - $sql = "UPDATE customers SET - first_name = :first_name, - last_name = :last_name, - phone = :phone, - email = :email, - address = :address, - city = :city, - state = :state - WHERE id = $id"; + $reguestParams = $request->getParams(); + + $updates = array_filter($reguestParams, function ($value) { + return null !== $value; + }); + + $sql = 'UPDATE customers SET'; + + $values = array(); + foreach ($updates as $name => $value) { + $sql .= ' '.$name.' = :'.$name.','; + $values[':'.$name] = $value; + } + + $sql = substr($query, 0, -1); + $sql .= " WHERE id = " . $id . ';'; try{ // Get DB Object $db = new db(); // Connect $db = $db->connect(); - + $stmt = $db->prepare($sql); + $stmt->execute($values); + $db = null; - $stmt->bindParam(':first_name', $first_name); - $stmt->bindParam(':last_name', $last_name); - $stmt->bindParam(':phone', $phone); - $stmt->bindParam(':email', $email); - $stmt->bindParam(':address', $address); - $stmt->bindParam(':city', $city); - $stmt->bindParam(':state', $state); - - $stmt->execute(); - - echo '{"notice": {"text": "Customer Updated"}'; + echo '{"notice": {"text": "Customer Updated"} }'; } catch(PDOException $e){ - echo '{"error": {"text": '.$e->getMessage().'}'; + echo '{"error": {"text": '.$e->getMessage().'} }'; } }); @@ -155,8 +146,8 @@ $stmt = $db->prepare($sql); $stmt->execute(); $db = null; - echo '{"notice": {"text": "Customer Deleted"}'; + echo '{"notice": {"text": "Customer Deleted"} }'; } catch(PDOException $e){ - echo '{"error": {"text": '.$e->getMessage().'}'; + echo '{"error": {"text": '.$e->getMessage().'} }'; } -}); \ No newline at end of file +});