php - 仅为提交的表单编辑的值更新数据库值
问题描述
我目前有一个包含客户信息的数据库。我还为每个客户提供了一个页面,它将数据库中的每个信息显示到表单的某些字段中,并且在同一页面中有一个更新按钮。这样,我只想在数据库中更新表单中已更改的字段。哪个是最好的方法?或者我只需要更新每一列,考虑到 mysql 更新函数只识别和更新已更改的值?
解决方案
最好的方法是在 PHP 中为每个数据库对象(CustomerInformation)构建对象......然后你可以这样做:
include 'db.customerinformation.php'; /* you'll need to create this file, and populate it with code to do the following */
$cust = new CustomerInformation;
$cust->Open($_POST['DatabaseID']);
$cust->_CompanyName = $_POST['CompanyName'];
$cust->_Phone = $_POST['Phone'];
$cust->_Email = $_POST['Email'];
$cust->Update();
就是这样!这显然是对您需要做的事情的简化,但绝对是正确的方法。
db.customerinformation.php 示例
class CustomerInformation {
public $_DatabaseID = '';
public $_CompanyName = '';
function Open($id) {
// MySQL code to open the DB object ... you need to code this.
$this->_CompanyName = $row['CompanyName']; /*database field for company name */
}
function Update() {
// Make code to update the entire row based on $_DatabaseID
}
}
推荐阅读
- angular - rxjs:我怎样才能得知取消订阅 observable
- command-line-interface - Picocli:使用可选值指定选项的最佳方法,当未指定值时打印当前值
- google-drive-api - 通过 python api 将 zip 文件上传到谷歌团队驱动器
- amazon-web-services - 公开存储桶后,Amazon S3 存储桶中的文件被拒绝
- php - WooCommerce 向会员免费送货导致错误
- android - 0Dagger 2:MVVM 无法确定模块范围
- python - 如何在 Python 中使用正则表达式在字符串中进行以下替换?
- extjs - 从 ExtJS 5 升级到 6 后,X 轴不显示在笛卡尔图表中 ExtJS 6.5.3
- reactjs - 调度单个操作后带有新请求的 redux-saga 垃圾邮件
- vagrant - 在 Vagrant 配置脚本中设置 gsettings