【问题标题】:How to update a MySQL Database using a PHP form using MVC如何使用 MVC 使用 PHP 表单更新 MySQL 数据库
【发布时间】:2015-06-15 15:12:13
【问题描述】:

我正在做一个学校项目。

这里是项目的链接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'; ?>

【问题讨论】:

  • 嗯,一方面...所有这些变量都需要引用lastName = $lastName, address = $address, city = $city, state = $state, postalCode = $postalCode, countryCode = $countryCode, phone = $phone, email = $email WHERE customerID = $customerID";,就像你为firstName = '$firstName'所做的那样。错误检查会发出语法错误的信号。
  • 是的,我最初消除了除 firstName 之外的所有变量以尝试缩小更新范围,并且我没有放回引号。即使使用单个变量,我仍然无法使其工作。我觉得这很简单。
  • 我能看到的第一件事是您在更新表单中隐藏的customerID 输入没有任何价值。此外,您没有将 view_customerData() 的返回值分配给任何东西。我会选择$customer = view_customerData($customerID);。然后你可以在你的customer-information.php 视图中使用$customer
  • 您使用哪个 MySQL API 连接,mysqli_ 或 PDO?好像是 PDO。
  • 这个问题的代码太多了。话虽如此,我相当有信心我已经查明了问题

标签: php mysql forms model-view-controller


【解决方案1】:

我相信这会解决您的问题,但我假设您标记为 “客户更新表单视图” 的表单位于 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 查询中。

【讨论】:

  • 成功了,不敢相信我错过了分配返回值。我知道这是显而易见的。谢谢
  • 等着走菲尔(回答者);-)