我必须<select multiple="multiple">
在 php.ini 中获得多种选择。之后,我使用该方法将它放入一个变量中,POST
并且我尝试至少将数组回显以确保我的变量$authors
具有选择但它会打印此错误
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
变量会是一个数组,我很乐意至少打印出来,但正如我已经说过的那样,这不会发生。
从数据库中获取的作者正确显示,没有任何问题。问题是我希望从“选择多个”列表中选择的那些被回显。