如何使用 MVC 使用 PHP 表单更新 MySQL 数据库
How to update a MySQL Database using a PHP form using MVC
我正在做一个学校项目。
这里是link项目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> </th>
<th> </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'; ?>
我相信这会解决您的问题,但我假设您标记为 "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 -->
现在,我强烈建议您开始使用带有参数绑定的预准备语句,而不是将值直接连接/插入到您的 SQL 查询中。
我正在做一个学校项目。
这里是link项目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> </th>
<th> </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'; ?>
我相信这会解决您的问题,但我假设您标记为 "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 -->
现在,我强烈建议您开始使用带有参数绑定的预准备语句,而不是将值直接连接/插入到您的 SQL 查询中。