簡體   English   中英

如何檢測表單編輯,以便僅在數據庫上進行更新?

[英]How to detect form editing so that will update that only on database?

我正在使用一種包含50多個字段的表單。 我已經完成了記錄的插入,選擇和刪除操作,但是我對更新記錄感到困惑,因為如果用戶要更新50個字段中的一個或兩個,則更新查詢應該使用最少的字段。 但是我如何才能檢測到該字段已被編輯,並且該字段需要在服務器端進行更新? 有人請幫助我提供精確的解決方案。

要獲得精確的解決方案,將需要更多數據(我認為); 但是,我相信通過使用變量來構建更新語句並遍歷$ _POST數據(建議您未經測試),PHP可以完成您的要求。

//assumes control names on form = field names in db

$strSQL = "UPDATE tblWhatever SET ";
$fieldsAndvalues = "";
$markers = "";

foreach($_POST as $key => $value)
{
  if ($value <> "") {

  //code to check for $value's data type and set $marker to what is 
  //appropriate (e.g., string set $markers to "'", if numeric set to "", etc.)

     $fieldsAndvalues .= $key . "=" . $marker . $value . $marker . ","
  }
}

//Remove final comma (,), build final SQL statment, execute statement
if ($fieldsAndvalues <> "") {
  $strSQL .= substr($fieldsAndvalues,0,-1) . " WHERE <whatever criteria is used>";

  //execute query with whatever error checking and such you require
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM