- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一个学校项目。
这里是项目的链接 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> </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'; ?>
客户更新表单 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/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!