2012-06-07 69 views
1

我有一個<select multiple="multiple">在php中獲得多個選擇。從那以後,我把它放在一個變量與POST方法和我嘗試至少呼應陣列,以確保我的變量$authors有選擇,但它打印此錯誤在MySQL表中插入多個選項

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ex\addBook.php on line 45

這裏是我的代碼:

<?php 
    include ("includes/connections.php"); 
    $authors = $_POST["authors"]; 
    foreach ($authors as $author) 
    { 
     echo $author; 
    } 
?> 

這裏是 '選擇多個' 我在HTML:

<?php multiple("author_ID", "author_firstname", "author_lastname", "author", "author_lastname", "authors"); ?> <span style="font-size: 12px; color: #666;">(Keep down Ctrl for selecting multiple authors)</span> 

這裏是多重功能:

<?php include ("includes/connections.php"); 
    function multiple($intIdField, $strfNameField, $strlNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") { 
     echo "<select multiple=\"multiple\" name=\"$strNameOrdinal\">\n"; 

     $strQuery = "select $intIdField, $strfNameField, $strlNameField 
        from $strTableName 
        order by $strOrderField $strMethod"; 

     $rsrcResult = mysql_query($strQuery); 

     while($arrayRow = mysql_fetch_assoc($rsrcResult)) { 
      $strA = $arrayRow["$intIdField"]; 
      $strB = $arrayRow["$strlNameField"] . " " . $arrayRow["$strfNameField"];  
      echo "<option value=\"$strA \">$strB</option>\n"; 
     } 

     echo "</select>"; 
    } 
?> 

所以我猜想,$authors變量將是一個數組,我會很高興至少打印,但正如我已經說過,這不會發生。

從數據庫中提取的作者正確顯示沒有任何問題。事情是我想要從'選擇多個'列表中選出的那些被回顯出來。

+0

的'$ authors'陣列沒有被填充的某些原因。你真的應該檢查這種情況,以防止發生這種錯誤。 –

+0

如何從'