4

データベーステーブルに可変数のフィールドを追加する適切な方法を思い付くことができないようです。訪問者がサインアップして連絡先を管理できるWebアプリを構築しているとしましょう。次のテキスト入力フィールドを持つフォームを介して、個々の連絡先を追加できます。

  • 連絡先
  • 電話番号
  • 電子メールアドレス
  • 会社
  • 住所
  • ホームページ

このフォームでは、「連絡先名」フィールドのみが必要であるため、連絡先の電話番号を知らなくても、連絡先の電子メールアドレスを追加することは完全に可能です。これを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;
}
4

1 に答える 1

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)";
}

このアプローチは 1 つのループのみを使用するため、高速です。しかし、誰かがスクリプトに投稿しているフォームを編集して無効なフィールド名を入力した場合に備えて、事前定義された許容フィールドの配列に対してフィールド名を検証することをお勧めします。

編集

より一般化されたアプローチを使用して、アプリケーション全体で他のテーブルで簡単に再利用できるユーティリティ関数を作成できます。

このロットは、一般的なインクルード ファイルに入れることができます。

// 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).
        }
    }

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

少し余分な作業をすると、柔軟で再利用可能なものができあがります。ただし、個人的には、よりオブジェクト指向 (アクティブ レコード) のアプローチを好みます。

于 2012-07-19T09:34:40.067 に答える