gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:55:51 27 4
gpt4 key购买 nike

我正在做一个学校项目。

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

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

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

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

明确地说,我必须遵循这个 MVC 结构。构建 php 文件而不是函数对我来说会容易得多,但这就是我应该这样做的方式。

这是我的代码。我首先列出了 Controller ,其次是模型,最后是 View 。

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

?>

包含我对 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);

}

?>

我的看法

客户 ListView

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

客户更新表单 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'; ?>

最佳答案

我相信这会解决您的问题,但我假设您标记为 “Customer Update Form View” 的表单位于 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 -->

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

关于php - 如何使用 MVC 使用 PHP 表单更新 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29552299/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com