MySQL에서 가져온 데이터를 기반으로 연결된 선택 상자를 만드는 다음 코드 스 니펫이 있습니다.AJAX - JSON 체인으로 연결된 동적 MySQL Select 요소는 텍스트에는 작동하지 않지만 숫자 값으로는 잘 작동합니다.
첫 번째 선택 항목은 PartTypeDescription이라는 열에서 DISTINCT를 사용합니다. 이 코드는 열의 값이 본질적으로 숫자 인 경우 효과적입니다 (예 : 11). 첫 번째 선택 영역을 선택하면 두 번째 선택 영역이 채워집니다.
데이터가 텍스트 인 경우 문제가 발생합니다 (예 : 배관). 예를 들어 배관을 선택하면 두 번째 선택 상자가 비어 있습니다. 두 번째 선택 상자가 올바르게 작동하지 않는 두 번째 쿼리를 가정합니다. 아래 코드에서 텍스트 값을 허용하지 않는 항목이 있습니까?
/* Configure the select boxes */
if (isset($_GET['key'])) {
$key = $_GET['key'];
switch ($key) {
case 'callTypeSelect':
$select = new SelectBox('What vehicle are you working from?','Choose a vehicle');
$res = mysql_query('SELECT DISTINCT PartTypeDescription FROM ' . DB_TABLE2);
$callTypes = array();
for ($i = 0; list($callType) = mysql_fetch_row($res); $i++) {
$callTypes[] = $callType;
$select->addItem($callType, 'brandSelect-' . $callType);
}
header('Content-type: application/json');
echo $select->toJSON();
break;
default:
if (strpos($key, 'brandSelect-') === 0) {
$callType = str_replace('brandSelect-', '', $key);
$resBrands = mysql_query('SELECT Invm_InventoryNumber FROM ' . DB_TABLE2
. ' WHERE PartTypeDescription = ' . mysql_real_escape_string($callType) . " ORDER BY Invm_InventoryNumber");
$select = new SelectBox('What part number are you looking for?', 'Pick a part');
for ($i = 0; list($brand) = mysql_fetch_row($resBrands); $i++) {
$select->addItem($brand, 'result-' . $brand . '-' . $callType);
}
header('Content-type: application/json');
echo $select->toJSON();
} elseif (strpos($key, 'result-') === 0) {
list($null, $brand, $callType) = explode('-', $key);
$res = mysql_query('SELECT * FROM ' . DB_TABLE2
. ' WHERE PartTypeDescription = \'' . mysql_real_escape_string($callType) . '\'
AND Invm_InventoryNumber = \'' . mysql_real_escape_string($brand) . "'");
$markup = '';
for ($i = 0; $row = mysql_fetch_assoc($res); $i++) {
//$row = array_map('htmlspecialchars', $row); it looks like the items is already encoded
$markup .= <<<HTML
굉장, 도움 주셔서 감사합니다! –