如何使用MVC使用PHP表单更新MySQL数据库


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

我正在做一个学校项目。

这是该项目的链接http://www.dsu-class.com/zito82/lab10/

我需要使用MVC模型来编写PHP应用程序。除了一步外,我已经完成了所有的步骤。我被要求在客户列表中添加一个更新输入按钮。通过这个输入按钮,我进入更新表单。当我提交这个表单时,它应该更新客户数据。

我有两个问题。我通过foreach循环创建了客户列表,并为每个更新按钮分配了一个customerID,但一旦我转到表单,我就无法将customerID拉到表单中。

第二个问题是我的表单没有更新MYSQL数据库。

为了清楚起见,我必须遵循这个MVC结构。对我来说,构建php文件而不是函数会容易得多,但这就是我应该做的。

这是我的密码。控制器列在第一位,模型列在第二位,视图列在最后。

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

包含我的函数的模型调用控制器

<?php
require_once('database.php');
function get_customers() {
    global $db;
    $query = "SELECT * FROM customers
              ORDER BY lastName";
    $customers = $db->query($query);
    return $customers;
}
function delete_customer($customerID) {
    global $db;
    $query = "DELETE FROM customers
              WHERE customerID = '$customerID'";
        $db->exec($query);
}
function view_customerData ($customerID) {
    global $db;
    $query = "SELECT * FROM customers
              WHERE 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'
              WHERE customerID = '$customerID' ";
    $db->exec($query);
}
?>

我的观点

客户列表视图

<?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 as $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'; ?>

客户更新表单查看

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

我相信这会解决您的问题,但我假设您标记为"客户更新表单视图"的表单在customer-information.php中。

在问题顶部的任何文件中,对于view_customerData操作。。。

else if ($action == 'view_customerData') {
    $customerID = $_GET['customerID'];
    $customer = view_customerData($customerID); // note the return value is now assigned
    include '../view/customer-information.php';
}

然后,在customer-information.php中,用数据预填充表单。主要缺少的是客户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 -->

现在,我强烈建议您开始使用带有参数绑定的准备好的语句,而不是将值直接连接/插值到SQL查询中。