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
變量將是一個數組,我會很高興至少打印,但正如我已經說過,這不會發生。
從數據庫中提取的作者正確顯示沒有任何問題。事情是我想要從'選擇多個'列表中選出的那些被回顯出來。
的'$ authors'陣列沒有被填充的某些原因。你真的應該檢查這種情況,以防止發生這種錯誤。 –
如何從'
試試這個線程的答案:http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php –