【问题标题】:Update database values only for values edited by a submitted form仅为提交的表单编辑的值更新数据库值
【发布时间】:2018-11-03 01:10:31
【问题描述】:

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

【问题讨论】:

  • 检查哪些字段已更改。仅为这些字段运行更新功能。确保使用准备好的函数......你有没有尝试过什么?

标签: php database forms mysqli


【解决方案1】:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多