How to update a MySQL Database using a PHP form using MVC -


i doing school project.

here link project http://www.dsu-class.com/zito82/lab10/

i need use mvc model code php application. i've gotten steps done except one. required add update input button list of customers. input button launch update form. when submit form supposed update customer data.

i having 2 problems. created list of customers via foreach loop , assigns customerid each update button once pass through form cannot pull customerid pass through form.

the second problem form not update mysql database.

to clear, have follow mvc structure. easier me build php files instead of functions how supposed this.

here code. have controller listed first, model(s) second, , view last.

<?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  ?> 

model containing function calls controller

<?php require_once('database.php');  function get_customers() {     global $db;     $query = "select * customers               order lastname";     $customers = $db->query($query);     return $customers; }  function delete_customer($customerid) {     global $db;     $query = "delete customers               customerid = '$customerid'";         $db->exec($query); }  function view_customerdata ($customerid) {     global $db;     $query = "select * customers               customerid = '$customerid'";     $customerdata = $db->query($query);     $customerdata = $customerdata->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'               customerid = '$customerid' ";     $db->exec($query);  }  ?> 

my views

customer list view

<?php include 'header.php'; ?>      <div id="main">         <div id="content">             <h2> customer list </h2>             <table>                 <tr>                     <th>name</th>                     <th>email address</th>                     <th>country code</th>                     <th>&nbsp;</th>                     <th>&nbsp;</th>                 </tr>                 <?php foreach ($customers $customer) : ?>                 <tr>                     <td><?php echo ($customer['lastname'] . "," . $customer['firstname']); ?></td>                     <td><?php echo strtolower($customer['email']); ?></td>                     <td><?php echo $customer['countrycode']; ?></td>                     <td>                         <form action="." method="get">                             <input type="hidden" name="action" value="view_customerdata" />                             <input type="hidden" name="customerid" value="<?php echo $customer['customerid']; ?>" />                             <input type="submit" value="update" />                         </form>                     </td>                     <td>                         <form action="." method="post">                             <input type="hidden" name="action" value="delete_customer" />                             <input type="hidden" name="customerid" value="<?php echo $customer['customerid']; ?>" />                             <input type="submit" value="delete" />                         </form>                     </td>                 </tr>             <?php endforeach; ?>             </table>          </div>     </div> <?php include 'footer.php'; ?> 

customer update form view

<?php include 'header.php'; ?>      <div id="main">         <div id="content">             <h2> update customer </h2>             <form action="../customer-manager/index.php" method="post" id="aligned">                 <input type="hidden" name="action" value="update_customer" />                 <input type="hidden" name="customerid" id="customerid" />                 <label for="firstname">first name:</label>                 <input type="text" name="firstname" id="firstname" autofocus></br>                 <label for="lastname">last name:</label>                 <input type="text" name="lastname" id="lastname"></br>                 <label for="address">address:</label>                 <input type="text" name="address" id="address"></br>                 <label for="city">city:</label>                 <input type="text" name="city" id="city"></br>                 <label for="state">state:</label>                 <input type="text" name="state" id="state"></br>                 <label for="postalcode">postal code:</label>                 <input type="text" name="postalcode" id="postalcode"></br>                 <label for="countrycode">country code:</label>                 <input type="text" name="countrycode" id="countrycode"></br>                 <label for="phone">phone:</label>                 <input type="tel" name="phone" id="phone"></br>                 <label for="email">email:</label>                 <input type="email" name="email" id="email"></br>                 <label for="password">password:</label>                 <input type="password" name="password" id="password"></br>                 <label for="update_customer"> </label>                 <input type="submit" value="update customer">             </form>         </div>     </div> <?php include 'footer.php'; ?> 

i believe solve problem i'm making assumption form you've labelled "customer update form view" in customer-information.php.

in whatever file @ top of question, view_customerdata action...

else if ($action == 'view_customerdata') {     $customerid = $_get['customerid'];     $customer = view_customerdata($customerid); // note return value assigned     include '../view/customer-information.php'; } 

then, in customer-information.php, pre-fill form data. main thing missing customer id...

<form action="../customer-manager/index.php" method="post" id="aligned">  <input type="hidden" name="action" value="update_customer"> <input type="hidden" name="customerid" id="customerid" value="<?= htmlspecialchars($customer['customerid']) ?>">  <label for="firstname">first name:</label> <input type="text" name="firstname" id="firstname" value="<?= htmlspecialchars($customer['firstname']) ?>" autofocus></br>  <label for="lastname">last name:</label> <input type="text" name="lastname" id="lastname" value="<?= htmlspecialchars($customer['lastname']) ?>"></br>  <!-- etc --> 

now, highly recommend start using prepared statements parameter binding instead of concatenating / interpolating values directly sql queries.


Popular posts from this blog