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