cuốn sách gpt4 ai đã làm

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

In lại Tác giả: Walker 123 更新时间:2023-11-29 01:55:51 27 4
mua khóa gpt4 Nike

我正在做一个学校项目。

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

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

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

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

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

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

require('../model/database.php');
require('../model/customer-db.php');

if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} khác {
$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');
} khác

?>

包含我对 Controller 的函数调用的模型

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





Customer List




Tên
Email Address
Country Code
 
 


























客户更新表单 View





Update Customer









































câu trả lời hay nhất

我相信这会解决您的问题,但我假设您标记为 “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...















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

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

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress