如何使用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'; ?>
dongpo1203
dongpo1203 真正。这就是为什么最好一砖一瓦地建造一座房子,而不是希望建筑物能站起来,灯光会继续下去,一旦落到地基上就不会破坏,就像一块大块一样。;-)一旦有人工作,其余的将会跟随。
5 年多之前 回复
douzaoqu4447
douzaoqu4447 这个问题中的代码太多了。话虽这么说,我相当自信我已经找到了问题所在
5 年多之前 回复
dongya1875
dongya1875 这肯定是一个棘手的问题。
5 年多之前 回复
dragonpeng200811111
dragonpeng200811111 是的,该操作由最后一个代码段中的表单触发(“客户更新表单视图”)。它不起作用,因为该表单中没有customerID值(请参阅上面的评论)
5 年多之前 回复
doukunsan5553
doukunsan5553 ......正在<formaction=“。”method=“get”><inputtype=“hidden”name=“action”value=“view_customerData”/><inputtype=“hidden”name=“customerID”value=“1118”/><inputtype=“submit“value=”更新“/>
5 年多之前 回复
doupishan3309
doupishan3309 OP还有if($action=='update_customer'){...},他们说更新不起作用。在链接中看到HTML源代码中的“更新”隐藏输入。
5 年多之前 回复
dongshan9619
dongshan9619 我认为这意味着以这种方式工作。他们单击运行view_customerData操作的Update按钮。OP没有明确解释,但我认为更新表格在customer-information.php中。很明显,列表视图中的表单中没有POST数据传递给update_customer
5 年多之前 回复
douhang5493
douhang5493 CustomerListView中的<inputtype=“hidden”name=“action”value=“view_customerData”/>指向应该为<inputtype=“hidden”的view_customerDataname=“action”value=“update_customer”/>试试吧。如果它有效,我会把它作为答案。上帝知道我应该;我只开始了20分钟。大声笑
5 年多之前 回复
doude2635
doude2635 您使用哪个MySQLAPI连接,mysqli_或PDO?好像是PDO。
5 年多之前 回复
douqiaoru2583
douqiaoru2583 我能看到的第一件事是更新表单中隐藏的customerID输入没有任何价值。此外,您没有将view_customerData()的返回值赋给任何东西。我将使用$customer=view_customerData($customerID);.然后,您可以在customer-information.php视图中使用$customer
5 年多之前 回复
dqcz57269
dqcz57269 是的,我最初删除除firstName之外的所有变量以尝试缩小更新范围,并且我没有将引号放回去。即使使用单个变量,我仍然无法使其工作。我觉得这很简单。
5 年多之前 回复
dongyanju1094
dongyanju1094 好吧,首先......所有这些变量都需要引用lastName=$lastName,address=$address,city=$city,state=$state,postalCode=$postalCode,countryCode=$countryCode,phone=$phone,email=$emailWHEREcustomerID=$customerID“;就像你对firstName='$firstName'所做的那样。错误检查会发出信号语法错误。
5 年多之前 回复

1个回答



我相信这会解决您的问题,但我假设您已标记为的表单“客户更新 表单视图“</ em>位于 customer-information.php </ code>。</ p>

在问题顶部的任何文件中, view_customerData </ code> action ... </ p>

  else if($ action =='view_customerData'){
$ customerID = $ _GET ['customerID'];
$ customer = view_customerData($ customerID); //注意现在已经分配了返回值
include'../view/customer-information.php';
n}
</ code>

nn

然后,在 customer-information.php </ code>,预先填写表格和数据。 缺少的主要是客户ID ... </ p>

 &lt; form action =“../ customer-manager / index.php”method =“post”id =“ 对齐“&gt; 

&lt; input type =”hidden“name =”action“value =”update_customer“&gt;
&lt; input type =”hidden“name =”customerID“id =”customerID“value =”&lt ;?= htmlspecialchars($ customer ['customerID'])?&gt;“&gt;

&lt; label for =”firstName“&gt;名字:&lt; / label&gt;
&lt; input type =”text“name =“firstName”id =“firstName”value =“&lt;?= htmlspecialchars($ customer ['firstName'])?&gt;” 自动对焦&gt;&lt; / br&gt;

&lt; label for =“lastName”&gt;姓氏:&lt; / label&gt;
&lt; input type =“text”name =“lastName”id =“lastName”value =“ &lt;?= htmlspecialchars($ customer ['lastName'])?&gt;“&gt;&lt; / br&gt;

&lt;! - etc - &gt;
</ code> </ pre>

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

展开原文

原文

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.

du5591
du5591 等待菲尔(回答者);-)
5 年多之前 回复
dourukeng5302
dourukeng5302 有效,不敢相信我错过了分配返回值。 我知道这很明显。 谢谢
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐