2013-10-30 17 views
0

我有一個難題。我有一個簡單的表單。一個動態選擇列表,它從數據庫和textarea中提取技術名稱。該表格旨在幫助我粘貼郵政編碼(格式爲:12345,12346,12347等),並將其與技術編號一起插入表格中。這很好用!但是,任何zip都不行 - 因爲我必須有許可證才能在其中工作。看看數組中是否存在textarea的值

爲了解決這個令人討厭的問題,我創建了一個表格,其中包含我提供服務的郵政編碼列表。所以,我有這些郵政編碼的記錄集,我將它們插入到一個數組中,然後在插入之前檢查該數組中是否存在textarea的值。除了它不工作。數組打印良好 - 這樣的作品。插入的東西工作正常 - 就像之前我添加in_array的東西。一起 - 他們就像我的女朋友和我一樣。它只是不會一起工作。

這裏是我的代碼:

//This is my recordset and where I'm putting it into an array 
mysql_select_db($database_localhost, $localhost); 
$query_Recordset1 = "SELECT zip_code FROM zip_code"; 
$Recordset1 = mysql_query($query_Recordset1, $localhost) or die(mysql_error()); 
$row_Recordset1 = mysql_fetch_assoc($Recordset1); 
$totalRows_Recordset1 = mysql_num_rows($Recordset1); 
$results = array(); 
do { 
    $results[] = $row_Recordset1; 
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); 
//DONE ARRAY 
//Now I'm taking zipcodes pasted in textarea so they can be inserted one at a time 
$zipcodes = explode(",", $_POST['textarea']); 
$countZips = (count($zipcodes)); 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 
foreach($zipcodes as $key=>$value) 
{ 

while ($countZips >= 1) { 
$editFormAction = $_SERVER['PHP_SELF']; 
if (isset($_SERVER['QUERY_STRING'])) { 
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
} 
//DONE textarea 
    //If statement to see if value exists in array 
     if (in_array($value, $results)){ 
    $insertSQL = sprintf("INSERT INTO zip_zip (zip_code, tech_id) VALUES (%s, %s)", 
         GetSQLValueString($value, "int"), 
         GetSQLValueString($_POST['select'], "int")); 

    mysql_select_db($database_localhost, $localhost); 
    $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error()); 
    $countZips--; 
} else { 
    $countZips--; 
} 
//Done value exists 
//Now moving to next page when done 
} 
    $insertGoTo = "index.php"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
    $insertGoTo .= $_SERVER['QUERY_STRING']; 
    } 
    header(sprintf("Location: %s", $insertGoTo)); 
} 
} 

//END 

有誰知道在世界上的問題是?另外 - 如果有人編輯它,並使代碼格式漂亮 - 你怎麼做?

謝謝!

回答

0

更改以下行

$ results [] = $ row_Recordset1;

到下面的線 $ results [] = $ row_Recordset1 ['zip_code'];

在你的代碼中,當從DB獲取記錄時,你將每行分配到你的$ result數組中,這就是爲什麼當你打算檢查你的$ result數組中的$ value給你false結果時,bcoz你的$ result數組具有以下結構,按您的上述代碼

陣列 ( [0] =>數組 ( [ZIP_CODE] => 12312331 ) )

以及您需要下列用於in_array陣列結構()

陣列 ( [0] => 12312331 )

+0

這完美地工作,如果有剛投入的文本字段中的一個郵政編碼。如果有多個比如43213,99999那麼它什麼也不做。它不應該檢查每個郵政編碼,因爲它被插入循環? – SherwoodPro

+0

這可能實際上工作。我只是意識到如果不添加數組,它會反覆插入相同的郵政編碼,這是textarea中郵政編碼的次數。所以我的循環插入信息是錯誤的開始。 – SherwoodPro

相關問題