dongti7838 2015-04-10 02:24
浏览 51
已采纳

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

I am doing a school project.

Here is a link to the project http://www.dsu-class.com/zito82/lab10/

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.

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.

The second problem is that my form does not update the MYSQL database.

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.

Here is my code. I have the Controller listed first, the Model(s) second, and the 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 my function calls for the Controller

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

}

?>

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

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'; ?>
  • 写回答

1条回答 默认 最新

  • douyiji3919 2015-04-10 03:22
    关注

    I believe this will solve your problem but I'm making the assumption that the form you've labelled as "Customer Update Form View" is in customer-information.php.

    In whatever file is at the top of your question, for the view_customerData action...

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

    Then, in customer-information.php, pre-fill the form with data. The main thing missing is the 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, I highly recommend you start using prepared statements with parameter binding instead of concatenating / interpolating values directly into your SQL queries.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。