2012-07-19 19 views
4

我似乎無法想出將數量可變的字段添加到數據庫表的正確方法。假設我正在構建一個Web應用程序,訪問者可以註冊並管理他們的聯繫人。用PHP添加可變數量的字段到數據庫表中

  • 聯繫人姓名
  • 電話號碼
  • E-mail地址
  • 公司
  • 地址
  • 首頁
  • :他們可以通過一種形式,它具有以下文本輸入字段增加個人聯繫方式

在這種形式,只有'聯繫人姓名'字段是必需的,所以我t完全可以在不知道他或她的電話號碼的情況下添加聯繫人的電子郵件地址。我怎樣才能在PHP中正確實現這一點?

他們之前做過的方式涉及迭代$ _POST數組,並檢查每個特定字段是否已提交。如果一個字段已經被提交,它的字段名將被添加到SQL語句中。然後我們也爲這些值做同樣的事情。

我的直覺告訴我,這種工作方式是可怕的錯誤,但我想不出任何其他方式來做到這一點...

function generateSQLStatement($_POST) { 

    // Add field names to SQL statement 
    foreach ($_POST as $field => $value) { 
     if (!empty($value)) { 
      $sql .= $field . ', '; 
     } 
    } 

    // Remove last comma and space 
    $sql = substr($sql, 0, -2); 

    // Add values to SQL statement 
    $sql .= ') VALUES ('; 
    foreach ($_POST as $field => $value) { 
     if (!empty($value)) { 
      $sql .= $value . ', '; 
     } 
    } 

    // Remove last comma and space 
    $sql = substr($sql, 0, -2); 

    $sql .= ')'; 

    return $sql; 
} 
+0

查看[活動記錄模式](http://en.wikipedia.org/wiki/Active_record_pattern)。我曾經這樣做,就像你現在做的那樣,但是將每個表格的列建模爲對象類使得它更容易處理。更少的錯誤。 – Stegrex 2012-07-19 09:26:08

+0

我真的希望你們正在清理那些已發佈的值... – megaflop 2012-07-19 09:27:25

+0

目前的方法沒有什麼特別的錯誤。我不會依賴數據庫字段名稱的職位名稱,我會爲這些數據,但仍然循環,看看他們是否填寫是好的。它並不是真的可變數量的字段,字段是固定的,它只是一些是空的(非常普通的東西)。提交空的不會受傷 – 2012-07-19 09:29:19

回答

3

注意,在下面SANITIZE_ME代碼諸如mysqli::real_escape_string之類的方法的佔位符,或者任何適合您的情況的方法。您需要適當調整這個答案。

function generateSQLStatement($_POST) { 

    $fieldNames = ""; 
    $values = ""; 

    foreach ($_POST as $field => $value) { 
     if (!empty($value)) { 
      if (!empty($fieldNames)) { 
       $fieldNames .= ','; 
       $values .= ','; 
      } 
      $fieldNames .= SANITIZE_ME($field); 
      $values .= "'" . SANITIZE_ME($value) . "'"; 

     } 
    } 

    return "($fieldNames) VALUES ($values)"; 
} 

這種方法只使用一個循環,所以速度更快。但是,您可能想根據預定義的可接受字段數組驗證您的字段名稱,以防某人編輯發佈到您的腳本中的表單並輸入無效的字段名稱。

編輯

一個更廣義的方法可以用來創建一個實用的功能,可以在整個應用程序中其他表輕鬆地重複:

這很多可以在一些通用的去包括文件:

// An array whose keys are valid table names and 
// whose values are arrays of valid field names 
// within the table named in the key 
$acceptableFields = array(
    'contacts' => array(
     // valid fields in the 'contacts' table 
     'name', 'address' //... 
    ) 
    // ... other mappings, if desired 
); 

function isValidField($table, $field) { 
    if (!isset($acceptableFields[$table])) 
     return false; 

    return in_array($field, $acceptableFields[$table]); 
    // Note that in_array is case-sensitive, so you may want 
    // to just manually loop through $acceptableFields[$table] 
    // and compare strings yourself. 
} 

function insertData($table, array $fieldValuesMap, mysqli $mysqli) { 
    // First, some self-explanatory validation: 
    if ($table === null) 
     throw new InvalidArgumentException('$table cannot be null'); 

    if (!is_string($table)) 
     throw new InvalidArgumentException('$table must be a String'); 

    if (empty($table)) 
     throw new InvalidArgumentException('$table cannot be an empty String'); 

    if (!isset($acceptableFields[$table])) 
     throw new InvalidArgumentException("\"$table\" is an invalid table name"); 

    $fieldNames = ""; 
    $values = ""; 

    foreach ($fieldValuesMap as $field => $value) { 
     // check the field name is valid for the given table 
     // and that the value is not empty. You may want to 
     // add a logging mechanism for invalid field names to 
     // help track bugs or even malicious use 
     if (isValidField($table, $field) && !empty($value)) { 
      // check to see whether there are any items in 
      // the lists already 
      if (!empty($fieldNames)) { 
       // yes, so add commas: 
       $fieldNames .= ','; 
       $values .= ','; 
      } 

      // no need to escape the field name as we have already 
      // checked that it is valid 
      $fieldNames .= $field; 
      // but we do need to escape the value 
      $values .= "'" . $mysqli->real_escape_string($value) . "'"; 

     } 
    } 

    // check whether we've actually got anything to insert: 
    if (empty($fieldNames)) 
     return NULL; 

    return $mysqli->query("INSERT INTO $table ($fieldNames) VALUES ($values)"); 
} 

在頁面上使用示例來添加聯繫人:

require_once "above.file"; // whatever it's called 

if ($_POST) { 

    $mysqli = new MySQLi(/*...*/); 
    if (mysqli_connect_errno()) { 
     // handle connection error 
    } else { 
     // set your charset 
     $mysqli->set_charset("utf8"); // or whatever you use 

     $result = insertData('contacts', $_POST, $mysqli); 

     if ($result === NULL) { 
      // There was nothing to insert 
     } elseif ($result === FALSE) { 
      // An error occurred, handle it here 
     } else { 
      // Success! Use $mysqli->insert_id to get your new 
      // record's ID (if appropriate). 
     } 
    } 

} 
// 
//============================================== 

有點額外的工作,你最終得到的是靈活和可重用的東西。但個人而言,我更喜歡更面向對象(主動記錄)的方法。

+0

我同意這個答案。你也可以考慮簡單地插入空字段的選項。 – 2012-07-19 09:46:01

+0

@FranciscoO。是的,我認爲只是插入了空字符串,但後來認爲在他們的應用程序的其他地方讀取這些字段時,OP可能依賴於這些字段中的NULL值(或其他默認值)。 – megaflop 2012-07-19 09:52:36

+0

我的應用程序確實依賴NULL值,所以空字符串是沒有選擇的。 – user1440560 2012-07-19 11:04:55