首页 > 解决方案 > 仅为提交的表单编辑的值更新数据库值

问题描述

我目前有一个包含客户信息的数据库。我还为每个客户提供了一个页面,它将数据库中的每个信息显示到表单的某些字段中,并且在同一页面中有一个更新按钮。这样,我只想在数据库中更新表单中已更改的字段。哪个是最好的方法?或者我只需要更新每一列,考虑到 mysql 更新函数只识别和更新已更改的值?

标签: phpdatabaseformsmysqli

解决方案


最好的方法是在 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
      }

}


推荐阅读