title: How to update a MySQL Database using a PHP form using MVC
body: <p>I am doing a school project.</p>
  
  <p>Here is a link to the project <a href="http://www.dsu-class.com/zito82/lab10/" rel="nofollow">http://www.dsu-class.com/zito82/lab10/</a></p>
  
  <p>I need to use the MVC model to code a PHP application. I've gotten all the steps done except for one. I was required to add an Update input button to the list of customers. From this input button I launch into the update form. When I submit this form it is supposed to update the customer data.</p>
  
  <p>I am having two problems. I created the list of customers via a foreach loop and that assigns a customerID to each update button however once I pass through to the form I cannot pull the customerID to pass through with the form. </p>
  
  <p>The second problem is that my form does not update the MYSQL database.</p>
  
  <p>To be clear, I have to follow this MVC Structure. It would be much easier for me to build php files instead of functions but that is how I am supposed to do this.</p>
  
  <p>Here is my code. I have the Controller listed first, the Model(s) second, and the view last.</p>
  
  <pre><code>&lt;?php
  require('../model/database.php');
  require('../model/customer-db.php');
  
  if (isset($_POST['action'])) {
      $action = $_POST['action'];
  } else if (isset($_GET['action'])) {
      $action = $_GET['action'];
  } else {
      $action = 'display_customers';
  }
  
  if ($action == 'display_customers') {
      $customers = get_customers();
      include '../view/customer-list.php';
  }
  else if ($action == 'view_customerData') {
      $customerID = $_GET['customerID'];
      view_customerData($customerID);
      include '../view/customer-information.php';
  }
  else if ($action == 'update_customer') {
      $customerID = $_POST['customerID']; $firstName = $_POST['firstName']; $lastName = $_POST['lastName'];
      $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $postalCode = $_POST['postalCode'];
      $countryCode = $_POST['countryCode']; $phone = $_POST['phone']; $email = $_POST['email'];
  
      update_customer($customerID, $firstName, $lastName, $address, $city, $state, $postalCode, $countryCode, $phone, $email);
      $customers = get_customers();
      include '../view/customer-list.php';
  }
  else if ($action == 'delete_customer') {
      $customerID = $_POST['customerID'];
      delete_customer($customerID);
      $customers = get_customers();
      include '../view/customer-list.php';
  }
  else if ($action == 'under-construction') {
      include('../under-construction.php');
  } else
  
  ?&gt;
  </code></pre>
  
  <p>Model containing my function calls for the Controller</p>
  
  <pre><code>&lt;?php
  require_once('database.php');
  
  function get_customers() {
      global $db;
      $query = "SELECT * FROM customers
                ORDER BY lastName";
      $customers = $db-&gt;query($query);
      return $customers;
  }
  
  function delete_customer($customerID) {
      global $db;
      $query = "DELETE FROM customers
                WHERE customerID = '$customerID'";
          $db-&gt;exec($query);
  }
  
  function view_customerData ($customerID) {
      global $db;
      $query = "SELECT * FROM customers
                WHERE customerID = '$customerID'";
      $customerData = $db-&gt;query($query);
      $customerData = $customerData-&gt;fetch();
      return $customerData;
  }
  
  function update_customer($customerID, $firstName, $lastName, $address, $city, $state, $postalCode, $countryCode, $phone, $email) {
      global $db;
      $query = "UPDATE customers
                SET
                    firstName = '$firstName', lastName = '$lastName', address = '$address', city = '$city', state = '$state',
                    postalCode = '$postalCode', countryCode = '$countryCode', phone = '$phone', email = '$email'
                WHERE customerID = '$customerID' ";
      $db-&gt;exec($query);
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>My views</p>
  
  <p>Customer List View</p>
  
  <pre><code>&lt;?php include 'header.php'; ?&gt;
  
      &lt;div id="main"&gt;
          &lt;div id="content"&gt;
              &lt;h2&gt; Customer List &lt;/h2&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Name&lt;/th&gt;
                      &lt;th&gt;Email Address&lt;/th&gt;
                      &lt;th&gt;Country Code&lt;/th&gt;
                      &lt;th&gt;&amp;nbsp;&lt;/th&gt;
                      &lt;th&gt;&amp;nbsp;&lt;/th&gt;
                  &lt;/tr&gt;
                  &lt;?php foreach ($customers as $customer) : ?&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;?php echo ($customer['lastName'] . "," . $customer['firstName']); ?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;?php echo strtolower($customer['email']); ?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;?php echo $customer['countryCode']; ?&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;form action="." method="get"&gt;
                              &lt;input type="hidden" name="action" value="view_customerData" /&gt;
                              &lt;input type="hidden" name="customerID" value="&lt;?php echo $customer['customerID']; ?&gt;" /&gt;
                              &lt;input type="submit" value="Update" /&gt;
                          &lt;/form&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;form action="." method="post"&gt;
                              &lt;input type="hidden" name="action" value="delete_customer" /&gt;
                              &lt;input type="hidden" name="customerID" value="&lt;?php echo $customer['customerID']; ?&gt;" /&gt;
                              &lt;input type="submit" value="Delete" /&gt;
                          &lt;/form&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;?php endforeach; ?&gt;
              &lt;/table&gt;
           &lt;/div&gt;
      &lt;/div&gt;
  &lt;?php include 'footer.php'; ?&gt;
  </code></pre>
  
  <p>Customer Update Form View</p>
  
  <pre><code>&lt;?php include 'header.php'; ?&gt;
  
      &lt;div id="main"&gt;
          &lt;div id="content"&gt;
              &lt;h2&gt; Update Customer &lt;/h2&gt;
              &lt;form action="../customer-manager/index.php" method="post" id="aligned"&gt;
                  &lt;input type="hidden" name="action" value="update_customer" /&gt;
                  &lt;input type="hidden" name="customerID" id="customerID" /&gt;
                  &lt;label for="firstName"&gt;First Name:&lt;/label&gt;
                  &lt;input type="text" name="firstName" id="firstName" autofocus&gt;&lt;/br&gt;
                  &lt;label for="lastName"&gt;Last Name:&lt;/label&gt;
                  &lt;input type="text" name="lastName" id="lastName"&gt;&lt;/br&gt;
                  &lt;label for="address"&gt;Address:&lt;/label&gt;
                  &lt;input type="text" name="address" id="address"&gt;&lt;/br&gt;
                  &lt;label for="city"&gt;City:&lt;/label&gt;
                  &lt;input type="text" name="city" id="city"&gt;&lt;/br&gt;
                  &lt;label for="state"&gt;State:&lt;/label&gt;
                  &lt;input type="text" name="state" id="state"&gt;&lt;/br&gt;
                  &lt;label for="postalCode"&gt;Postal Code:&lt;/label&gt;
                  &lt;input type="text" name="postalCode" id="postalCode"&gt;&lt;/br&gt;
                  &lt;label for="countryCode"&gt;Country Code:&lt;/label&gt;
                  &lt;input type="text" name="countryCode" id="countryCode"&gt;&lt;/br&gt;
                  &lt;label for="phone"&gt;Phone:&lt;/label&gt;
                  &lt;input type="tel" name="phone" id="phone"&gt;&lt;/br&gt;
                  &lt;label for="email"&gt;Email:&lt;/label&gt;
                  &lt;input type="email" name="email" id="email"&gt;&lt;/br&gt;
                  &lt;label for="password"&gt;Password:&lt;/label&gt;
                  &lt;input type="password" name="password" id="password"&gt;&lt;/br&gt;
                  &lt;label for="update_customer"&gt; &lt;/label&gt;
                  &lt;input type="submit" value="Update Customer"&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;?php include 'footer.php'; ?&gt;
  </code></pre>