9

MySQL で OSCommerce を使用するプロジェクトに取り組んでおり、いつ tep_db_input() または tep_db_prepare_input() を使用すべきかについて混乱しています。挿入/更新されている文字列の周りで tep_db_input() を使用する必要があると思いますが、いつ他の関数を使用する必要がありますか?

たとえば、データベースから一部のデータをSELECTし、その結果を使用して行を別のテーブルにINSERTする場合、ある時点で入力を準備する必要がありますか? それとも、もう一度 tep_db_input を使用しますか?

$width = '3"'; // 3 inches
$new_height = '3\' 5"'; // 3 feet 5 inches

$result = tep_db_query(
    "SELECT height 
     FROM measurements 
     WHERE width = '".tep_db_input($width)."'"
);

while ($row = tep_db_fetch_array($result)) {
    tep_db_query(
        "INSERT INTO measurement_history (
            field, 
            old_value, 
            new_value
        ) VALUES (
            'height',
            '".tep_db_input($row['height'])."',
            '".tep_db_input($new_height)."'
        )"
    );
}

これは正しいです?

編集::これらの関数に慣れていない人のために、定義を次に示します。

function tep_sanitize_string($string) {
    $patterns = array ('/ +/','/[<>]/');
    $replace = array (' ', '_');
    return preg_replace($patterns, $replace, trim($string));
}

function tep_db_input($string, $link = 'db_link') {
    global $$link;

    if (function_exists('mysql_real_escape_string')) {
        return mysql_real_escape_string($string, $$link);
    } elseif (function_exists('mysql_escape_string')) {
        return mysql_escape_string($string);
    }

    return addslashes($string);
}

function tep_db_prepare_input($string) {
    if (is_string($string)) {
        return trim(tep_sanitize_string(stripslashes($string)));
    } elseif (is_array($string)) {
        reset($string);
        while (list($key, $value) = each($string)) {
            $string[$key] = tep_db_prepare_input($value);
        }
        return $string;
    } else {
        return $string;
    }
}
4

2 に答える 2

8

tep_db_input はmysql_real_escape_stringまたはmysql_escape_stringを使用します。これは、データベース入力を準備するための推奨される方法です。(そして、mysql_real_escape_string は PHP 5.5.0 から非推奨になるため、この関数は後のリリースで mysqli_real_escape_string() または類似のものを使用すると思います。)

mysql_real_escape_string を指定した tep_db_input は、単にエスケープを行います:

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, 
which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

tep_db_prepare_input は、空白のトリミング、括弧の置き換え、ストリップスラッシュの呼び出しによる引用符の解除 (!) など、さまざまなことを行います。

したがって、私のアドバイスは次のとおりです。常に tep_db_input を使用してください。また、空白などを取り除くために tep_db_prepare_input を使用する場合は、後で tep_db_input も使用してください。

于 2013-05-17T08:16:08.513 に答える